Selection Sort


DESCRIPTION

Selection Sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the unsorted portion of the list and moving it to the sorted portion of the list. This algorithm is straightforward and performs well on small datasets.

In Selection Sort algorithm:

  • Repeatedly select the smallest (or largest) element from the unsorted portion of the list.
  • Swap it with the first element of the unsorted part.
  • Continue this process for the remaining unsorted portion until the entire list is sorted.
Advantages Disadvantages
  • Simple and easy to understand.
  • Works well with small datasets.
  • Time complexity of O(n2) in the worst and average case.
  • Does not work well on large datasets.
  • Does not preserve the relative order of items with equal keys which means it is not stable.

COMPLEXITY

Worst Case O(n2)
Best Case O(n2)
Average Case O(n2)
Space Complexity O(1)

IMPLEMENTATIONS

void swap(int *xp, int *yp) {
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}

void selectionSort(int arr[], int n) {
    int i, j, min_idx;

    for (i = 0; i < n-1; i++) {
        min_idx = i;

        for (j = i+1; j < n; j++) {
          if (arr[j] < arr[min_idx])
            min_idx = j;

           if(min_idx != i)
            swap(&arr[min_idx], &arr[i]);
        }
    }
}
                        
void selectionSort(int arr[], int n) {
    int i, j, min_idx;

    for (i = 0; i < n - 1; i++) {

        min_idx = i;
        for (j = i + 1; j < n; j++) {
            if (arr[j] < arr[min_idx])
                min_idx = j;
        }

        if (min_idx != i)
            swap(arr[min_idx], arr[i]);
    }
}
                        
void selectionSort(int arr[]) {
    int n = arr.length;

    for (int i = 0; i < n-1; i++) {
        int min_idx = i;

        for (int j = i+1; j < n; j++) {
            if (arr[j] < arr[min_idx])
                min_idx = j;
        }

        int temp = arr[min_idx];
        arr[min_idx] = arr[i];
        arr[i] = temp;
    }
}
                        
static void sort(int []arr) {
    int n = arr.Length;

    for (int i = 0; i < n - 1; i++) {

        int min_idx = i;
        for (int j = i + 1; j < n; j++)
            if (arr[j] < arr[min_idx])
                min_idx = j;
        
        int temp = arr[min_idx];
        arr[min_idx] = arr[i];
        arr[i] = temp;
    }
}
                        
for i in range(len(A)-1):
    
    min_idx = i
    for j in range(i+1, len(A)):
        if A[min_idx] > A[j]:
            min_idx = j
                    
    A[i], A[min_idx] = A[min_idx], A[i]
                        
function selection_sort(&$arr, $n) {
    for($i = 0; $i < $n ; $i++) {
        $low = $i;

        for($j = $i + 1; $j < $n ; $j++) {
            if ($arr[$j] < $arr[$low]) {
                $low = $j;
            }
        }
        
        if ($arr[$i] > $arr[$low]) {
            $tmp = $arr[$i];
            $arr[$i] = $arr[$low];
            $arr[$low] = $tmp;
        }
    }
}