-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
33 lines (29 loc) · 847 Bytes
/
InsertionSort.java
File metadata and controls
33 lines (29 loc) · 847 Bytes
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
import java.util.*;
class InsertionSort{
public static void main(String[] args) {
int a[]= new int[5];
int temp;
System.out.println("Please enter array");
Scanner sc=new Scanner(System.in);
for(int i=0;i<=a.length-1;i++){
a[i]=sc.nextInt();
}
System.out.println("This is your array before sorting");
for(int i=0;i<=a.length-1;i++){
System.out.println(a[i]);
}
for(int i=1;i<a.length-1;i++){
temp=a[i];
int j=i-1;
while(j>=0&&a[j]>temp){
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
System.out.println("This is your array after sorting");
for(int i=0;i<=a.length-1;i++){
System.out.println(a[i]);
}
}
}