-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertation.java
More file actions
54 lines (39 loc) · 960 Bytes
/
Insertation.java
File metadata and controls
54 lines (39 loc) · 960 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
44
45
46
47
48
49
50
51
52
53
import java.util.Scanner;
public class Insertion {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter Number of Elements: ");
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println(" ");
System.out.println("Elements");
System.out.println(" ");
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" \t");
}
for(int i=1;i<a.length;i++)
{
int temp=a[i];
int j=i-1;
while(j>=0 && temp<=a[j])
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
System.out.println(" \n");
System.out.println(" ");
System.out.println("Sorting Elements");
System.out.println(" ");
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" \t");
}
}
}