ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCC-213Selection Sort
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CC-213

    Data Structures & Algorithms

    Progress0 / 37 done
    Topics37
    1Abstract Data Types2Complexity Analysis3Big Oh Notation4Stacks (Linked Lists and Array Implementations)5Recursion and analyzing recursive algorithms6Divide and Conquer Algorithms7Sorting Algorithms8Selection Sort9Insertion Sort10Merge Sort11Quick Sort12Bubble Sort13Heap Sort14Shell Sort15Radix Sort16Bucket Sort17Queue18Dequeuer19Priority Queues (linked and array implementations of queues)20Linked List and Its Various Types21Sorted Linked List22Searching an Unsorted Array23Binary Search for Sorted Arrays24Hashing and Indexing25Open Addressing and Chaining26Trees and Tree Traversals27Binary Search Trees28Heaps29M-Way Trees30Balanced Trees31Graphs32Breadth-First Traversal33Depth-First Traversal34Topological Order35Shortest Path36Adjacency Matrix and Adjacency List Implementations37Memory Management and Garbage Collection
    3 min read
    Topic 8 of 37
    Topic Study Note Illustration
    08

    Selection Sort

    Description : Selection sort is a simple comparison-based sorting algorithm that works by dividing the input list into two parts: a sorted part and an unsorted part....

    Selection Sort

    Description: Selection sort is a simple comparison-based sorting algorithm that works by dividing the input list into two parts: a sorted part and an unsorted part. It repeatedly selects the smallest (or largest, depending on sorting order) element from the unsorted part and moves it to the end of the sorted part.

    How It Works

    1. Initialization: Start with the entire list as unsorted.
    2. Find Minimum: Look for the smallest element in the unsorted portion of the list.
    3. Swap: Swap this smallest element with the first element of the unsorted part.
    4. Move Boundary: The boundary between the sorted and unsorted parts is moved one element to the right.
    5. Repeat: Repeat the process until the entire list is sorted.

    Time Complexity

    • Worst Case: O(n2)O(n^2)O(n2) — occurs when the array is sorted in reverse order.
    • Average Case: O(n2)O(n^2)O(n2)
    • Best Case: O(n2)O(n^2)O(n2) — even if the array is already sorted, the algorithm still checks every element.

    Space Complexity

    • Space Complexity: O(1)O(1)O(1) — selection sort is an in-place sorting algorithm, meaning it requires a constant amount of additional storage space.

    Stability

    • Stability: Selection sort is not a stable sort. Equal elements may not maintain their relative order after sorting.

    Example Code

    Here's a C++ implementation of selection sort:

    #include <iostream>
    using namespace std;
    
    void selectionSort(int arr[], int n) {
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i; // Assume the minimum is the first element of the unsorted part
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j; // Update minIndex if a smaller element is found
                }
            }
            // Swap the found minimum element with the first element of the unsorted part
            swap(arr[minIndex], arr[i]);
        }
    }
    
    int main() {
        int arr[] = {64, 25, 12, 22, 11};
        int n = sizeof(arr) / sizeof(arr[0]);
        selectionSort(arr, n);
    
        cout << "Sorted array: ";
        for (int i = 0; i < n; i++) {
            cout << arr[i] << " ";
        }
        cout << endl;
    
        return 0;
    }
    

    Explanation of the Code

    1. Function Definition: The selectionSort function takes an array and its size as parameters.
    2. Outer Loop: Iterates through each element in the array up to the second-to-last element.
    3. Inner Loop: Searches for the smallest element in the unsorted portion of the array.
    4. Swap: Once the smallest element is found, it swaps this element with the current position in the outer loop.
    5. Output: After sorting, the program prints the sorted array.

    Conclusion

    Selection sort is easy to understand and implement, making it a great educational tool. However, its O(n2)O(n^2)O(n2) time complexity makes it inefficient for large datasets. For practical applications, more efficient algorithms like quick sort or merge sort are preferred.

    ← Previous topic 7
    Sorting Algorithms
    Next topic 9 →
    Insertion Sort

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    3mremaining
    543words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Data Structures & Algorithms