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
    🧩
    Data Structures & Algorithms
    CC-213
    Progress0 / 37 topics
    Topics
    1. Abstract Data Types2. Complexity Analysis3. Big Oh Notation4. Stacks (Linked Lists and Array Implementations)5. Recursion and analyzing recursive algorithms6. Divide and Conquer Algorithms7. Sorting Algorithms8. Selection Sort9. Insertion Sort10. Merge Sort11. Quick Sort12. Bubble Sort13. Heap Sort14. Shell Sort15. Radix Sort16. Bucket Sort17. Queue18. Dequeuer19. Priority Queues (linked and array implementations of queues)20. Linked List and Its Various Types21. Sorted Linked List22. Searching an Unsorted Array23. Binary Search for Sorted Arrays24. Hashing and Indexing25. Open Addressing and Chaining26. Trees and Tree Traversals27. Binary Search Trees28. Heaps29. M-Way Trees30. Balanced Trees31. Graphs32. Breadth-First Traversal33. Depth-First Traversal34. Topological Order35. Shortest Path36. Adjacency Matrix and Adjacency List Implementations37. Memory Management and Garbage Collection
    CC-213›Radix Sort
    Data Structures & AlgorithmsTopic 15 of 37

    Radix Sort

    5 minread
    772words
    Beginnerlevel

    Radix Sort

    Description: Radix sort is a non-comparative sorting algorithm that sorts numbers by processing individual digits. It groups numbers by each digit, starting from the least significant digit (LSD) to the most significant digit (MSD), or vice versa. Radix sort is particularly effective for sorting large sets of integers or strings.

    How It Works

    1. Determine Maximum Digits: Find the maximum number in the array to know how many digits need to be processed.
    2. Counting Sort by Digit: For each digit, use counting sort (or another stable sorting algorithm) to sort the array based on that digit.
      • Starting with the least significant digit, sort all numbers by that digit.
      • Move to the next significant digit and repeat until the most significant digit is processed.

    Time Complexity

    • Worst Case: O(nk)O(nk)O(nk) — where nnn is the number of elements and kkk is the number of digits in the largest number.
    • Average Case: O(nk)O(nk)O(nk)
    • Best Case: O(nk)O(nk)O(nk)

    Space Complexity

    • Space Complexity: O(n+k)O(n + k)O(n+k) — extra space is needed for the counting sort.

    Stability

    • Stability: Radix sort is a stable sorting algorithm, meaning it preserves the relative order of equal elements.

    Example Code

    Here's a C++ implementation of radix sort using counting sort as a subroutine:

    #include <iostream>
    #include <vector>
    using namespace std;
    
    // A function to do counting sort based on the digit represented by exp
    void countingSort(vector<int>& arr, int n, int exp) {
        vector<int> output(n); // output array
        int count[10] = {0}; // count array for digits (0-9)
    
        // Count occurrences of each digit in exp
        for (int i = 0; i < n; i++) {
            count[(arr[i] / exp) % 10]++;
        }
    
        // Update count[i] so that count[i] now contains the actual position of this digit in output[]
        for (int i = 1; i < 10; i++) {
            count[i] += count[i - 1];
        }
    
        // Build the output array
        for (int i = n - 1; i >= 0; i--) {
            output[count[(arr[i] / exp) % 10] - 1] = arr[i];
            count[(arr[i] / exp) % 10]--;
        }
    
        // Copy the output array to arr[], so that arr[] now contains sorted numbers according to the current digit
        for (int i = 0; i < n; i++) {
            arr[i] = output[i];
        }
    }
    
    // The main function to implement radix sort
    void radixSort(vector<int>& arr) {
        int n = arr.size();
        
        // Find the maximum number to know the number of digits
        int maxVal = *max_element(arr.begin(), arr.end());
    
        // Apply counting sort for each digit
        for (int exp = 1; maxVal / exp > 0; exp *= 10) {
            countingSort(arr, n, exp);
        }
    }
    
    int main() {
        vector<int> arr = {170, 45, 75, 90, 802, 24, 2, 66};
        radixSort(arr);
    
        cout << "Sorted array: ";
        for (int i : arr) {
            cout << i << " ";
        }
        cout << endl;
    
        return 0;
    }
    

    Explanation of the Code

    1. Function Definitions:

      • countingSort: A helper function that sorts the array based on the digit represented by exp.
      • radixSort: The main function that sorts the array using radix sort.
    2. Counting Sort Process:

      • The countingSort function counts how many times each digit appears and calculates their positions in the output array.
      • The output array is built by placing elements in their correct positions based on the current digit.
    3. Radix Sort Logic:

      • The radixSort function first finds the maximum value in the array to determine how many digits need sorting.
      • It then calls the counting sort for each digit, increasing the place value (units, tens, hundreds, etc.) with each iteration.
    4. Output: After sorting, the program prints the sorted array.

    Conclusion

    Radix sort is an efficient sorting algorithm for large datasets of integers or strings with a fixed number of digits. Its linear time complexity for certain datasets makes it faster than comparison-based algorithms for sorting numbers. Radix sort's stability also makes it suitable for applications where the relative order of equal elements must be preserved. However, it may require additional memory for counting and output arrays.

    Previous topic 14
    Shell Sort
    Next topic 16
    Bucket 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 count772
      Code examples0
      DifficultyBeginner