-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInsertion.java
More file actions
43 lines (33 loc) · 938 Bytes
/
Copy pathInsertion.java
File metadata and controls
43 lines (33 loc) · 938 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
34
35
36
37
38
39
40
41
42
43
package Sorting_Algo;
import java.util.Scanner;
public class Insertion {
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter Array Size : ");
int size = scan.nextInt();
int a[] = new int[size];
int temp;
System.out.println("Enter Array Elements : ");
for(int i=0; i<a.length; i++)
{
a[i] = scan.nextInt();
}
for(int i=0; i<a.length; i++) {
for (int j=1 ; j<a.length; j++){
temp = a[i];
i = j-1;
while(i>0 && a[i]> temp) {
a[i+1] = a[i];
i = i-1;
}
a[i+1] = temp;
}
}
System.out.print("Now the Array after Sorting is : ");
for(int x : a)
{
System.out.print( x + "\t");
}
}
}