-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSort.java
More file actions
49 lines (49 loc) · 833 Bytes
/
insertionSort.java
File metadata and controls
49 lines (49 loc) · 833 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
import java.io.*;
class insertionSort
{
int n,b;
int a[]=new int[5];
void input() throws Exception
{
DataInputStream d=new DataInputStream(System.in);
System.out.print("Enter the number of elements:");
n=Integer.parseInt(d.readLine());
System.out.println("Enter the array elements");
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(d.readLine());
}
System.out.print("Enter the number to be searched:");
b=Integer.parseInt(d.readLine());
}
void sort()
{
int i,j;
for(i=1;i<n;i++)
{
b=a[i];
for(j=i-1;j>=0&&a[j]>b;j--)
{
a[j+1]=a[j];
}
a[j+1]=b;
}
}
void display()
{
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
class result
{
public static void main(String a[]) throws Exception
{
insertion p=new insertion();
p.input();
p.sort();
p.display();
}
}