Selection Sort Java Algorithm | JavaSorting

Total Views : 52
Zoom In Zoom Out Read Later Print

we will discuss two different ways to implement selection sorting In Java Application. The project download URL has been added so that you can easily execute the given program, with appropriate examples and samples outputs added for selection sort In Java.

Here we are using two ways for sorting but the are not different from each other. Only difference is how we get input from the user and store it. Sorting of the element will be done in same way.

We will use following 2 ways to implement Selection Sort in Java

1. Using Buffered Reader.
2. Using Array.

Selection Sort Using Buffered Reader

SortBufferReader.java


package com.manjeet.prodevsblog;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SortBufferReader {

    public static void main(String[] args) throws IOException {

        System.out.println("Sorting Using Buffered Reader....");

        int n, i;
        BufferedReader b = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter Number of Elements (n):");
        n = Integer.parseInt(b.readLine());
        int[] array = new int[n];

        System.out.println("Enter "+n+" Elements (Press Enter After Each Element) ");

        for( i = 0; i < n; i++) {
            array[i] = Integer.parseInt(b.readLine());
        }

        System.out.println( "Elements in Array Before Sorting...");
        printArray(array);

        sortArray(array);

        System.out.println( "Elements in Array After Sorting");
        printArray(array);
    }

    public static void sortArray(int[] array) {
        int n = array.length, i, j, p, temp;
        for (i = 0; i < n - 1; i++) {
            p = i;
            for (j = i+1; j < n; j++) {
                if (array[p] > array[j])
                    p=j;

            }
            temp = array[p];
            array[p] = array[i];
            array[i] = temp;
        }
    }

    public static void printArray(int[] array) {
        for (int value : array) {
            System.out.print(value + " ");
        }
        System.out.println();
    }
}


Output:

Sorting Using Buffered Reader....
Enter Number of Elements (n):4
Enter 4 Elements (Press Enter After Each Element) 
2
1
8
4
Elements in Array Before Sorting...
2 1 8 4 
Elements in Array After Sorting
1 2 4 8 

Process finished with exit code 0

Selection Sort Using Array & Scanner

SortArray.java

package com.manjeet.prodevsblog;

import java.util.Scanner;

public class SortArray {

    public static void main(String[] args) {

        System.out.println("Sorting Using Array & Scanner....");

        int n, i;
        Scanner s = new Scanner(System.in);

        System.out.print("Enter Number of Elements (n):");
        n = s.nextInt();
        int[] array = new int[n];

        System.out.println("Enter "+n+" Elements (Press Enter After Each Element) ");

        for( i = 0; i < n; i++) {
            array[i] = s.nextInt();
        }

        System.out.println( "Elements in Array Before Sorting...");
        printArray(array);

        sortArray(array);

        System.out.println( "Elements in Array After Sorting");
        printArray(array);
    }

    public static void sortArray(int[] array) {
        int n = array.length, i, j, p, temp;
        for (i = 0; i < n - 1; i++) {
            p = i;
            for (j = i+1; j < n; j++) {
                if (array[p] > array[j])
                    p=j;

            }
            temp = array[p];
            array[p] = array[i];
            array[i] = temp;
        }
    }

    public static void printArray(int[] array) {
        for (int value : array) {
            System.out.print(value + " ");
        }
        System.out.println();
    }
}


Output:

Sorting Using Array & Scanner....
Enter Number of Elements (n):6
Enter 6 Elements (Press Enter After Each Element) 
5
0
3
45
88
4
Elements in Array Before Sorting...
5 0 3 45 88 4 
Elements in Array After Sorting 
5 0 3 45 88 4
Process finished with exit code 0

Sample Loop Iteration :

Elements in Array Before Sorting...
5, 0, 3, 45, 88, 4, 
Loop 0 Array Start----------------
5, 0, 3, 45, 88, 4, 
5, 0, 3, 45, 88, 4, 
5, 0, 3, 45, 88, 4, 
5, 0, 3, 45, 88, 4, 
5, 0, 3, 45, 88, 4, 
Array After Loop 0----------------
0, 5, 3, 45, 88, 4, 
Loop 0 Array End----------------
Loop 1 Array Start----------------
0, 5, 3, 45, 88, 4, 
0, 5, 3, 45, 88, 4, 
0, 5, 3, 45, 88, 4, 
0, 5, 3, 45, 88, 4, 
Array After Loop 1----------------
0, 3, 5, 45, 88, 4, 
Loop 1 Array End----------------
Loop 2 Array Start----------------
0, 3, 5, 45, 88, 4, 
0, 3, 5, 45, 88, 4, 
0, 3, 5, 45, 88, 4, 
Array After Loop 2----------------
0, 3, 4, 45, 88, 5, 
Loop 2 Array End----------------
Loop 3 Array Start----------------
0, 3, 4, 45, 88, 5, 
0, 3, 4, 45, 88, 5, 
Array After Loop 3----------------
0, 3, 4, 5, 88, 45, 
Loop 3 Array End----------------
Loop 4 Array Start----------------
0, 3, 4, 5, 88, 45, 
Array After Loop 4----------------
0, 3, 4, 5, 45, 88, 
Loop 4 Array End----------------
Elements in Array After Sorting
0, 3, 4, 5, 45, 88, 

1. The numbers entered by user will store in to the int Array array[] using for loop up-to to number entered by the user from console.

2. The function printArray(int[] array) will print the numbers of the array.

3.  sortArray(int[] array) will sort the numbers in ascending order. The inner loop will find the next smaller number to the previous number and the outer loop will place the least number in proper position in the array.

For example: given numbers are 5, 0, 3, 45, 88, 4.

a. The inner loop will compare the first two numbers 5,0 , the least number is 0, then the loop compares 0 with 3, 45, 88, 4. There is no least number available than 0. So outer loop swap the 5,0 numbers. Then the series is 0, 5, 3, 45, 88, 4.

b. Now the inner loop compares the 5,3, the number 3 is the least than 5, then compare 3 with 45, 88, 4. Compare with the next elements, 3 is the least number. Outer loop swap the numbers 5,3. Now the series is 0, 3, 5, 45, 88, 4.

c. Compare 5,45, then the least number is 5, find the least number than 5. In this series 4 is least compare with 5, so the outer loop swap the numbers 5,4. The series is 0, 3, 4, 45, 88, 5.

d. Compare 45,88, the least number is 45, find the least number than 45, 5 is the least number in the remaining series, Outer loop swap the numbers 45,5.Now the series is 0,3,4,5,88,45.

e. Compare 88 with 45, 45 is the least number, swap the numbers 45,88.

After selection sort, the number series is 0, 3, 4, 5, 45, 88.

You can download the Complete project from below link.

See More

Latest Photos