Skip to content

Commit 12d7c48

Browse files
committed
insertion sort
1 parent 4ba9588 commit 12d7c48

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

InsertionSort.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.Scanner;
2+
3+
class InsertionSort
4+
{
5+
public static void main(String[] args)
6+
{
7+
int array[]=new int[6];
8+
Scanner input=new Scanner(System.in);
9+
10+
//Input
11+
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
12+
for(int i=0; i<6; i++)
13+
{
14+
array[i]=input.nextInt();
15+
}
16+
17+
//Sorting
18+
for(int i=0; i<6; i++)
19+
{
20+
int temp=array[i];
21+
int j=i-1;
22+
while(j>=0 && temp<array[j] )
23+
{
24+
array[j+1]=array[j];
25+
j--;
26+
}
27+
28+
array[j+1]=temp;
29+
}
30+
31+
//Output
32+
for(int i=0; i<6; i++)
33+
{
34+
System.out.print(array[i]+"\t");
35+
}
36+
37+
}
38+
}

SelectionSort.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.Scanner;
2+
3+
class SelectionSort
4+
{
5+
public static void main(String[] args)
6+
{
7+
int array[]=new int[6];
8+
Scanner input=new Scanner(System.in);
9+
10+
//Input
11+
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
12+
for(int i=0; i<6; i++)
13+
{
14+
array[i]=input.nextInt();
15+
}
16+
17+
//Sorting
18+
for(int i=0; i<6; i++)
19+
{
20+
int min=i;
21+
for(int j=i+1; j<6; j++)
22+
{
23+
if(array[j]<array[min])
24+
{
25+
min=j;
26+
}
27+
}
28+
int temp=array[i];
29+
array[i]=array[min];
30+
array[min]=temp;
31+
}
32+
33+
//Output
34+
for(int i=0; i<6; i++)
35+
{
36+
System.out.print(array[i]+"\t");
37+
}
38+
39+
}
40+
}

0 commit comments

Comments
 (0)