Q:

selection sort algorithm

// Por ter uma complexidade alta,
// não é recomendado para um conjunto de dados muito grande.
// Complexidade: O(n²) / O(n**2) / O(n^2)
// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

function selectionSort(vetor) {
    let minor;
    for (let i = 0; i < vetor.length - 1; i += 1) {
        minor = i;
        for (let j = i + 1; j < vetor.length; j += 1) {
            if (vetor[j] < vetor[minor]) {
                minor = j;
            }
        }
        if (i !== minor) {
            [vetor[i], vetor[minor]] = [vetor[minor], vetor[i]];
        }
    }
    return vetor;
}

selectionSort([1, 2, 5, 8, 3, 4]);
3
void selectionSort(int arr[], int n)  
{  
    int i, j, min_idx;  
  
    // One by one move boundary of unsorted subarray  
    for (i = 0; i < n-1; i++)  
    {  
        // Find the minimum element in unsorted array  
        min_idx = i;  
        for (j = i+1; j < n; j++)  
        if (arr[j] < arr[min_idx])  
            min_idx = j;  
  
        // Swap the found minimum element with the first element  
        swap(&arr[min_idx], &arr[i]);  
    }  
0

New to Communities?

Join the community