How to figure out progress while sorting?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Sorting is a fundamental operation in computer science that involves arranging elements in a particular order. Often, it's important to monitor the progress of a sorting algorithm to optimize performance, estimate completion time, or manage user expectations. This article delves into how to gauge "progress" during sorting, utilizing technical explanations, examples, and a concise summary table for clarity.
Understanding the Progress in Sorting
Progress in sorting algorithms can be understood as a measure of how close the algorithm is to completing its task. However, assessing this requires insight into the internal workings of the sorting process. Here we explore three classic sorting algorithms—Bubble Sort, Quick Sort, and Merge Sort—demonstrating how progress can be measured.
Bubble Sort
Bubble Sort is a simple comparison-based sorting algorithm. While not efficient for large datasets, it provides an intuitive understanding of sorting progress.
- Mechanics of Bubble Sort:
- Repeatedly compares adjacent pairs and swaps them if they are in the wrong order.
- Makes several passes through the list until no swaps are needed.
- Measuring Progress:
- Progress can be measured by tracking the number of passes. Each pass guarantees that at least one element (the largest remaining unsorted element) is placed correctly.
- Completed passes are directly proportional to progress: for an array of size `n`, `i/n` passes indicates an approximate progress of `(i/n) * 100%`.
Quick Sort
Quick Sort is a highly efficient divide-and-conquer algorithm, with its progress more complex to measure due to its recursive nature.
- Mechanics of Quick Sort:
- Divides the array into two sub-arrays around a pivot and recursively sorts the sub-arrays.
- Measuring Progress:
- Utilize the "depth" of recursion as an indicator; deeper recursion levels indicate finer divisions and progress.
- Track the number of elements sorted in each recursive call. An indirect measure can be the ratio of sorted elements to the total number of elements.
Merge Sort
Merge Sort also uses a divide-and-conquer approach and is stable with a consistent time complexity of .
- Mechanics of Merge Sort:
- Recursively divides the list into halves until single-element lists are reached and then merges the lists in sorted order.
- Measuring Progress:
- Similar to Quick Sort, analyze the depth of recursion.
- Track the completion of merge operations, which combines ordered sublists into a single ordered list.
Techniques to Monitor Sorting Progress
- Visual Indicators: Use graphical or console-based progress bars to indicate the percentage completion of the sort.
- Time-based Estimates: Estimate the completion time based on initial progress rates. Suitable when the sorting algorithm demonstrates consistent behavior.
- Instrumentation: Insert logging or counters within the sorting code to repeatedly assess and record progress state at intervals or milestones.
Practical Examples and Code Snippets
Bubble Sort Example:
Related reading
- How to fill a 2D array diagonally based on coordinates
- How to find 3 numbers in increasing order and increasing indices in an array in linear time
- How to find a checksum of the same checksum? job-interview question
- How to find a duplicate element in an array of shuffled consecutive integers?
- How to find a triangle inside a graph?
- How to find all combinations of coins when given some dollar value
- How to find all grid squares on a line?
- How to find all occurrences of a substring?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.