SoFunction
Updated on 2025-03-06

Java selection sorting method and detailed explanation of examples

Selection sorting technique is a method of selecting the smallest element in an array and exchanging it with the first element of the array. Next, the second smallest element in the array is exchanged with the second element and vice versa.

This way, the smallest elements in the array are repeatedly selected and placed in place until the entire array is sorted.

Two subarrays are maintained for selection sorting:

Sort subarrays: In each iteration, find the smallest element and place it in place. This subarray is sorted.

Unsorted subarray: Unsorted remaining elements.

Selecting sorting is a simple and clear sorting technique. This technique involves finding the smallest element in each pass and placing it in the right place. Selection sort is ideal for smaller datasets because it can efficiently sort smaller datasets.

Therefore, we can say that the selection sort does not work for larger data lists.

Select the sorting algorithm

The general algorithm for selecting sorting is as follows:

Select Sort (A, N)

Step 1: Repeat Step 2 and Step 3 for K = 1 to N-1

Step 2: Minimum call routines (A, K, N, POS)

Step 3:

Swap A[K] with A[POS]

[End of loop]

Step 4: Exit

Minimum routine (A, K, N, POS)

Step 1: [Initialization] Set minimumItem = A[K]

Step 2: [Initialization] Set POS = K

Step 3:

For J = K+1 to N -1, repeat

if minimumItem > A [J]
set minimumItem = A [J]
set POS = J
[if end]
[End of loop]

Step 4: Return to POS

As you can see, the routine to find the smallest number is called when it is traversing the dataset. Once the smallest element is found, place it in the desired position.

Selection sorting implementation in Java

Now let's demonstrate a Java program that implements the selection sorting.

import .*;
class Main 
{ 
    static void sel_sort(int numArray[]) 
    { 
        int n = ;    
        // traverse unsorted array 
        for (int i = 0; i < n-1; i++) 
        { 
            // Find the minimum element in unsorted array 
            int min_idx = i; 
            for (int j = i+1; j < n; j++) 
                if (numArray[j] < numArray[min_idx]) 
                    min_idx = j;    
            // swap minimum element with compared element  
            int temp = numArray[min_idx]; 
            numArray[min_idx] = numArray[i]; 
            numArray[i] = temp; 
        } 
    }    
    public static void main(String args[]) 
    { 
        //declare and print the original array
        int numArray[] = {7,5,2,20,42,15,23,34,10};
        ("Original Array:" + (numArray)); 
        //call selection sort routine
        sel_sort(numArray); 
        //print the sorted array
        ("Sorted Array:" + (numArray)); 
    } 
} 

Output:

Original array:[7, 5, 2, 20, 42, 15, 23, 34, 10]
Sort arrays:[2, 5, 7, 10, 15, 20, 23, 34, 42]

Knowledge point expansion:

Bubble sorting idea

1: Outer loop:How many times does it take to control it?
Suppose you have 5 numbers, you have to go 4 times, and you don’t have to go for the last time. The last number is already in its position, so you have to go length-1 times.
2: Inner loop:Control compares one by one, and if it is found that the previous number is larger than the next number, it is exchanged.
Notice! Because the longer the better, the smaller the length, the length must be length-1-i.

package com.test_1;

public class Demo5_3 {

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

    int arr [ ] ={1,6,0,-1,9};
    int temp=0;//Intermediate value    //----------Bubbling sorting method    //Outer circulation, it decides how many trips it takes in total    for(int i = 0;i&lt;-1;i++){
      //The inner layer loop begins to compare one by one      //If we find that the previous number is larger than the next number, then exchange      for(int j=0;j&lt;-1-i;j++){
        if (arr[j]&gt;arr[j+1]) {
          //transposition          temp = arr[j];
          arr[j] = arr[j+1];
          arr[j+1] = temp;
        }
      }

    }
    //Output result    for(int i = 0;i&lt;;i++){
      (arr[i]);
    }

  }

}

This is the article about Java selection sorting and detailed explanation of examples. For more related Java selection sorting and example content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!