-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
50 lines (39 loc) · 1.36 KB
/
InsertionSort.java
File metadata and controls
50 lines (39 loc) · 1.36 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
import java.util.Scanner;
public class InsertionSort {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
//Starting from the first element we check,we increment element by 1.
//Checking with the before elements.
//If the previous element is greater than the current element ,then swap.
//Repeat untill array is fully sorted.
int[] arr;
int[] sortedArr;
int size,j;
System.out.print("Enter the size of array : ");
size=scanner.nextInt();
arr=new int[size];
sortedArr=new int[size];
System.out.print("\nEnter the array elements : \n");
for(int i=0;i<size;i++){
System.out.printf("Enter element %d : ",i);
arr[i]=scanner.nextInt();
}
System.out.print("\nUnsorted Array is : ");
for(int i=0;i<size;i++){
System.out.print(arr[i] + " ");
}
for(int i=0;i<size;i++){
j=i;
while(j>0 && arr[j-1]>arr[j]){
int temp = arr[j-1];
arr[j-1]=arr[j];
arr[j]=temp;
j--;
}
}
System.out.print("\nSorted Array is : ");
for(j=0;j<size;j++){
System.out.print(arr[j] + " ");
}
}
}