Back to Course

Algorithm

0% Complete
0/82 Steps
  1. Getting Started with Algorithm
    What is an Algorithm?
  2. Characteristics of Algorithm
    1 Topic
  3. Analysis Framework
  4. Performance Analysis
    3 Topics
  5. Mathematical Analysis
    2 Topics
  6. Sorting Algorithm
    Sorting Algorithm
    10 Topics
  7. Searching Algorithm
    6 Topics
  8. Fundamental of Data Structures
    Stacks
  9. Queues
  10. Graphs
  11. Trees
  12. Sets
  13. Dictionaries
  14. Divide and Conquer
    General Method
  15. Binary Search
  16. Recurrence Equation for Divide and Conquer
  17. Finding the Maximum and Minimum
  18. Merge Sort
  19. Quick Sort
  20. Stassen’s Matrix Multiplication
  21. Advantages and Disadvantages of Divide and Conquer
  22. Decrease and Conquer
    Insertion Sort
  23. Topological Sort
  24. Greedy Method
    General Method
  25. Coin Change Problem
  26. Knapsack Problem
  27. Job Sequencing with Deadlines
  28. Minimum Cost Spanning Trees
    2 Topics
  29. Single Source Shortest Paths
    1 Topic
  30. Optimal Tree Problem
    1 Topic
  31. Transform and Conquer Approach
    1 Topic
  32. Dynamic Programming
    General Method with Examples
  33. Multistage Graphs
  34. Transitive Closure
    1 Topic
  35. All Pairs Shortest Paths
    6 Topics
  36. Backtracking
    General Method
  37. N-Queens Problem
  38. Sum of Subsets problem
  39. Graph Coloring
  40. Hamiltonian Cycles
  41. Branch and Bound
    2 Topics
  42. 0/1 Knapsack problem
    2 Topics
  43. NP-Complete and NP-Hard Problems
    1 Topic
Lesson 6, Topic 1
In Progress

Selection Sort

Lesson Progress
0% Complete

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.

KodNest Selection sort 0 initial array

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.

KodNest Selection sort 0 comparision

3. After each iteration, minimum is placed in the front of the unsorted list.

KodNest Selection sort 0 swapping

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.

KodNest Selection sort 0
KodNest Selection sort 1
KodNest Selection sort 2
KodNest Selection sort 3 1

Selection Sort Algorithm

selectionSort(array, size)
  repeat (size - 1) times
  set the first unsorted element as the minimum
  for each of the unsorted elements
    if element < currentMinimum
      set element as new minimum
  swap minimum with first unsorted position
end selectionSort

Selection Sort Program in C

// Selection sort in C
    #include <stdio.h>
    void swap(int *a, int *b)
    {
      int temp = *a;
      *a = *b;
      *b = temp;
    }
    void selectionSort(int array[], int size)
    {
      for (int step = 0; step < size - 1; step++)
      {
        int min_idx = step;
        for (int i = step + 1; i < size; i++)
        {
          if (array[i] < array[min_idx])
            min_idx = i;
        }
        swap(&array[min_idx], &array[step]);
      }
    }
    void printArray(int array[], int size)
    {
      for (int i = 0; i < size; ++i)
      {
        printf("%d  ", array[i]);
      }
      printf("\n");
    }
    int main()
    {
      int data[] = {20, 12, 10, 15, 2};
      int size = sizeof(data) / sizeof(data[0]);
      selectionSort(data, size);
      printf("Sorted array in Acsending Order:\n");
      printArray(data, size);
    }

Complexity

CycleNumber of Comparison
1st(n-1)
2nd(n-2)
3rd(n-3)
last1

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)
KodNest Training New Batch is starting on 19th October 2020. Attend one week free demo classes.Register Now

New Report

Close