30
CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

Embed Size (px)

Citation preview

Page 1: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

CS2006 - Data Structures I

Chapter 10

Algorithm Efficiency

& Sorting III

Page 2: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

2

Topics

Sorting Merge Sort Quick Sort

Page 3: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

3

Merge Sort - General Description

Recursively divide list in half until each piece is of size one:

then merge adjacent one element lists into a sorted two element list.

each sorted part is merged with adjacent parts to give a larger sorted list.

eventually the final two pieces are merged to give a sorted version of the original list.

Page 4: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

4

Merge Sort

Example:Divide array in two halves: 8 1 4 3 2

8 1 4 3 2

Sort the halves: 1 4 8 2 3

Merge the halves into temporary array:

1 < 2 Put 1 into Temporary array 1

2 < 4 Put 2 into TemArr 1 2

3 < 4 Put 3 into TemArr 1 2 3

Right half finished; add remaing elements

1 2 3 4 8

Copy Temporary array back into original array

1 2 3 4 8

Page 5: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

5

mergeSort -methodpublic static void mergeSort(Comparable[ ] theArray, int first, int last) {// ---------------------------------------------------------// Sorts the items in an array into ascending order. // Calls: merge.// --------------------------------------------------------- if (first < last) { // sort each half int mid = (first + last)/2; // index of midpoint // sort left half theArray[first..mid] mergeSort(theArray, first, mid); // sort right half theArray[mid+1..last] mergeSort(theArray, mid+1, last); // merge the two halves merge(theArray, first, mid, last); } // end if} // end mergesort

Page 6: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

6

merge - methodprivate static void merge(Comparable[ ] theArray, int first, int mid, int last) {// ---------------------------------------------------------// Merges two sorted array segments theArray[first..mid] and // theArray[mid+1..last] into one sorted array.// Implementation note: This method merges the two subarrays into a temporary array // and copies the result into the original array anArray.// ---------------------------------------------------------

int maxSize = theArray.length; // temporary array Comparable[ ] tempArray = new Comparable[maxSize];

// initialize the local indexes to indicate the subarrays int first1 = first; // beginning of first subarray int last1 = mid; // end of first subarray int first2 = mid + 1; // beginning of second subarray int last2 = last; // end of second subarray

Page 7: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

7

merge - method (2) // while both subarrays are not empty, copy the // smaller item into the temporary array int index = first1; // next available location in tempArray while ((first1 <= last1) && (first2 <= last2)) { // Invariant: tempArray[first1..index-1] is in order if (theArray[first1].compareTo(theArray[first2])<0) { tempArray[index] = theArray[first1]; first1++; } else { tempArray[index] = theArray[first2]; first2++; } // end if index++; } // end while

Page 8: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

8

merge - method (3) // finish off the nonempty subarray // finish off the first subarray, if necessary

while (first1 <= last1) { // Invariant: tempArray[first1..index-1] is in order

tempArray[index] = theArray[first1]; first1++; index++; } // end while

// finish off the second subarray, if necessary

while (first2 <= last2) { // Invariant: tempArray[first1..index-1] is in order

tempArray[index] = theArray[first2]; first2++; index++; } // end while // copy the result back into the original array

for (index = first; index <= last; ++index) { theArray[index] = tempArray[index]; } // end for} // end merge

Page 9: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

9

mergeSort -driver

public static void main(String[ ] args) {

Integer[ ] values = new Integer[10];

values[0] = new Integer(9); values[1] = new Integer(15);

values[2] = new Integer(13); values[3] = new Integer(20);

values[4] = new Integer(5); values[5] = new Integer(0);

values[6] = new Integer(7); values[7] = new Integer(10);

values[8] = new Integer(3); values[9] = new Integer(2);

mergeSort(values, 0, values.length -1);

}

Page 10: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

10

Merge Sort

Analysis: O(n * log2 n)

Self study

Page 11: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

11

Quicksort

Overview A recursive sorting algorithm which divides an

array into two smaller partitions. All small elements in one partition and large

elements in another Calls itself recursively with each partition - a

divide and conquer algorithm Requires use of an array since random access

is necessary.

Page 12: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

12

Quicksort

Partitioning Scheme Choose the median element in the array as the

"pivot" element (ideally). All elements smaller than the pivot go into the

part of the array below the pivot. All elements larger than the pivot go into the

part of the array above the pivot.

Page 13: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

13

Quicksort

Choosing the "pivot": Finding the true median element is more time

consuming than quicksort itself so - Choose (guess) the pivot element quickly. Commonly used methods are to choose the

first or the middle element as pivot. More on pivot choosing in the analysis section.

Page 14: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

14

Quick Sort

Choosing the pivot:

< P P P

F Pivot Index L

S1 S2

P < P P ?

F

Pivot

L

S1 S2 Unknown

LastS1 FirstUnknown

Page 15: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

15

Quick Sort

Swapping elements:

Page 16: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

16

Quick Sort Example:

Original array 27 38 12 39 27 16

27 38 12 39 27 16

27 12 38 39 27 16

27 12 38 39 27 16

27 12 38 39 27 16

27 12 16 39 27 38

First partition 12 16 27 39 27 38

Pivot Unknown

S2

S2

S2

S2

S1

S1

S1

S1

S2

Unknown

Unknown

Unknown

Unknown

Swap 16 & 38

Swap Pivot & 16

Page 17: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

17

Quicksort Methodpublic static void quicksort (Comparable[] theArray, int first, int last) {// ---------------------------------------------------------// sorting the items in an array into ascending order // Precondition: theArray[first..last] is an array; first <= last.// Postcondition: theArray[first..last] is an sorted array.// ---------------------------------------------------------

int pivotIndex;if (first<last) {

pivotIndex = partition (theArray, first, last);

// sort region S1 and S2 quicksort(theArray, first, pivotIndex-1);quicksort(theArray, pivotIndex+1, last);

} // end of if } //end quicksort

Page 18: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

18

Partition Methodprivate static int partition(Comparable[] theArray, int first, int last) {// ---------------------------------------------------------// Partitions an array for quicksort.// Precondition: theArray[first..last] is an array; first <= last.// Postcondition: Returns the index of the pivot element of theArray [first..last].// Upon completion of the method, this will be the index value lastS1 such that// S1 = theArray[first..lastS1-1] < pivot// theArray[lastS1] == pivot// S2 = theArray[lastS1+1..last] >= pivot// --------------------------------------------------------- // tempItem is used to swap elements in the array Comparable tempItem; // place pivot in theArray[first] // choosePivot(theArray, first, last); Comparable pivot = theArray[first]; // reference pivot

// initially, everything but pivot is in unknown int lastS1 = first; // index of last item in S1

Page 19: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

19

Partition Method (2) // move one item at a time until unknown region is empty for (int firstUnknown = first + 1; firstUnknown <= last; ++firstUnknown) { // move item from unknown to proper region if (theArray[firstUnknown].compareTo(pivot) < 0) { // item from unknown belongs in S1 ++lastS1; tempItem = theArray[firstUnknown]; theArray[firstUnknown] = theArray[lastS1]; theArray[lastS1] = tempItem; } // end if // else item from unknown belongs in S2 } // end for // place pivot in proper position and mark its location tempItem = theArray[first]; theArray[first] = theArray[lastS1]; theArray[lastS1] = tempItem; return lastS1;} // end partition

Page 20: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

20

Quicksort Analysis

Best Case Assumptions: All array elements are in random order. We can choose a perfect median value.

thus the array is cut exactly in half on each recursive call.

Partition takes O(n) time since the loop runs from the second to the last element.

for (int firstUnknown = first + 1; firstUnknown <= last; ++firstUnknown)

Page 21: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

21

Quicksort Analysis (2) Determine the total number of calls to quickSort.

A call to quicksort with an array partition size (n) of 1 (first == last) generates no further recursive calls.

Each call to quicksort cuts the array size in half so partition deals with 1/2 the previous number of elements.

n = 8

n = 4n = 4

n = 2n = 2

n = 1 n = 1 n = 1 n = 1 n = 1 n = 1 n = 1 n = 1

n = 2 n = 2

Page 22: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

22

Quicksort Analysis (3)

Find the growth rate function f(n): On each level of this tree the partition call(s) deal

with a sum total of n elements. So the question becomes: how many levels are

there in the tree? Since the number of elements is cut in half on

each level there are log2n + 1 levels.

So f(n) = n ( log2n + 1 ) = n log

2n + n

The time complexity of quickSort is O(nlog2n)

Page 23: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

23

Quicksort Analysis (4) Worst case: ?

Page 24: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

24

Quicksort Analysis (4) Worst case:

data is already sorted. the first (rather than middle) element is chosen as the

pivot. only one element (the first) gets partitioned off each call to partition has to deal with one less element and

there are n recursive calls f(n) = n + (n-1) + (n-2) + (n-3) + . . .+ 1 = n(n+1)/2 = n2/2 +

n/2 The time complexity becomes O(n2) which is the same as

bubblesort or insertion sort.

Page 25: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

25

Quicksort Analysis (4)

Worst case: Insertion sort has a time complexity which

approaches O(n) for nearly sorted (or small arrays) while quicksort approaches O(n2)

Thus, insertion sort is better than quicksort for small or nearly sorted arrays.

Page 26: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

26

Review Each merge step of the mergesort

requires ______ major operations. 3 * n – 1 4 * n – 1 (n – 1)/2 n – 1

Page 27: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

27

Review The quicksort is ______ in the worst case.

O(n2) O(n3) O(n * log2n)

O(log2n)

Page 28: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

28

Review A bubble sort requires at most ______

passes to sort an array of n items. n/2 n – 2 n – 1 n

Page 29: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

29

Review Assuming a linked list of n nodes, the code fragment:

Node curr = head;

while (curr != null) {

System.out.println(curr.getItem());

curr.setNext(curr.getNext());

} // end while

requires ______ write operations. n n – 1 n + 1

1

Page 30: CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

30

Review An algorithm’s execution time is related to

the number of ______ it requires. parameters test data sets data fields operations