ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Design and Analysis of Algorithms
    COMP3138
    Progress0 / 53 topics
    Topics
    1. Introduction to Algorithm Design2. Data Structures3. Efficiency in Algorithms4. Analysis of Algorithms5. Mathematical Review6. Mathematical Analysis of Algorithms7. Types of Functions8. Order of Growth9. Asymptotic Notations10. Sorting Algorithms11. Selection Sort Algorithm12. Example and Analysis of Selection Sort13. Insertion Sort Algorithm14. Divide and Conquer Algorithms15. Merge Sort Algorithm16. Quick Sort Algorithm17. Bucket Sort Algorithm18. Radix Sort Algorithm19. Counting Sort Algorithm20. Heap Sort Basics21. Heap Algorithms22. Heap Properties and Examples23. Heap Operations24. Heap Sort Algorithm Analysis25. Heap Insertion and Deletion26. Tree Based Algorithms and Hashing27. Red Black Tree Basics28. Binary Search Tree Basics29. Tree Searching Algorithms30. Analysis of Tree Searching Algorithms31. Analysis of Insertion and Deletion in BST32. Hashing Basics33. Examples of Hash Functions34. Analysis of Collision Resolution Techniques35. Dynamic Programming36. 0-1 Knapsack Problem37. Fractional Knapsack Problem38. Longest Common Subsequence39. Shortest Path Finding40. Matrix Chain Multiplication41. Assembly Line Chain Problem42. Greedy Algorithms43. Prim's Algorithm44. Kruskal's Algorithm45. Dijkstra's Algorithm46. Huffman Coding47. NP-Completeness48. Polynomial Time Verification49. Reducibility50. NP-Completeness Proofs51. Randomized Algorithms52. Particle Swarm Optimization53. Genetic Algorithms
    COMP3138›Selection Sort Algorithm
    Design and Analysis of AlgorithmsTopic 11 of 53

    Selection Sort Algorithm

    5 minread
    804words
    Beginnerlevel

    Selection Sort Algorithm

    Selection Sort is a simple comparison-based sorting algorithm. It works by repeatedly selecting the smallest (or largest, depending on the order) element from the unsorted part of the array and placing it in its correct position in the sorted part of the array. The algorithm performs in-place sorting, meaning it doesn't require additional memory outside of the input array.

    How Selection Sort Works

    The basic idea of Selection Sort is to divide the array into two parts:

    1. The sorted portion at the beginning of the array.
    2. The unsorted portion that remains to be sorted.

    In each iteration, Selection Sort finds the smallest (or largest) element from the unsorted portion of the array and swaps it with the first unsorted element, effectively growing the sorted portion.

    Steps of the Algorithm:

    1. Start with the first element of the array as the current position of the sorted portion.
    2. Search for the smallest (or largest) element in the unsorted portion of the array.
    3. Swap the smallest (or largest) element found with the element at the current position.
    4. Move the boundary between the sorted and unsorted portions one step forward (i.e., move to the next element).
    5. Repeat steps 2 to 4 until all elements are sorted.

    Pseudocode:

    for i = 0 to n-1
        min_index = i
        for j = i+1 to n
            if array[j] < array[min_index]
                min_index = j
        swap array[i] with array[min_index]
    
    • Outer loop: It runs from 0 to n-1, where n is the size of the array. This loop controls the boundary of the sorted portion of the array.
    • Inner loop: It runs from i+1 to n to find the smallest element in the unsorted portion.
    • Swap: The smallest element found is swapped with the first unsorted element (at index i).

    Example:

    Let's walk through an example to better understand how Selection Sort works.

    Suppose we have an array:
    [64, 25, 12, 22, 11]

    First Pass:

    • The unsorted portion is [64, 25, 12, 22, 11].

    • The smallest element in the unsorted portion is 11 (index 4).

    • Swap 11 with the first element (index 0), so the array becomes:

      [11, 25, 12, 22, 64]

    Second Pass:

    • The unsorted portion is [25, 12, 22, 64].

    • The smallest element is 12 (index 2).

    • Swap 12 with the second element (index 1), so the array becomes:

      [11, 12, 25, 22, 64]

    Third Pass:

    • The unsorted portion is [25, 22, 64].

    • The smallest element is 22 (index 3).

    • Swap 22 with the third element (index 2), so the array becomes:

      [11, 12, 22, 25, 64]

    Fourth Pass:

    • The unsorted portion is [25, 64].

    • The smallest element is 25 (index 3).

    • Swap 25 with the fourth element (index 3), no change in the array.

      [11, 12, 22, 25, 64]

    Fifth Pass:

    • The unsorted portion is [64], and it's already sorted.

    The array is now fully sorted:
    [11, 12, 22, 25, 64]

    Time Complexity:

    • Best Case: O(n²)
    • Worst Case: O(n²)
    • Average Case: O(n²)

    Explanation:

    • The algorithm always requires two nested loops: the outer loop runs n times, and the inner loop runs n-1, n-2, ..., 1 times. This results in a time complexity of O(n²), regardless of whether the array is already sorted or not.
    • Selection Sort is not adaptive, meaning it doesn't take advantage of already sorted portions of the array.

    Space Complexity:

    • Space Complexity: O(1)

    Explanation:

    • Selection Sort is an in-place sorting algorithm, meaning it does not require any extra space other than a few variables to keep track of indices and the current element. It sorts the array in-place with constant additional space.

    Advantages of Selection Sort:

    • Simplicity: Selection Sort is very easy to understand and implement.
    • In-place Sorting: It does not require additional memory, which makes it space-efficient.
    • No extra swaps: The algorithm only performs one swap per iteration, so it is efficient in terms of swap operations.

    Disadvantages of Selection Sort:

    • Inefficient for large datasets: The algorithm has a time complexity of O(n²), making it inefficient for sorting large arrays compared to more advanced algorithms like Merge Sort or Quick Sort.
    • Not adaptive: Selection Sort does not adapt to the input data and will always run in O(n²) time regardless of the initial order of the array.
    • Not stable: It is not a stable sorting algorithm, meaning that equal elements may not retain their relative order after sorting.

    Conclusion:

    Selection Sort is a very simple but inefficient algorithm, particularly for large arrays. It is more suitable for small datasets or as a teaching tool to understand basic sorting principles. For practical use on large datasets, more efficient algorithms like Quick Sort, Merge Sort, or Heap Sort are preferred.

    Previous topic 10
    Sorting Algorithms
    Next topic 12
    Example and Analysis of Selection Sort

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time5 min
      Word count804
      Code examples0
      DifficultyBeginner