Merge Sort Algorithm

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

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?

  • 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?

  • 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?

  • 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?

<p>Bottom-up merge sort starts by merging small subarrays and iteratively increases their size, while top-down merge sort recursively divides the array. (C)</p> Signup and view all the answers

What is the primary benefit of using dual-pivot or multi-pivot Quicksort algorithms?

<p>They improve cache utilization and reduce the number of recursive calls, leading to better average-case performance. (B)</p> Signup and view all the answers

In the context of merge sort, what role does the auxiliary array play, and how does it impact the algorithm's space complexity?

<p>It serves as a temporary storage space during the merging process, requiring additional memory proportional to the input size, resulting in a space complexity of $O(n)$. (A)</p> Signup and view all the answers

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?

<p>Implementing multiple classes that implement the <code>Comparator</code> interface, each providing a different custom <code>compare()</code> method for each criterion and using system sort. (B)</p> Signup and view all the answers

Which of the following is a characteristic of Timsort?

<p>It is an adaptive sorting algorithm that exploits pre-existing order in real-world data. (C)</p> Signup and view all the answers

In the context of Quicksort, what is the purpose of shuffling the array before sorting, and what problem does it address?

<p>Shuffling is done to avoid the worst cases, caused by sorted arrays, by randomizing the input. (C)</p> Signup and view all the answers

Given that Quicksort is known for its efficiency, under what condition would its performance degrade to $O(n^2)$ time complexity?

<p>When the partitioning step consistently results in one subarray being much larger than the other due to a poor pivot selection. (D)</p> Signup and view all the answers

What is the key advantage of merge sort over other sorting algorithms in terms of performance guarantees.

<p>Merge sort has a guaranteed $O(n log n)$ time complexity regardless of the input data's initial order. (A)</p> Signup and view all the answers

Why is quicksort considered an unstable sorting algorithm?

<p>Because elements with equal keys may change their relative order during the partitioning process. (A)</p> Signup and view all the answers

How does increasing cache locality improve the performance of quicksort?

<p>By arranging data so that frequently accessed elements are stored closer together in memory, reducing access latency. (D)</p> Signup and view all the answers

What is the primary difference in how quicksort and mergesort utilize memory?

<p>Mergesort requires significantly more auxiliary space than quicksort. (F)</p> Signup and view all the answers

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?

<p>A random pivot is least likely to yield $\mathcal{O}(n^{2})$ performance, because it reduces the chance of consistently poor partitioning compared to always picking the first element in a sorted array. (E)</p> Signup and view all the answers

What characteristic makes quicksort not well-suited for sorting small arrays or sub-arrays?

<p>The overhead and complexity of partitioning aren't justified for shorter arrays. (D)</p> Signup and view all the answers

What is a primary advantage of using randomized quicksort compared to deterministic quicksort?

<p>Randomized quicksort avoids making assumptions about data, so is more resistant to worst-case inputs. (F)</p> Signup and view all the answers

Which statement is true regarding cache improvements?

<p>Spatial locality helps to quickly access nearby memory locations. (F)</p> Signup and view all the answers

What happens if a 'stop at equal keys' implementation is not used in quicksort?

<p>The quicksort's performance may degrade to (\sim 1/2 n^2) compares when the keys are all equal. (B)</p> Signup and view all the answers

Flashcards

Merge sort

A sorting algorithm that divides the array into two halves, sorts each recursively, and merges the arrays.

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

A method to merge sort that iteratively merges subarrays of increasing sizes until the entire array is sorted.

Auxiliary array (merge sort)

A sorting algorithm that requires an additional array for merging sorted subarrays.

Signup and view all the flashcards

Timsort

Adaptive sorting algorithm derived from merge sort and insertion sort, and identifies existing patterns in the data

Signup and view all the flashcards

Quicksort

A list of 10 important algorithm that is used for science and engineering

Signup and view all the flashcards

Quicksort

A sorting algorithm that partitions the array so that, for some index , all elements to the left are no larger than a[j], and all elements to the right are no smaller than a[j]

Signup and view all the flashcards

Pivot (Quicksort)

A value in the array that partitions the array so that, all elements to the left are no larger than the pivot, and all elements to the right are no smaller than the pivot

Signup and view all the flashcards

Partitioning

Process of dividing an array around a pivot value such that elements less than the pivot are on one side and elements greater than the pivot are on the other side.

Signup and view all the flashcards

In-place partitioning

Where the partition takes place directly within the original array, without needing extra memory space

Signup and view all the flashcards

Median-of-three

A technique to select a suitable pivot that avoids worst-case scenarios.

Signup and view all the flashcards

Quicksort (Stop at equal keys)

A sorting algorithm that chooses a pivot and stops until i and j pointers are equal

Signup and view all the flashcards

3-way partitioning

A technique to improve algorithm performance by dividing the input into multiple sections (typically three) of lesser, equal, and greater value

Signup and view all the flashcards

Dutch National Flag Problem

An issue sorting three colors in a constant space

Signup and view all the flashcards

3-way Partitioning Goal

Algorithm to partition three parts so that, entries between 1t and gt= partition item, no larger left of lt, smaller right of gt

Signup and view all the flashcards

2-pivot Quicksort

An alternative to 3 way partitioning. Use two partitioning items and divide into three subarrays

Signup and view all the flashcards

3-pivot Quicksort

Similar to 2-pivot but there are three partitioning items and four subarrays

Signup and view all the flashcards

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

Signup and view all the flashcards

Quicksort cache improvements

A way to improve the cache in Quicksort. The locality allows for smaller recursive calls to smaller sub problems

Signup and view all the flashcards

Java sorting methods

A description stating which sorting algorithm is used with which primitive data type within Java

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.

Quiz Team

Related Documents

More Like This

Merge Sort Algorithm Overview
28 questions
Merge Sort Algorithm Overview
13 questions

Merge Sort Algorithm Overview

FastestGrowingSulfur8880 avatar
FastestGrowingSulfur8880
5 Popular Sorting Algorithms in Java
42 questions

5 Popular Sorting Algorithms in Java

AccommodativeHeliotrope5023 avatar
AccommodativeHeliotrope5023
Use Quizgecko on...
Browser
Browser