introduction to data structures and algorithms

14
Introduction to Data Introduction to Data Structures and Structures and Algorithms Algorithms Chapter 7 Chapter 7 Quick Sort Quick Sort

Upload: yardley-shannon

Post on 02-Jan-2016

25 views

Category:

Documents


1 download

DESCRIPTION

Introduction to Data Structures and Algorithms. Chapter 7 Quick Sort. Quick Sort. The general strategy of QuickSort is to: Partition the list into two sublists, rearranging the keys of a list to be sorted, so that all the “small” keys precede the “large” keys. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Data Structures and Algorithms

Introduction to Data Introduction to Data Structures and Structures and

AlgorithmsAlgorithms

Chapter 7Chapter 7

Quick SortQuick Sort

Page 2: Introduction to Data Structures and Algorithms

Quick SortQuick SortThe general strategy of QuickSort is to:• Partition the list into two sublists,

rearranging the keys of a list to be sorted, so that all the “small” keys precede the “large” keys.

• QuickSort the two sublists recursively

Page 3: Introduction to Data Structures and Algorithms

Partition AlgorithmPartition Algorithm

select splitVal, which is the pivot valuesplit the list so that value[first] .. value[splitPt-1] < splitVal value[splitPt] = splitVal value[splitPt+1] .. Value[last] > splitVal

Page 4: Introduction to Data Structures and Algorithms

Algorithm Implementation(1)Algorithm Implementation(1)

function partition return type integer parameters left of type integer right of type integer pivot of type longis variables define leftPtr of type integer define rightPtr of type integer define found of type booleanbegin set leftPtr = left set righPtr = right while leftPtr < rightPtr do set found = false // find element larger than pivot while leftPtr < right and found != true do if theArray [leftPtr] < pivot then increment leftPtr else found = true endif endwhile

Page 5: Introduction to Data Structures and Algorithms

Algorithm Implementation(2)Algorithm Implementation(2)

set found = false // find element smaller than pivot while rightPtr < left and found != true do if theArray [rightPtr] > pivot then decrement rightPtr else found = true endif endwhile // now swap the two elements call swap using leftPtr, rightPtr endwhile return leftPtr // index of partition pointendfun partition // end of function

Page 6: Introduction to Data Structures and Algorithms

Efficiency of Partition AlgorithmEfficiency of Partition Algorithm

• The algorithm has a time complexity of O(N)

• The two indices (leftPtr and rightPtr) start at opposite end of the array and move towards each other, swapping as they move

• On average, there will be N/2 comparisons

Page 7: Introduction to Data Structures and Algorithms

QuickSortQuickSort

function quickSort parameters first of type integer, last of type integerisvariables define splitPt of type integerbegin if first < last then set splitPt = partition (first, last, pivot) quickSort(first, spliPt-1) quickSort(splitPt+1, last) endifendfun quickSort

Page 8: Introduction to Data Structures and Algorithms

Time Complexity of Quick SortTime Complexity of Quick Sort

• The time complexity of Quick sort is O(N log N)

• It is an in-place algorithm• Ideally, the pivot should be median of

the values being sorted• This would be the case in having two

subarrays of equal size• In the worst case, the algorithm

degenerates to O(N2)

Page 9: Introduction to Data Structures and Algorithms

Loop Invariants and VariantsLoop Invariants and Variants• To help verify the correctness of loops • A loop invariant is a boolean expression

that evaluates to true every time the loop guard is evaluated.

• The boolean expression normally includes variables used in the loop.

Page 10: Introduction to Data Structures and Algorithms

Loop InvariantsLoop Invariants• The initial value of the loop invariant

helps determine the proper initial values of variables used in the loop guard and body

• In the loop body, some statements make the invariant false, and other statements must then re-establish the invariant so that it is true before the loop guard is evaluated again.

Page 11: Introduction to Data Structures and Algorithms

ExampleExample

function gcd return type integer parameters a of type integer, b of type integer is set x = a set y = b precondition a > 0 and b > 0 begin while x != y do if x > y then

set x = x – y else set y = y – x endif endwhileendfun gcd

Page 12: Introduction to Data Structures and Algorithms

Loop Invariant ExampleLoop Invariant Example

• In the loop code above, the following boolean expression is true after loop initialization and maintained on every iteration: x > 0 and y > 0

• The OOSimL language syntax allows us to write an additional construct:

loopinv

X > 0 and y > 0

Page 13: Introduction to Data Structures and Algorithms

Loop VariantLoop Variant

• The loop variant is an integer expression that always evaluates to a non-negative integer value and decreases on every iteration

• The loop variant helps to guarantee that the loop terminates (in a finite number of iterations).

Page 14: Introduction to Data Structures and Algorithms

ExampleExample

• The the gcd function, included above, the variant is: max(x,y).

• In OOsimL, the syntax for this construct appears as:

loopvariant

max (x, y)