-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathInsertionSort.java
More file actions
33 lines (27 loc) · 818 Bytes
/
InsertionSort.java
File metadata and controls
33 lines (27 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class InsertionSort implements SortingStrategy
{
InsertionSort() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public int[] sort( int[] inputArray )
{
for( int i = 1; i < inputArray.length; i++ )
{
// a temporary copy of the current element
int tmp = inputArray[i];
int j;
// find the position for insertion
for( j = i; j > 0; j-- )
{
if( inputArray[j - 1] < tmp )
break;
// shift the sorted part to right
inputArray[j] = inputArray[j - 1];
}
// insert the current element
inputArray[j] = tmp;
}
System.out.println("Array is sorted using Insertion Sort Algorithm");
return inputArray;
}
}