Selection Sort
import java.io.*;
public class SelectionSort
{
public static void main(String[] args)throws IOException
{
int[] a = new int[5]; // Declaring array of size 5
int i,j,c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Elements To Sort :-");
for(i=0;i<5;i++)
{
a[i] = Integer.parseInt(br.readLine()); // Taking Input
}
for(i =0;i<a.length-1;i++) // Selection Sort Logic
{
for(j =i+1; j<a.length;j++)
{
if(a[i] > a[j])
{
c= a[i];
a[i] = a[j];
a[j] = c;
}
}
}
System.out.println("The Sorted elements are :-");
for(i=0;i<5;i++)
{
System.out.print(a[i]+ " "); // Printing Elements
}
}
}
public class SelectionSort
{
public static void main(String[] args)throws IOException
{
int[] a = new int[5]; // Declaring array of size 5
int i,j,c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Elements To Sort :-");
for(i=0;i<5;i++)
{
a[i] = Integer.parseInt(br.readLine()); // Taking Input
}
for(i =0;i<a.length-1;i++) // Selection Sort Logic
{
for(j =i+1; j<a.length;j++)
{
if(a[i] > a[j])
{
c= a[i];
a[i] = a[j];
a[j] = c;
}
}
}
System.out.println("The Sorted elements are :-");
for(i=0;i<5;i++)
{
System.out.print(a[i]+ " "); // Printing Elements
}
}
}
/*
Enter Elements To Sort :-
INPUT :
4
2
6
7
1
OUTPUT :
The Sorted elements are :-
1 2 4 6 7
*/
Comments
Post a Comment