-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertionSortPart1.java
69 lines (60 loc) · 1.53 KB
/
InsertionSortPart1.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// https://www.hackerrank.com/challenges/insertionsort1
import java.util.Scanner;
/*
Sample input
10
2 3 4 5 6 7 8 9 10 1
sample output
2 3 4 5 6 7 8 9 10 10
2 3 4 5 6 7 8 9 9 10
2 3 4 5 6 7 8 8 9 10
2 3 4 5 6 7 7 8 9 10
2 3 4 5 6 6 7 8 9 10
2 3 4 5 5 6 7 8 9 10
2 3 4 4 5 6 7 8 9 10
2 3 3 4 5 6 7 8 9 10
2 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
*/
public class InsertionSortPart1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int s = in.nextInt();
int[] ar = new int[s];
for(int i=0;i<s;i++){
ar[i]=in.nextInt();
}
in.close();
insertIntoSorted(ar);
}
// to print the array ar
private static void printArray(int[] ar) {
for(int n: ar){
System.out.print(n+" ");
}
System.out.println("");
}
public static void insertIntoSorted(int[] ar) {
// Fill up this function
int val = ar[ar.length-1];
boolean b = false;
for(int i=ar.length-2;i>=0;i--){
// System.out.println(ar[i]+" "+val);
if(ar[i] > val) {
ar[i+1] = ar[i];
printArray(ar);
} else {
ar[i+1] = val;
b = true;
printArray(ar);
break;
}
}
// if val is less than all numbers in array
if(!b)
{
ar[0] = val;
printArray(ar);
}
}
}