insertion sort

This commit is contained in:
Anup Kumar Panwar 2016-07-19 12:33:07 +05:30
parent 4ba958863b
commit 12d7c48ee4
2 changed files with 78 additions and 0 deletions

38
InsertionSort.java Normal file
View File

@ -0,0 +1,38 @@
import java.util.Scanner;
class InsertionSort
{
public static void main(String[] args)
{
int array[]=new int[6];
Scanner input=new Scanner(System.in);
//Input
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
for(int i=0; i<6; i++)
{
array[i]=input.nextInt();
}
//Sorting
for(int i=0; i<6; i++)
{
int temp=array[i];
int j=i-1;
while(j>=0 && temp<array[j] )
{
array[j+1]=array[j];
j--;
}
array[j+1]=temp;
}
//Output
for(int i=0; i<6; i++)
{
System.out.print(array[i]+"\t");
}
}
}

40
SelectionSort.java Normal file
View File

@ -0,0 +1,40 @@
import java.util.Scanner;
class SelectionSort
{
public static void main(String[] args)
{
int array[]=new int[6];
Scanner input=new Scanner(System.in);
//Input
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
for(int i=0; i<6; i++)
{
array[i]=input.nextInt();
}
//Sorting
for(int i=0; i<6; i++)
{
int min=i;
for(int j=i+1; j<6; j++)
{
if(array[j]<array[min])
{
min=j;
}
}
int temp=array[i];
array[i]=array[min];
array[min]=temp;
}
//Output
for(int i=0; i<6; i++)
{
System.out.print(array[i]+"\t");
}
}
}