-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path20.1 Insertion Sort.java
65 lines (51 loc) · 1.55 KB
/
20.1 Insertion Sort.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
/*
Akash is found of collecting unique coins. Every coin has a denomination i.e Z. One Afternoon, he has K coins. He wnat to arrange K coins in non-increaasing order as per denomination. Write a programming solution to help akash. - Note: Use Insertion Sort
Input Format
1st line contain K Coins
2nd line contain space separated denomination from K coins
Constraints
K>0 & K<50
Z>0 & Z<100
Output Format
space separated sorted denomination from K coins
Sample Input 0
5
10 4 3 7 8
Sample Output 0
10 8 7 4 3
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner sc = new Scanner(System.in);
//take input for number of coins: n
int n = sc.nextInt();
//take input for domination of each coin ie. Z as an array
int [] Z = new int [n];
for(int i=0;i<n;i++)
Z[i] = sc.nextInt();
//for sorting we use INSERTION SORT
for(int i=1;i<n;i++)
{
int key = Z[i];
int j = i-1;
while(j>=0)
{
if(key>Z[j])
{
Z[j+1] = Z[j];
Z[j] = key;
}
j--;
}
}
//printing the final sorted array ie. Z
for(int i:Z)
System.out.print(i+" ");
}
}