Saturday, March 8, 2014

sorting methods in java

import java.util.Scanner;


public class sorting {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub


System.out.println("enter the elements ");
Scanner n=new Scanner(System.in);
int k = 1;
int[] a=new int[10];
int m=5;
a[0]=45;
a[1]=67;
a[2]=9;
a[3]=23;
a[4]=1;

System.out.println(" enter your choice of sorting\n1.bubble sort\n2.selection sort\n3.insertion sort ");
Scanner k=new scanner(System.in);
switch(k)
{

case 1: System.out.println(" sorted using bubble sort");

for(int i=0;i<m;i++)
{
for(int j=0;j<m-i-1;j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int i=0;i<m;i++)
System.out.println(a[i]+" ");break;
case 2:
System.out.println(" sorted using selection sort ");

for(int i=0;i<m;i++)
{
int loc = 0;
int temp=a[i];
int f=0;
for(int j=i+1;j<m;j++)
{
if(temp>a[i])
{
f=1;
loc=j;

}
}
if(f==1)
{
a[loc]=a[i];
a[i]=temp;
}
}
}
}
}

No comments:

Post a Comment

Contributors

Translate