Implement Selection sort Java We will discuss a couple of methods to Implement Selection Sort In Java Algorithm. The compiler has been added so you can easily execute the given programs, alongside suitable examples and samples outputs added for Selection Sort In Java, For More Sorting Programs in Java Visit here.
- Using Array.
- Using Buffered Reader.
Selection Sort Algorithm Java – Using Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import java.util.Scanner; public class SSort { public static void Sort(int a[]) { int n=a.length,i,j,p,temp; for (i = 0; i < n - 1; i++) { p = i; for (j=i+1; j < n; j++) { if (a[p]>a[j]) p=j; } temp=a[p]; a[p]=a[i]; a[i]=temp; } } public static void printarray(int a[]) { for(int i=0; i < a.length; i++) { System.out.print(a[i]+" "); } } public static void main(String[] args) { int n, res,i; Scanner s = new Scanner(System.in); System.out.print("Enter number of elements in the array:"); n = s.nextInt(); int a[] = new int[n]; System.out.println("Enter "+n+" elements "); for( i=0; i < n; i++) { a[i] = s.nextInt(); } System.out.println( "elements in array "); printarray(a); Sort(a); System.out.println( "\nelements after sorting"); printarray(a); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
Enter number of elements in the array:6 Enter 6 elements 9 0 1 23 99 5 elements in array 9 0 1 23 99 5 elements after sorting 0 1 5 9 23 99 |
Explanation For Above Algorithm:
1) The entered numbers will store in to the int array a[] using for loop with the structure for( i=0; i < n; i++).
2) Printarray(int a[]) will print the numbers, from the index i=0 to i<length of the array.
3) Sort(int a[]) will sort the numbers in ascending order. The inner loop will find the next least number to the previous number and the outer loop will place the least number in proper position in the array.
Given numbers are 9, 0, 1, 23, 99, 5.
a) The inner loop will compare the first two numbers 9,0 , the least number is 0, then the loop compares 0 with 1, 23, 99, 5. There is no least number available than 0. So outer loop swap the 9,0 numbers. Then the series is 0, 9, 1, 23, 99, 5.
b) Now the inner loop compares the 9,1, the number 1 is the least than 9, then compare 1 with 23, 99, 5. Compare with the next elements, 1 is the least number. Outer loop swap the numbers 9,1. Now the series is 0, 1, 9, 23, 99, 5.
c) Compare 9,23, then the least number is 9, find the least number than 9. In this series 5 is least compare with 9, so the outer loop swap the numbers 9,5. The series is 0, 1, 5, 23, 99, 9.
d) Compare 23,99, the least number is 23, find the least number than 23, 9 is the least number in the remaining series, Outer loop swap the numbers 23,9.Now the series is 0,1,5,9,99,23.
e) Compare 99 with 23, 23 is the least number, swap the numbers 23,99.
After selection sort, the number series is 0, 1, 5, 9, 23, 99.
Using Buffered Reader
1) Buffered Reader class reads the text from character input stream. In this program, “InputStreamReader” is the character input stream reader, which converts bytes into characters “System.in” reads the bytes from the keyboard.
2) b.readLine() reads the text as string, convert the string into an integer using Integer.parseInt() method. Integer is wrapper class, it converts the string into an integer.
3) Sort() method will find the next least element to the previous element and place the least element in the proper position in the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import java.util.Scanner; import java.io.*; public class SSort { public static void Sort(int a[]) { int n=a.length,i,j,p,temp; for (i = 0; i < n - 1; i++) { p = i; for (j=i+1; j < n; j++) { if (a[p]>a[j]) p=j; } temp=a[p]; a[p]=a[i]; a[i]=temp; } } public static void printarray(int a[]) { for(int i=0; i < a.length; i++) { System.out.print(a[i]+" "); } } public static void main(String[] args) throws IOException { int n,i; BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter N: "); n=Integer.parseInt(b.readLine()); int a[] = new int[n]; System.out.println("enter "+n+" elements "); for(i= 0; i< n; i++) a[i] = Integer.parseInt(b.readLine()); System.out.println("elements in array "); printarray(a); Sort(a); System.out.println("\nelements after sorting"); printarray(a); } } |
Output:
1 2 3 4 5 6 7 8 9 10 |
enter N: 3 enter 3 elements 59 0 1 elements in array 59 0 1 elements after sorting 0 1 59 |