Docsity

Comprehensive Guide to Sorting Algorithms: Merge, Quick, Bucket, and Radix Sort, Slides of Data Structures and Algorithms

Partial preview of the text

Download Comprehensive Guide to Sorting Algorithms: Merge, Quick, Bucket, and Radix Sort and more Slides Data Structures and Algorithms in PDF only on Docsity!

Data Structures &

Algorithm Analysis

Today…

  • Quick Review
    • Divide and Conquer paradigm
    • Merge Sort
    • Quick Sort
  • Two more sorting algorithms
    • Bucket Sort
    • Radix Sort

Merge-Sort

  • Algorithm:
    • Divide : If S has at leas two elements (nothing needs to be done if S has zero or one elements), remove all the elements from S and put them into two sequences, S 1 and S 2 , each containing about half of the elements of S. (i.e. S 1 contains the first n/2 elements and S 2 contains the remaining n/2 elements.
    • Recur : Recursive sort sequences S 1 and S 2.
    • Conquer : Put back the elements into S by merging the sorted sequences S 1 and S 2 into a unique sorted sequence. Docsity.com

Merge-Sort Example

Quick-Sort

1) Divide : If the sequence S has 2 or more elements,

select an element x from S to be your pivot. Any

arbitrary element, like the last, will do. Remove all the

elements of S and divide them into 3 sequences:

L, holds S’s elements less than x E, holds S’s elements equal to x G, holds S’s elements greater than x

2) Recurse : Recursively sort L and G

3) Conquer : Finally, to put elements back into S in order,

first inserts the elements of L, then those of E, and

those of G. Docsity.com

Idea of Quick Sort

  1. Select : pick an element

2 ) Divide : rearrange elements so that x goes to its final position E

  1. Recurse and Conquer : recursively sort

In-Place Quick-Sort

Divide step: l scans the sequence from the left, and r from the right.

A swap is performed when l is at an element larger than the pivot and r is at one smaller than the pivot.

In Place Quick Sort (cont’d)

A final swap with the pivot completes the divide step

More Sorting Algorithms

  • Bucket Sort
  • Radix Sort
  • Stable sort
    • A sorting algorithm where the order of elements having the same key is not changed in the final sequence
    • Is bubble sort stable?
    • Is merge sort stable? Docsity.com

Bucket Sort

  • Bucket sort
    • Assumption: the keys are in the range [0, N)
    • Basic idea:
      1. Create N linked lists ( buckets ) to divide interval [0,N) into subintervals of size 1
      2. Add each input element to appropriate bucket
      3. Concatenate the buckets
    • Expected total time is O(n + N), with n = size of original sequence - if N is O(n)  sorting algorithm in O(n)!

Bucket Sort

Now, pull the elements from the buckets into the array

At last, the sorted array (sorted in a stable way): Docsity.com

Does it Work for Real Numbers?

  • What if keys are not integers?
    • Assumption: input is n reals from [0, 1)
    • Basic idea:
      • Create N linked lists ( buckets ) to divide interval [0,1) into subintervals of size 1/ N
      • Add each input element to appropriate bucket and sort buckets with insertion sort
    • Uniform input distribution  O(1) bucket size
      • Therefore the expected total time is O(n)
    • Distribution of keys in buckets similar with ….?

Radix Sort

  • Intuitively, you might sort on the most

significant digit, then the second most

significant, etc.

  • Problem: lots of intermediate piles of cards to

keep track of

  • Key idea: sort the least significant digit first

RadixSort(A, d) for i=1 to d StableSort(A) on digit i

Radix Sort

  • Can we prove it will work?
  • Inductive argument:
    • Assume lower-order digits {j: j<i}are sorted
    • Show that sorting next digit i leaves array correctly sorted - If two digits at position i are different, ordering numbers by that digit is correct (lower-order digits irrelevant) - If they are the same, numbers are already sorted on the lower-order digits. Since we use a stable sort, the numbers stay in the right order