The road to entry for junior programmers the use of simple selection sort

Let’s introduce a relatively simple [selection sort] , as follows:

Selection sort is a simple and intuitive [sorting algorithm] , and no matter what data goes in, it takes O(n²) time complexity. So when using it, the smaller the data size, the better. The only benefit may be that it doesn’t take up extra memory space.

1. Algorithm steps

First find the smallest (largest) element in the unsorted sequence and store it at the beginning of the sorted sequence.

Continue to find the smallest (largest) element from the remaining unsorted elements and place it at the end of the sorted sequence.

Repeat step 2 until all elements are sorted.

2. Code implementation

JavaScript code implementation

function selectionSort(arr) {
    var len = arr.length;
    var minIndex, temp;
    for (var i = 0; i < len - 1; i++) {
        minIndex = i;
        for ( var j = i + 1 ; j < len; j++) {
             if (arr[j] < arr[minIndex]) {      // find the smallest number 
                minIndex = j;                  // save the index of the smallest number
            }
        }
        temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;
    }
    return arr;
}

Python code implementation

def  selectionSort (arr) : 
    for i in range(len(arr) - 1 ):
         # record the index of the smallest number
        minIndex = i
        for j in range(i + 1, len(arr)):
            if arr[j] < arr[minIndex]:
                minIndex = j
        # When i is not the smallest number, swap i and the smallest number 
        if i != minIndex:
            arr[i], arr[minIndex] = arr[minIndex], arr[i]
    return arr

Go code implementation

func selectionSort(arr []int) []int {
        length := len(arr)
        for i := 0; i < length-1; i++ {
                min := i
                for j := i + 1; j < length; j++ {
                        if arr[min] > arr[j] {
                                min = j
                        }
                }
                arr[i], arr[min] = arr[min], arr[i]
        }
        return arr
}

Java code implementation

public class SelectionSort implements IArraySort {

    @Override
    public int[] sort(int[] sourceArray) throws Exception {
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        // a total of N-1 rounds of comparison 
        for ( int i = 0 ; i < arr.length - 1 ; i++) {
             int min = i;

            // The number of comparisons required per round Ni 
            for ( int j = i + 1 ; j < arr.length; j++) {
                 if (arr[j] < arr[min]) {
                     // Record the minimum element that can be found so far subscript of
                    min = j;
                }
            }

            // Swap the min value found with the value at position i 
            if (i != min) {
                 int tmp = arr[i];
                arr[i] = arr[min];
                arr[min] = tmp;
            }

        }
        return arr;
    }
}

PHP code implementation

function selectionSort($arr)
{
    $len = count($arr);
    for ($i = 0; $i < $len - 1; $i++) {
        $minIndex = $i;
        for ($j = $i + 1; $j < $len; $j++) {
            if ($arr[$j] < $arr[$minIndex]) {
                $minIndex = $j;
            }
        }
        $temp = $arr[$i];
        $arr[$i] = $arr[$minIndex];
        $arr[$minIndex] = $temp;
    }
    return $arr;
}

C language

void  swap ( int *a, int *b)  //Swap two variables
 {
     int temp = *a;
    *a = *b;
    *b = temp;
}
void selection_sort(int arr[], int len)
{
    int i,j;

        for (i = 0 ; i < len - 1 ; i++)
    {
                int min = i;
                 for (j = i + 1 ; j < len; j++)      // visit the unsorted elements 
                        if (arr[j] < arr[min])     // find the current minimum 
                                min = j;     // Record the minimum value 
                swap(&arr[min], &arr[i]);     //Do the swap
        }
}

C++ implementation

template < typename T> //Integer or floating point numbers can be used, if you want to use the object (class), you must set the operator function greater than (>) 
void  selection_sort ( std :: vector <T>& arr)  {
         for ( int i = 0 ; i < arr.size() - 1 ; i++) {
                 int min = i;
                 for ( int j = i + 1 ; j < arr.size(); j++)
                         if (arr[j] < arr [min])
                                min = j;
                std::swap(arr[i], arr[min]);
        }
}

Swift implementation

import Foundation
 /// Selection Sort 
/// 
/// - Parameter list: Array to be sorted 
func selectionSort(_ list : inout [Int]) -> Void {
     for j in 0. .< list .count - 1 {
         var minIndex = j
         for i in j..< list .count {
             if  list [minIndex] > list [i] {
                minIndex = i
            }
        }
        list .swapAt(j, minIndex)
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *