-
Notifications
You must be signed in to change notification settings - Fork 97
/
descInsertionSort.java
39 lines (38 loc) · 1.01 KB
/
descInsertionSort.java
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
import java.util.*;
public class descInsertionSort
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n; //size of the array
System.out.println("Enter the size of the array: ");
n = in.nextInt();
int[] arr = new int[n]; //declared array
for(int i=0;i<n;i++)
{
arr[i] = in.nextInt();
}
insertion(arr);
System.out.println("The sorted array: ");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
in.close();
}
public static void insertion(int[] array)
{
for(int i=1;i<array.length;i++)
{
int raw = array[i]; //created a raw element which we insert
int j;
for(j=i;j>0 && array[j-1] < raw;j--)
{
//shifting from left to right
array[j] = array[j-1];
}
array[j] = raw;
}
}
}