public class SelectionSort
{
public static void main(String args[])throws IOException
{
int ch;
InputStreamReader cin=new InputStreamReader(System.in);
BufferedReader obj=new BufferedReader (cin);
System.out.println("Enter the no. element");
int n=Integer.parseInt(obj.readLine());
int a[]=new int[n];
for(int i=0;i<=n-1;i++)
{
System.out.println("Enter the element");
a[i]=Integer.parseInt(obj.readLine());
}
do
{
System.out.println("\nMenu\n1.Selection Sort\n2.exit");
System.out.println("Enter the choice");
ch=Integer.parseInt(obj.readLine());
switch (ch)
{
case 1:Selectionsort(a,n);
System.out.println("Sorted Array is ");
for(int w=0;w<=n-1;w++)
System.out.println(a[w]);
break;
case 2:
break;
default:System.out.println("Wrong choice");
break;
}
}
while(ch!=2);
}
public static void Selectionsort(int a[],int n)
{
int min,p,t;
for(int i=0;i<=n-2;i++)
{
min=a[i];
p=i;
for(int j=i+1;j<=n-1;j++)
{
if (a[j]
{
min=a[j];
p=j;
}
}
t=a[p];
a[p]=a[i];
a[i]=t;
}
}
}
/***************************Output****************************************
Enter the no. element
5
Enter the element
6
Enter the element
10
Enter the element
50
Enter the element
-2
Enter the element
0
Menu
1.Selection Sort
2.exit
Enter the choice
1
Sorted Array is
-2
0
6
10
50
Menu
1.Selection Sort
2.exit
Enter the choice
2
Process Exit...
*/
0 comments:
Post a Comment