-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion_Sort.java
More file actions
59 lines (53 loc) · 1.68 KB
/
Insertion_Sort.java
File metadata and controls
59 lines (53 loc) · 1.68 KB
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
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
public class Insertion_Sort{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of array : ");
// int size=sc.nextInt();
int n=sc.nextInt();
// ArrayList<Integer> a=new ArrayList<>();
// for(int x=0;x<size;x++){
// System.out.println("Enter element at index "+x);
// int d=sc.nextInt();
// a.add(d);
// }
// for(int x=1;x<a.size();x++){
// for(int y=x-1;y>=0;y--){
// // for(int i=0;i<size;i++){
// // System.out.println(a.get(i));
// // }
// System.out.println(a);
// while(){
// if(a.get(x)<a.get(y)){
// int swap=a.get(x);
// a.set(x,a.get(y));
// a.set(y,swap);
// }
// }
// }
// for(int x=0;x<size;x++){
// System.out.println(a.get(x));
// }
int ar[]=new int[n];
for(int x=0;x<n;x++){
System.out.println("Enter element at index "+x);
ar[x]=sc.nextInt();
}
for(int x=1;x<n;x++){
int temp=ar[x];
for(int y=x-1;y>=0;y--){
if(temp<ar[y]){
ar[y+1]=ar[y];
}
else{
ar[y+1]=temp;
break;
}
}
}
for(int x=0;x<n;x++){
System.out.println(ar[x]);
}
}
}