-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertionSortP2.java
51 lines (45 loc) · 1.09 KB
/
InsertionSortP2.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
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.*;
import java.util.*;
public class InsertionSortP2
{
public static void insertionSortParrt2(int[] arr)
{
int pos = 1;
while (pos < arr.length)
{
int currentPos = pos;
for(int i = pos-1; i>=0 ; i-- )
{
if(arr[currentPos]<arr[i])
{
//insert the value
int temp = arr[currentPos];
arr[currentPos] = arr[i];
arr[i]=temp;
currentPos--;
}
}
pos++;
printarray(arr);
}
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int s = in.nextInt();
int[] arr = new int[s];
for(int i=0;i<s;i++)
{
arr[i]=in.nextInt();
}
insertionSortParrt2(arr);
}
private static void printarray(int[] arr)
{
for(int n: arr)
{
System.out.print(n+" ");
}
System.out.println("");
}
}