-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion.java
More file actions
executable file
·55 lines (42 loc) · 1.1 KB
/
Insertion.java
File metadata and controls
executable file
·55 lines (42 loc) · 1.1 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
import java.util.*;
public class Insertion{
int[] list;
public Insertion(int length){
list = new int[length];
}
public void swap(int i,int j){
int temp;
temp=this.list[i];
this.list[i]=this.list[j];
this.list[j]=temp;
}
public void print(){
for (int el : this.list ) {
System.out.print(el+",");
}
System.out.println();
}
public void insertion(){
for (int i=0;i<this.list.length-1;i++ ) {
for (int j=i+1;j>0 ;j-- ) {
if(this.list[j]<this.list[j-1]){
swap(j,j-1);
}else{
break;
}
}
}
print();
}
public static void main(String arg[]){
Scanner s=new Scanner(System.in);
System.out.println("Please input the size");
int n = s.nextInt();
Insertion i=new Insertion(n);
System.out.println("Please input the values");
for (int ii=0;ii<5 ;ii++ ) {
i.list[ii]=s.nextInt();
}
i.insertion();
}
}