Podcast
Questions and Answers
What is the primary reason that Quicksort is often preferred over Mergesort in practice, despite having a potentially worse worst-case time complexity?
What is the primary reason that Quicksort is often preferred over Mergesort in practice, despite having a potentially worse worst-case time complexity?
- Quicksort can be more easily parallelized than Mergesort, allowing it to take better advantage of multi-core processors.
- Quicksort generally involves less data movement, leading to faster performance in practice, despite a higher number of comparisons. (correct)
- Quicksort is a stable sorting algorithm, preserving the relative order of equal elements, which is crucial for certain applications.
- Quicksort always requires less memory due to its in-place sorting nature, unlike Mergesort which needs auxiliary space.
In the context of optimizing Mergesort, which of the following strategies aims to reduce the overhead associated with small subarrays?
In the context of optimizing Mergesort, which of the following strategies aims to reduce the overhead associated with small subarrays?
- Applying a randomized approach to pivot selection, aiming to achieve more balanced partitions.
- Using an in-place merge algorithm to eliminate the need for an auxiliary array.
- Switching to insertion sort for subarrays smaller than a certain size to leverage its efficiency on nearly sorted data. (correct)
- Implementing a dual-pivot partitioning scheme to divide the array into more subproblems.
How does the '3-way partitioning' scheme in Quicksort address the performance issues caused by arrays with many duplicate keys?
How does the '3-way partitioning' scheme in Quicksort address the performance issues caused by arrays with many duplicate keys?
- It balances the partition by equally distributing smaller and larger elements of pivot, avoiding worst case scenarios.
- It reduces the number of recursive calls by ensuring that elements equal to the pivot are not included in the subarrays to be sorted.
- It partitions the array into three segments: elements less than, equal to, and greater than the pivot, thus placing all equal elements in their final sorted position. (correct)
- It increases cache locality by grouping identical elements together, improving memory access times.
How does the bottom-up merge sort approach differ fundamentally from the top-down approach?
How does the bottom-up merge sort approach differ fundamentally from the top-down approach?
What is the primary benefit of using dual-pivot or multi-pivot Quicksort algorithms?
What is the primary benefit of using dual-pivot or multi-pivot Quicksort algorithms?
In the context of merge sort, what role does the auxiliary array play, and how does it impact the algorithm's space complexity?
In the context of merge sort, what role does the auxiliary array play, and how does it impact the algorithm's space complexity?
Consider a scenario where you need to sort an array of objects based on multiple criteria, each with its own custom ordering. Which approach would be most suitable?
Consider a scenario where you need to sort an array of objects based on multiple criteria, each with its own custom ordering. Which approach would be most suitable?
Which of the following is a characteristic of Timsort?
Which of the following is a characteristic of Timsort?
In the context of Quicksort, what is the purpose of shuffling the array before sorting, and what problem does it address?
In the context of Quicksort, what is the purpose of shuffling the array before sorting, and what problem does it address?
Given that Quicksort is known for its efficiency, under what condition would its performance degrade to $O(n^2)$ time complexity?
Given that Quicksort is known for its efficiency, under what condition would its performance degrade to $O(n^2)$ time complexity?
What is the key advantage of merge sort over other sorting algorithms in terms of performance guarantees.
What is the key advantage of merge sort over other sorting algorithms in terms of performance guarantees.
Why is quicksort considered an unstable sorting algorithm?
Why is quicksort considered an unstable sorting algorithm?
How does increasing cache locality improve the performance of quicksort?
How does increasing cache locality improve the performance of quicksort?
What is the primary difference in how quicksort and mergesort utilize memory?
What is the primary difference in how quicksort and mergesort utilize memory?
How does the choice of pivot affect quicksort's performance on an unsorted array, and which pivot selection strategy is least likely to yield $O(n^2)$ performance?
How does the choice of pivot affect quicksort's performance on an unsorted array, and which pivot selection strategy is least likely to yield $O(n^2)$ performance?
What characteristic makes quicksort not well-suited for sorting small arrays or sub-arrays?
What characteristic makes quicksort not well-suited for sorting small arrays or sub-arrays?
What is a primary advantage of using randomized quicksort compared to deterministic quicksort?
What is a primary advantage of using randomized quicksort compared to deterministic quicksort?
Which statement is true regarding cache improvements?
Which statement is true regarding cache improvements?
What happens if a 'stop at equal keys' implementation is not used in quicksort?
What happens if a 'stop at equal keys' implementation is not used in quicksort?
Flashcards
Merge sort
Merge sort
A sorting algorithm that divides the array into two halves, sorts each recursively, and merges the arrays.
Top-down merge sort
Top-down merge sort
A variation of merge sort that recursively divides the array into smaller halves until single elements are reached, then merges them back in sorted order.
Bottom-up merge sort
Bottom-up merge sort
A method to merge sort that iteratively merges subarrays of increasing sizes until the entire array is sorted.
Auxiliary array (merge sort)
Auxiliary array (merge sort)
Signup and view all the flashcards
Timsort
Timsort
Signup and view all the flashcards
Quicksort
Quicksort
Signup and view all the flashcards
Quicksort
Quicksort
Signup and view all the flashcards
Pivot (Quicksort)
Pivot (Quicksort)
Signup and view all the flashcards
Partitioning
Partitioning
Signup and view all the flashcards
In-place partitioning
In-place partitioning
Signup and view all the flashcards
Median-of-three
Median-of-three
Signup and view all the flashcards
Quicksort (Stop at equal keys)
Quicksort (Stop at equal keys)
Signup and view all the flashcards
3-way partitioning
3-way partitioning
Signup and view all the flashcards
Dutch National Flag Problem
Dutch National Flag Problem
Signup and view all the flashcards
3-way Partitioning Goal
3-way Partitioning Goal
Signup and view all the flashcards
2-pivot Quicksort
2-pivot Quicksort
Signup and view all the flashcards
3-pivot Quicksort
3-pivot Quicksort
Signup and view all the flashcards
Temporal Locality
Temporal Locality
Signup and view all the flashcards
Quicksort cache improvements
Quicksort cache improvements
Signup and view all the flashcards
Java sorting methods
Java sorting methods
Signup and view all the flashcards
Study Notes
- Merge sort and Quick sort are discussed.
- Merge Sort is covered before Quick Sort.
Merge Sort
- A recursive sorting algorithm using the "divide and conquer" approach.
- Top-down merge sort is recursive
- Divides the array into two halves.
- Sorts each array recursively.
- Merges the arrays.
Bottom Up Merge Sort
- Iterative.
- Iterates through the array.
- Merges subarrays of size 1, then size 2, 4, 8, and so on.
- Merging requires an auxiliary array for extra space.
Top Down Merge Sort Java Implementation
- The methods needed are:
- A public method that passes in the array to be sorted
- A public static void sort (Comparable [] a)
- A recursive method that has passes of the original and auxiliary arrays, and indices of the subarray to be sorted
- private static void sort (Comparable [] a, Comparable [] aux, int lo, int hi)
- A merge method, to merge sorted subarrays, with the 2 arrays to be merged, lowest, highest and midpoint indices
- private static void merge (Comparable [] a, Comparable [] aux, int lo, int mid, int hi)
- An auxiliary array of the same size of the original array is created
- Recursion kicks off by passing in 0 and array length-1 as indices (ie the full original array)
- Recursion is repeated untill lo and hi are equal to get to array of length 1
Recursive Method
- Note: mid = lo+ (hi-lo)/2 avoids integer overflow.
- Merge method involves copying the original array into auxiliary array, then merging elements back into the original one in sorted order
Merge Sort Running Time
- A laptop executes 10^8 compares/second
- A supercomputer executes 10^12 compares/second
- Number of compares is less than N lg N.
- It is Linearithmic.
- Applies to both average and worst cases.
- Is Stable – the "less than" operator favours the left hand value to right hand one even when they're equal.
- Number of array accesses is less than 6 N lg N.
- Memory use relies on an auxiliary array of size N.
Merge Sort Improvements
- Cut off to insertion sort is used for ~10 items to avoid too much overhead for small subarrays
Merge Sort Further Improvements
- Improve if largest item in first half is smaller than smallest in second half to stop if already sorted.
- Switch the roles of aux[] and a[]
- Eliminates the time taken to copy to the auxiliary array used for merging, but does not affect the space used.
- Uses 2 invocations of the sort method.
- Takes input from the given array and puts the sorted output in the auxiliary array
- Takes its input from the auxiliary array and puts the sorted output in the given array.
Merge Sort - Bottom Up
- Passes through array merging subarrays of size 1, 2, 4, etc
- Adaptive sort, combination of
- Natural merge sort: exploits pre-existing order by identifying naturally occurring non-descending sequences (so ascending or equal) and searches for at least 2 elements
- insertion sort is used to make initial runs
- Timsort is used in Java 7 onwards (for non-primitive data types), Python, and Android
Quick Sort
- One of top 10 algorithms of 20th century in science and engineering.
- Invented by Tony Hoare in 1959
- Steps
- Shuffle the array
- Partition the array
- S − {v} is partitioned into two disjoint subsets: S1 = {all values x ≤ v} and S2 = {all values x ≥ v}
- S1 is the left side of the array
- S2 is the right side of the array
- Sort each subarray recursively
Quick Sort Details
- Implement partitioning recursively
- Pick a pivot
- Want a value that will cause |S1| and |S2| to be non-zero, and close to equal in size if possible
- Deal with cases where the element equals the pivot
Quick Sort - Picking a Pivot
- Ideally it would be the median value
- Pivot options:
- Pick the first element, which is the simplest
- Ok if array shuffled, bad if array sorted (worst case for quicksort)
- Choose pivot randomly, which uses a random number generator
- Approximate: choose a median of first, middle and last value, which is more expensive, calculating median
Quick Sort - Partitioning
- It is necessary to partition the array into left and right sub-arrays
- The elements in left sub-array are less than or equal to the pivot
- The elements in right sub-array are greater than or equal to the pivot
- Elements get to the correct partition by choosing an element from the array as the pivot and making one pass through the rest of the array and swap as needed to put elements in partitions.
Quick Sort Recursive Code
- Uses random numbers to decide what to do next somewhere in its logic
Quick Sort Performance
- Make sure to shuffle the array at the start or pick a random pivot in each subarray to avoid worst case performance
- Home PC executes 10^8 compares/second, whilst a supercomputer executes 10^12 compares/second.
- Expected number of compares in average case is ~ 1.39 n lg n, which is 39% more compares than mergesort, but is faster than mergesort in practice because of less data movement.
- Properties Summary
- Not stable because of long distance swapping.
- No iterative version (without using a stack).
- Pure quicksort is not good for small arrays.
- "In-place", it uses auxiliary storage because of recursive call (O(logn) space).
- Time complexity is O(n log n) average case performance, but O(n²) worst case performance.
- Improvements
- Use insertion sort for small arrays Cut off to insertion sort at subarray size ~10
- Use median for pivot value (median of 3 random items, ie first, last, middle)
- 3-way quicksort, dual pivot, 3-pivot
- Stop at equal keys to avoid problem if all items equal to pivot are moved to one side, which causes the consequence of ~1/2 n^2 compares when all keys are equal
- Stop scanning if keys are equal. This results in the keys dividing the array exactly
- A 3-way partitioning can be used
Three Way Partitioning
- Partition array into three parts so that:
- Entries in between lt and gt equal to the partition item.
- No larger entries to the left of lt.
- No smaller entries to the right of gt.
- Lets you stop scanning if keys are equal
- Is a Dutch national flag problem using Edsger Dijkstra's solution
2 Pivot Quick Sort
- Partitions with Keys less than p1, Keys between p1 and p2, and Keys greater than p2.
- Recursively sort three subarrays
3 Pivot Quick Sort
- Has keys grouped into less than p1, between p1 and p2, between p2 and p3 and greater than p3
Quick Sort - Cache Improvements
- The Principle of locality:
- the same values, or related storage locations, are frequently accessed
- Temporal locality
- If at one point a particular memory location is referenced, then it is likely that the same location will be referenced again in the near future
- Spatial locality
- If a particular storage location is referenced at a particular time, then it is likely that nearby memory locations will be referenced in the near future -> pre-fetch arrays
- Implemented through caching to store data "nearer" to processor, 2-pivot and 3-pivot have smaller number of cache misses and recursive calls
Merge Vs Quick
- In Java, Arrays.sort() uses QuickSort for sorting primitives and MergeSort for sorting Arrays of Objects.
- This is because merge sort is stable, so it won't reorder elements that are equal.
- QuickSort in java is 2-pivot since 2009
- MergeSort in java is Timsort
Sort Algorithms Summary
- System sort - Arrays.sort(); is usually good enough
- Compare performance to system sort in assignment
Comparator Interface
- Has a Comparable interface
- Can override compareTo() method
- Uses multiple classes implementing Comparator and override compare.
- Allows users to create custom orders in system sorts (pass as a second argument to Array.sort(a, new MyCustomOrder());)
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.