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›Sorting Algorithms
    Design and Analysis of AlgorithmsTopic 10 of 53

    Sorting Algorithms

    8 minread
    1,359words
    Intermediatelevel

    Sorting Algorithms

    Sorting is one of the most fundamental operations in computer science. It involves rearranging the elements of a collection (such as an array or list) into a specific order — typically in ascending or descending order. Sorting is widely used in many applications, such as organizing data, searching, and optimizing other algorithms. There are several sorting algorithms, each with its own strengths and weaknesses depending on the context.

    Below are the most common sorting algorithms, categorized by their performance characteristics.


    1. Bubble Sort

    Bubble Sort is one of the simplest sorting algorithms, but it is also one of the most inefficient for large datasets. It repeatedly steps through the list, compares adjacent items, and swaps them if they are in the wrong order. The algorithm "bubbles" the largest unsorted element to its correct position in each pass.

    Algorithm Steps:

    1. Compare each pair of adjacent elements.
    2. If the pair is in the wrong order, swap them.
    3. Continue this process for every element in the array, repeating the entire process for the remaining unsorted portion of the array.

    Time Complexity:

    • Best Case: O(n) (when the array is already sorted)
    • Worst Case: O(n²) (when the array is sorted in reverse order)
    • Average Case: O(n²)

    Space Complexity: O(1) (in-place sorting)

    Advantages:

    • Simple to implement.
    • In-place algorithm (no extra memory required).

    Disadvantages:

    • Very inefficient for large datasets.
    • Requires multiple passes over the data, resulting in poor performance.

    2. Selection Sort

    Selection Sort is a simple algorithm that works by repeatedly selecting the minimum (or maximum) element from the unsorted portion of the array and swapping it with the first unsorted element. This process is repeated for each element until the entire array is sorted.

    Algorithm Steps:

    1. Find the smallest (or largest) element in the unsorted portion of the array.
    2. Swap it with the first unsorted element.
    3. Move the boundary between the sorted and unsorted portions of the array.
    4. Repeat the process for the remaining unsorted portion.

    Time Complexity:

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

    Space Complexity: O(1) (in-place sorting)

    Advantages:

    • Simple to implement.
    • Does not require additional memory (in-place).
    • Performs well for small datasets.

    Disadvantages:

    • Inefficient for large datasets (O(n²) time complexity).
    • Not adaptive, meaning it doesn't take advantage of partially sorted arrays.

    3. Insertion Sort

    Insertion Sort works similarly to how people sort playing cards. The algorithm takes one element at a time from the unsorted portion and inserts it into its correct position within the sorted portion. It repeatedly "inserts" the current element into the already sorted part of the array.

    Algorithm Steps:

    1. Start from the second element and compare it with the previous element.
    2. If the current element is smaller, move the previous element to the right and continue comparing.
    3. Insert the current element in the correct position.
    4. Repeat this for each element in the array.

    Time Complexity:

    • Best Case: O(n) (when the array is already sorted)
    • Worst Case: O(n²) (when the array is sorted in reverse order)
    • Average Case: O(n²)

    Space Complexity: O(1) (in-place sorting)

    Advantages:

    • Simple to implement.
    • Efficient for small datasets or partially sorted arrays.
    • Stable sorting algorithm (does not change the relative order of equal elements).

    Disadvantages:

    • Inefficient for large datasets (O(n²) time complexity).
    • Performs poorly on large or random datasets.

    4. Merge Sort

    Merge Sort is a divide-and-conquer algorithm that splits the array into two halves, recursively sorts each half, and then merges the sorted halves back together. Merge Sort has a guaranteed time complexity of O(n log n), making it much more efficient than the previous algorithms for large datasets.

    Algorithm Steps:

    1. Divide the array into two halves.
    2. Recursively sort both halves.
    3. Merge the sorted halves into a single sorted array.

    Time Complexity:

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

    Space Complexity: O(n) (requires extra space for merging)

    Advantages:

    • Efficient, with a time complexity of O(n log n).
    • Stable sorting algorithm.
    • Works well for large datasets.

    Disadvantages:

    • Requires additional memory (not an in-place algorithm).
    • Slower in practice compared to some other O(n log n) algorithms like Quick Sort.

    5. Quick Sort

    Quick Sort is another divide-and-conquer algorithm. It selects a "pivot" element and partitions the array around the pivot, placing smaller elements to the left and larger elements to the right. The subarrays are then recursively sorted. Quick Sort is often faster in practice than Merge Sort, even though both have the same average time complexity of O(n log n).

    Algorithm Steps:

    1. Select a pivot element.
    2. Partition the array into two subarrays: one with elements smaller than the pivot, and one with elements greater than the pivot.
    3. Recursively sort both subarrays.

    Time Complexity:

    • Best Case: O(n log n) (balanced partitioning)
    • Worst Case: O(n²) (when the pivot is the smallest or largest element)
    • Average Case: O(n log n)

    Space Complexity: O(log n) (in-place sorting, but requires space for recursion stack)

    Advantages:

    • Generally faster in practice than Merge Sort due to smaller constant factors.
    • In-place sorting (does not require additional memory for arrays).
    • Efficient for large datasets.

    Disadvantages:

    • Worst-case time complexity is O(n²) (though this can be avoided with good pivot selection or randomized Quick Sort).
    • Not a stable sorting algorithm.

    6. Heap Sort

    Heap Sort is a comparison-based sorting algorithm that works by first building a max-heap (or min-heap) and then repeatedly removing the largest (or smallest) element to build the sorted array. It is an in-place sorting algorithm with O(n log n) time complexity.

    Algorithm Steps:

    1. Build a max-heap from the input data.
    2. The largest element is at the root of the heap. Swap it with the last element of the array.
    3. Reduce the heap size and restore the heap property by "heapifying" the root.
    4. Repeat the process until the heap size is reduced to 1.

    Time Complexity:

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

    Space Complexity: O(1) (in-place sorting)

    Advantages:

    • Time complexity is guaranteed to be O(n log n).
    • In-place sorting (no additional memory required).
    • Not recursive, so it avoids stack overflow issues.

    Disadvantages:

    • Not stable.
    • The constant factors in Heap Sort tend to make it slower than Quick Sort and Merge Sort in practice.

    7. Radix Sort

    Radix Sort is a non-comparative integer sorting algorithm. It works by sorting numbers digit by digit, starting from the least significant digit (LSD) or the most significant digit (MSD), depending on the implementation. Radix Sort can be faster than comparison-based algorithms when the input is large and consists of integers with a relatively small range of values.

    Algorithm Steps:

    1. Sort the elements by the least significant digit using a stable sorting algorithm like Counting Sort.
    2. Move to the next digit and sort again.
    3. Repeat the process until all digits have been processed.

    Time Complexity:

    • Best Case: O(nk) (where k is the number of digits or bits in the maximum number)
    • Worst Case: O(nk)
    • Average Case: O(nk)

    Space Complexity: O(n)

    Advantages:

    • Can be faster than comparison-based algorithms like Quick Sort and Merge Sort when dealing with large datasets with smaller ranges.
    • Efficient for sorting large numbers of integers.

    Disadvantages:

    • Limited to sorting integers or fixed-length strings.
    • Not a comparison-based algorithm, so it may not work well in all situations.
    • The time complexity depends on the number of digits (or the range of the numbers).

    8. Bucket Sort

    Bucket Sort is a non-comparative sorting algorithm that works by dividing the elements into several "buckets," sorting each bucket individually (using another sorting algorithm), and then concatenating the results. It is particularly effective when the input is uniformly distributed over a range.

    Algorithm Steps:

    1. Divide the input array into k equal-sized buckets.
    2. Sort each bucket individually (usually using a different sorting algorithm like Insertion Sort).
    3. Concatenate the sorted buckets into the final sorted array.

    Time Complexity:

    • Best Case: O(n + k)
    • Worst Case: O(n²) (if the buckets
    Previous topic 9
    Asymptotic Notations
    Next topic 11
    Selection Sort Algorithm

    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 time8 min
      Word count1,359
      Code examples0
      DifficultyIntermediate