Selection sort algorithm

Selection Sort Algorithm

In this tutorial, you will learn how selection sort works. Also, you will find working examples of selection sort in C, C++, Java and Python.
Selection sort is an algorithm that selects the smallest element from an unsorted list in each iteration and places that element at the beginning of the unsorted list.

How Selection Sort Works?

  1. Set the first element as minimum.
    Selection Sort Steps
  2. Compare minimum with the second element. If the second element is smaller than minimum, assign second element as minimum.

    Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing. The process goes on until the last element.
    Selection Sort Steps
  3. After each iteration, minimum is placed in the front of the unsorted list.
    Selection Sort Steps
  4. For each iteration, indexing starts from the first unsorted element. Step 1 to 3 are repeated until all the elements are placed at their correct positions.
    Selection Sort StepsSelection sort steps Selection sort steps Selection sort steps  

Selection Sort Algorithm

  1. selectionSort(array, size)
  2. repeat (size - 1) times
  3. set the first unsorted element as the minimum
  4. for each of the unsorted elements
  5. if element < currentMinimum
  6. set element as new minimum
  7. swap minimum with first unsorted position
  8. end selectionSort

C++ Examples

  1. // Selection sort in C++
  2. #include <iostream>
  3. using namespace std;
  4. void swap(int *a, int *b)
  5. {
  6. int temp = *a;
  7. *a = *b;
  8. *b = temp;
  9. }
  10. void printArray(int array[], int size)
  11. {
  12. for (int i = 0; i < size; i++)
  13. {
  14. cout << array[i] << " ";
  15. }
  16. cout << endl;
  17. }
  18. void selectionSort(int array[], int size)
  19. {
  20. for (int step = 0; step < size - 1; step++)
  21. {
  22. int min_idx = step;
  23. for (int i = step + 1; i < size; i++)
  24. {
  25. if (array[i] < array[min_idx])
  26. min_idx = i;
  27. }
  28. swap(&array[min_idx], &array[step]);
  29. }
  30. }
  31. int main()
  32. {
  33. int data[] = {20, 12, 10, 15, 2};
  34. int size = sizeof(data) / sizeof(data[0]);
  35. selectionSort(data, size);
  36. cout << "Sorted array in Acsending Order:\n";
  37. printArray(data, size);
  38. }

 

Complexity

Cycle Number of Comparison
1st (n-1)
2nd (n-2)
3rd (n-3)
... ...
last 1
Number of comparisons:(n-1) + (n-2) + (n-3) +.....+ 1 = n(n-1)/2 nearly equals to n2

Complexity = O(n2)
Also, we can analyze the complexity by simply observing the number of loops. There are 2 loops so the complexity is n*n = n2.
Time Complexities:
  • Worst Case Complexity: O(n2)

    If we want to sort in ascending order and the array is in descending order then, the worst case occurs.
     
  • Best Case Complexity: O(n2)

    It occurs when the the array is already sorted
     
  • Average Case Complexity: O(n2)

    It occurs when the elements of the array are in jumbled order (neither ascending nor descending).
The time complexity of selection sort is the same in all cases. At every step, you have to find the minimum element and put it in the right place. The minimum element is not known until the end of the array is not reached.
Space Complexity:
Space complexity is O(1) because an extra variable temp is used.

Selection Sort Applications

The selection sort is used when:
  • small list is to be sorted
  • cost of swapping does not matter
  • checking of all the elements is compulsory
  • cost of writing to a memory matters like in flash memory (number of writes/swaps is O(n) as compared to O(n2) of bubble sort)

Comments

Popular posts from this blog

C Programming Tutorials

DataStructure and Algorithm

Java Programming Tutorial