quick sort
quick sort
QuickSort is a divide-and-conquer sorting algorithm which segregates an array Step 1: Partitioning the Array
into smaller subarrays and then recursively sorts them. Left Partition (<= 3): [-3, 2, -6, 1]
Steps involved in the algorithm : Right Partition (>= 3): [8, 5, 9, 6]
Choose a pivot element from the array. The pivot 3 is put in the correct sorted position.
Partition the array into two parts: one with elements less than or equal to the Step 2: Sorting Left Partition <= 3
pivot, and another with elements greater than the pivot. Array: [-3, 2, -6, 1]
Recursively apply QuickSort to both subarrays. Pivot: 1 (in red)
Combine the sorted subarrays with the pivot in the middle. Left Partition <= 1: [-3, -6]
Right Partition >= 1: [2]
The pivot 1 is at its correct position.
array [9, -3, 5, 2, 6, 8, -6, 1, 3] and the ideal pivot 5, the recursion tree has a depth of log𝑛, as the array size halves with
• In the best-case scenario, the pivot divides the array into two roughly equal halves at each step. For example, with the
each level. At every level, 𝑂(𝑛) comparisons are made to partition the array, resulting in a time complexity of 𝑂( 𝑛log𝑛 ).
• 2. Average Case:𝑂(𝑛log𝑛)
• In the average-case scenario, the pivot divides the array into reasonably balanced partitions. For example, with the array
comparisons are similar to the best-case scenario. This results in a time complexity of 𝑂( 𝑛log𝑛)
[9, -3, 5, 2, 6, 8, -6, 1, 3] and a randomly chosen pivot of -3, the depth of the recursion tree and the total number of
a straight line with 𝑛 levels. This results in a time complexity of 𝑂( 𝑛^2). For instance, with the input array [9, -3, 5, 2, 6, 8,
always the smallest or largest element, one partition contains all but one element. The recursion tree becomes essentially
-6, 1, 3] and a poorly chosen pivot of 9 (the largest element in this step), the total work is 𝑛× 𝑂( 𝑛)= 𝑂( 𝑛^2).
Bubble sort
Initial array:[5, 1, 4, 2, 8].
Bubble sort is a sorting algorithm that repeatedly compares adjacent elements in
a list and swaps them if they are in the wrong order. This process is repeated until Step 1:
the entire list is sorted. Compare 5 and 1. Swap them → [1, 5, 4, 2, 8]
Step-by-step Process: Start at the beginning of the list. Compare the first pair of Compare 5 and 4. Swap them → [1, 4, 5, 2, 8]
adjacent elements. If the first element is greater than the second, swap them;
Compare 5 and 2. Swap them → [1, 4, 2, 5, 8]
otherwise, leave them as they are.
Move to the next pair and repeat until the end of the list. Compare 5 and 8. No swap needed → [1, 4, 2, 5, 8]
After one pass, the largest element "bubbles up" to its correct position. At the end of Pass 1, the largest element (8) is in its correct position.
Repeat the process for the rest of the unsorted elements.
Step 2:
Compare 1 and 4. No swap → [1, 4, 2, 5, 8]
Compare 4 and 2. Swap them → [1, 2, 4, 5, 8]
Compare 4 and 5. No swap → [1, 2, 4, 5, 8]
Compare 5 and 8. No swap → [1, 2, 4, 5, 8]
At the end of Pass 2, the second largest element (5) is in its correct position.
Step 3:
Compare 1 and 2. No swap → [1, 2, 4, 5, 8]
Compare 2 and 4. No swap → [1, 2, 4, 5, 8]
Compare 4 and 5. No swap → [1, 2, 4, 5, 8]
At the end of Pass 3, the third largest element (4) is in its correct position.
Step 4:
Compare 1 and 2. No swap → [1, 2, 4, 5, 8]
Compare 2 and 4. No swap → [1, 2, 4, 5, 8]
At the end of Pass 4, the entire array is sorted: [1, 2, 4, 5, 8].
The time complexity of the Bubblesort
terminates after a single pass. This results in a time complexity of 𝑂( 𝑛), with 𝑛−1 comparisons and 0 swaps.
• In the best-case scenario, when the array is already sorted[1, 2, 3, 4, 5], no swaps are needed, and the algorithm
2. Average Case:𝑂(𝑛log𝑛)
required. This results in a time complexity of 𝑂(𝑛^2), with ( 𝑛*( 𝑛−1))/2 comparisons and fewer swaps compared to the
• In the average-case scenario, when the array is in random order[5, 1, 4, 2, 8], multiple passes and some swaps are
and swapped in each pass. This results in a time complexity of 𝑂( 𝑛^2), with ( 𝑛*( 𝑛−1))/2 comparisons and an equal
• In the worst-case scenario, when the array is sorted in reverse order[5, 4, 3, 2, 1], every element needs to be compared
number of swaps.
Quick sort vs Bubble sort
In this plot, I used 100 random lists to test the performance of Bubble Sort and
Quick Sort. The data was sourced from a CSV file to generate the plot. Regression
lines were added to illustrate the time complexity of each sorting algorithm.
Quick Sort:
The execution time increases slowly as input size n grows, reflecting its efficiency.
The theoretical trend line follows O(nlogn), which is what one would expect
for its time complexity.
The actual data points are very close to the trend line.
Bubble Sort:
The execution time increases rapidly with increasing input size n, showing its
inefficiency.
The trend line suggests a complexity of O(n^2) which agrees with its theoretical
performance.
Actual data points also follow this trend, especially for larger input sizes (right
side).
Conclusion:
This chart clearly shows that Quick Sort outperforms Bubble Sort, especially for
large input sizes. The slower growth of execution time for Quick Sort makes it a far
more efficient option for sorting tasks.
linear search , target k = 17
This requires checking all 𝑛 elements before finding the target or concluding it's not there. For example, if the target is 14
• In the worst-case scenario for a linear search, the target element is either at the very end of the array or not present at all.
and it's the last element, it takes 9 comparisons to find it. Thus, the time complexity is 𝑂( 𝑛).
Binary search , target k = 40
Binary Search is a far quicker search algorithm compared to the linear search, Initial Array: [3, 5, 9, 14, 39, 40, 52]
but it will only work on sorted arrays. It works by repeatedly dividing the search Step 1: Find Midpoint
interval in half. Calculate the middle index of the array: mid = (0 + 6) // 2 = 3
The midpoint is array[3] = 14. Mark 14 in green as the division
point.
Steps in Binary Search: Start with the middle element of the array. Decision: Compare the target T with 14.
If the middle element equals the target, return the index. If T < 14, search the left half: [3, 5, 9].
If the target is smaller than the middle element, search the left half. If T > 14, search the right half: [39, 40, 52].
If the target is larger than the middle element, then search the right half.
Repeat until the target is found or the interval is empty. Left Sub-array:[3, 5, 9]
Step 2: Find Midpoint
For [3, 5, 9], calculate the middle index: mid = (0 + 2) // 2 = 1.
The midpoint is array[1] = 5. Mark 5 in green.
Decision: Compare the target T with 5.
If T < 5, search the left half: [3].
If T > 5, search the right half: [9].
Linear Search does not require the data to be sorted. Thus, it is applied in
unsorted datasets where sorting is either too expensive or even
unnecessary.
Binary Search requires sorted data. Thus, an initial sorting costs O(nlogn),
but this is a one-time cost for static datasets. For dynamic datasets, i.e.,
frequent inserts/deletes, maintaining sorted order can be expensive.
3. Use Cases
Linear Search: Small datasets, data that is unsorted, or when the search has
to be simple and flexible.
• Time Comparison:
• size iterative_avg_time recursive_avg_time To find the critical t-value, consider the degrees of freedom (df), significance level (df), and
whether the test is one-tailed or two-tailed.
• 0 100 0.000572 0.000006
Given the sample size n=1000, the degrees of freedom df=999. From a 95% confidence level,
• 1 200 0.000108 0.001971 that is a two-tailed test, so α=0.05, with each tail α/2=0.025. The critical t-value for df=999 is
approximately ±1.962.
• 2 300 0.002220 0.002638 For a 99% confidence level, which is a two-tailed test, α = 0.01, so that with each tail α/2=
• 3 400 0.005238 0.003798 0.005. The critical t-value for df=999 is approximately ±2.626.
This t-value of 1.7056 is less than both critical values, indicating the result is not significant at
• 4 500 0.007817 0.007162 either confidence level.
• 5 600 0.011292 0.012066
• 6 700 0.016783 0.016400
• 7 800 0.021922 0.021855 Null Hypothesis (𝐻0): There is no significant difference in the time taken by iterative and
• 9 1000 0.036501 0.034268 Since 𝑝>0.05:We fail to reject the null hypothesis. This means there is not enough evidence to
conclude a significant difference in performance between iterative and recursive Bubble Sort
for this dataset.
• Paired t-Test Results:
• t-statistic: 1.1363323173766038
• p-value: 0.25856071602874336
Use seaborn and pandas to create a scatter plot and linear regression with your
measurements as data