forked from pranjal36/SortingAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion-sort.java
More file actions
40 lines (33 loc) · 962 Bytes
/
Insertion-sort.java
File metadata and controls
40 lines (33 loc) · 962 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
34
35
36
37
38
39
40
class Sort
{
static void insertionSort(int arr[], int n)
{
if (n <= 1)
{
return;
}
insertionSort( arr, n-1 );
int last = arr[n-1];
int j = n-2;
while (j >= 0 && arr[j] > last)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = last;
}
void display(int arr[])
{
for (int i=0; i<arr.length; ++i)
{
System.out.print(arr[i]+" ");
}
}
public static void main(String[] args)
{
int arr[] = {22, 21, 11, 15, 16};
insertionSort(arr, arr.length);
Sort ob = new Sort();
ob.display(arr);
}
}