-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathInsertionSort
More file actions
31 lines (27 loc) · 856 Bytes
/
InsertionSort
File metadata and controls
31 lines (27 loc) · 856 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
public class InsertionSortDemo {
public void insertionsort(int A[], int n) {
for(int i=1;i<n;i++) {
int temp = A[i];
int position = i;
while(position>0 && A[position-1] > temp) {
A[position] = A[position-1];
position = position - 1;
}
A[position] = temp;
}
}
public void display(int A[], int n) {
for(int i=0;i<n;i++)
System.out.print(A[i] + " ");
System.out.println();
}
public static void main(String args[]) {
InsertionSortDemo s = new InsertionSortDemo();
int A[] = {3, 5, 8, 9, 6, 2};
System.out.println("Original Array: ");
s.display(A, 6);
s.insertionsort(A, A.length);
System.out.println("Sorted Array: ");
s.display(A, 6);
}
}