-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertion_Sort.java
51 lines (51 loc) · 1009 Bytes
/
Insertion_Sort.java
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
import java.util.*;
public class Insertion_Sort
{
int a[];
int n;
Insertion_Sort(int n)
{
this.n=n;
a=new int[n];
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter array");
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
}
void sort()
{
int h;
int p;
for(int i=0;i<n-1;i++)
{
h=a[i];
p=i;
for(int j=i;j<n-1;j++)
{
if(a[i]<a[j])
{
h=a[j];
p=j;
}
}
for(int k=i;k<p;k++)
a[k]=a[k+1];
a[p]=h;
}
}
void display()
{
System.out.println(Arrays.toString(a));
}
public static void main(String args[])
{
Insertion_Sort obj=new Insertion_Sort(5);
obj.input();
obj.display();
obj.sort();
obj.display();
}
}