Docsity

Divide and Conquer Sorting - Algorithms and Applications in Java - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Divide and Conquer Sorting - Algorithms and Applications in Java - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Divide-And-Conquer Sorting

  • Small instance.

 n <= 1 elements.  n <= 10 elements.  We’ll use n <= 1 for now.

  • Large instance.

 Divide into k >= 2 smaller instances.  k = 2, 3, 4, …?  What does each smaller instance look like?  Sort smaller instances recursively.  How do you combine the sorted smaller instances?Docsity.com

Insertion Sort

  • k = 2
  • First n - 1 elements (a[0:n-2]) define one of the smaller instances; last element (a[n-1]) defines the second smaller instance.
  • a[0:n-2] is sorted recursively.
  • a[n-1] is a small instance.

a[0] (^) a[n-2] a[n-1]

Divide And Conquer

  • Divide-and-conquer algorithms generally have best complexity when a large instance is divided into smaller instances of approximately the same size.
  • When k = 2 and n = 24, divide into two smaller instances of size 12 each.
  • When k = 2 and n = 25, divide into two smaller instances of size 13 and 12, respectively.

Merge Sort

  • k = 2
  • First ceil(n/2) elements define one of the smaller instances; remaining floor(n/2) elements define the second smaller instance.
  • Each of the two smaller instances is sorted recursively.
  • The sorted smaller instances are combined using a process called merge.
  • Complexity is O(n log n).
  • Usually implemented nonrecursively.

Merge Two Sorted Lists

• A = (5, 6)

B = (3, 8, 9, 10)

C = (1, 2)

• A = (5, 6)

B = (8, 9, 10)

C = (1, 2, 3)

• A = (6)

B = (8, 9, 10)

C = (1, 2, 3, 5) Docsity.com

Merge Two Sorted Lists

• A = ()

B = (8, 9, 10)

C = (1, 2, 3, 5, 6)

  • When one of A and B becomes empty, append the other list to C.
  • O(1) time needed to move an element into C.
  • Total time is O(n + m), where n and m are, respectively, the number of elements initially in A and B.

Merge Sort

[3, 8] [6, 13]

[3, 6, 8, 13]

[8] [3] [13] [6]

[2, 14]

[2, 5, 14]

[2, 3, 5, 6, 8, 13, 14]

[5]

[2] [14]

[9, 10]

[1, 9, 10]

[1]

[9] [10]

[7, 12]

[4, 7, 12]

[1, 4, 7, 9, 10,12]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13,14]

[4]

[7] [12]

Time Complexity

  • Let t(n) be the time required to sort n elements.
  • t(0) = t(1) = c, where c is a constant.
  • When n > 1,

t(n) = t(ceil(n/2)) + t(floor(n/2)) + dn, where d is a constant.

  • To solve the recurrence, assume n is a power of 2 and use repeated substitution.
  • t(n) = O(n log n).

Nonrecursive Merge Sort

[8] [3] [13] [6] [2] [14] [5] [9] [10] [1] [7] [12] [4]

[3, 8] [6, 13] [2, 14] [5, 9] [1, 10] [7, 12] [4]

[3, 6, 8, 13] [2, 5, 9, 14] [1, 7, 10, 12] [4]

[2, 3, 5, 6, 8, 9, 13, 14] [1, 4, 7, 10, 12]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14]

Complexity

  • Sorted segment size is 1, 2, 4, 8, …
  • Number of merge passes is ceil(log 2 n).
  • Each merge pass takes O(n) time.
  • Total time is O(n log n).
  • Need O(n) additional space for the merge.
  • Merge sort is slower than insertion sort when n <= 15 (approximately). So define a small instance to be an instance with n <= 15.
  • Sort small instances using insertion sort.
  • Start with segment size = 15.

Quick Sort

  • Small instance has n <= 1. Every small instance is a sorted instance.
  • To sort a large instance, select a pivot element from out of the n elements.
  • Partition the n elements into 3 groups left, middle and right.
  • The middle group contains only the pivot element.
  • All elements in the left group are <= pivot.
  • All elements in the right group are >= pivot.
  • Sort left and right groups recursively.
  • Answer is sorted left group, followed by middle group followed by sorted right group. Docsity.com

Example

6 2 8 5 11 10 4 1 9 7 3

Use 6 as the pivot.

2 5 4 1 3 6 7 9 10 11 8

Sort left and right groups recursively.

Choice Of Pivot

  • Median-of-Three rule. From the leftmost, middle, and rightmost elements of the list to be sorted, select the one with median key as the pivot.  When sorting a[6:20], examine a[6], a[13] ((6+20)/2), and a[20]. Select the element with median (i.e., middle) key.  If a[6].key = 30, a[13].key = 2, and a[20].key = 10, a[20] becomes the pivot.  If a[6].key = 3, a[13].key = 2, and a[20].key = 10, a[6] becomes the pivot.

Choice Of Pivot

 If a[6].key = 30, a[13].key = 25, and a[20].key = 10, a[13] becomes the pivot.

  • When the pivot is picked at random or when the median-of-three rule is used, we can use the quick sort code of the text provided we first swap the leftmost element and the chosen pivot.

pivot

swap