+ All Categories
Home > Documents > Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting...

Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting...

Date post: 25-May-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
32
Quicksort 4-18-2013
Transcript
Page 1: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

Quicksort

4-18-2013

Page 2: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

Thursday, April 18th Barben Rooms A&B, Cheel

4:00 pm–4:45 pm Panel discussion on “Technical Career Opportunities”, Moderated by Bob Lockwood ‘86

5:00 pm–5:45 pm Roundtables:

How to Communicate with your Client – Differences in Internal vs. External Client Development with Chris Snelling ‘86 and Rich Bogart ‘86

Technology Trends in Industry with Ron Ayers ‘02 and Chris Fohlin ‘07

Tricks and Tips for the Job Seeker with Dan Dedrick ‘06 and Bob Lockwood ‘86

6:00 pm – 6:45 pm “Building Your Digital Presence”; Presentation by Chris Fohlin ’07

Employers and clients do their homework on you before you walk through the door. Does your online presence paint the right picture? Learn best practices for managing, and maximizing, your professional digital image.

)

Page 3: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

Sorting

Quicksort

OOP

inheritance

Reading: Maciel

Chapter 15, Sorting

Project#2: Evil Hangman, due Wed. 4/24

see the sample output

Page 4: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

Selection Sort ϴ(n2)

if ( [start,stop) contains more than one element ) {

i_max = index of the largest element in [start,stop)

swap a[i_max] and a[stop - 1]

sort [start, stop-1)

}

Insertion Sort ϴ(n2)

if ( [start,stop) contains more than one element ) {

sort [start, stop-1)

insert a[stop-1] into [start, stop-1)

}

Page 5: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

Mergesort

if ( the array contains more than one element ) {

sort the first half of the array

sort the second half of the array

merge the two sorted halves

}

Quicksort

find a “pivot point”

partition around the pivot

sort halves

Page 6: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

example run: (top level of recursion)

[ 60 12 37 42 25 38 16 ]

[ 60 12 37 42 ] [ 25 38 16 ] 1. divide in half

[ 12 37 42 60 ] [ 16 25 38 ] 2. sort halves

[ 12 16 25 37 38 42 60 ] 3. merge

Page 7: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

example run: trace

[ 60 12 37 42 25 38 16 ]

[ 60 12 37 42 ] [ 25 38 16 ]

[ 60 12 ] [ 37 42 ] [ 25 38 ] [ 16 ]

[ 60 ] [ 12 ] [ 37 ] [ 42 ] [ 25 ] [ 38 ] [ 16 ]

[ 12 60 ] [ 37 42 ] [ 25 38 ] [ 16 ]

[ 12 37 42 60 ] [ 16 25 38 ]

[ 12 16 25 37 38 42 60 ]

Page 8: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

merging algorithm: array [ 60 12 37 42 25 38 16]

first second result

[ 12 37 42 60 ] [ 16 25 38 ] [ ]

[ 37 42 60 ] [ 16 25 38 ] [ 12 ]

[ 37 42 60 ] [ 25 38 ] [ 12 16 ]

[ 37 42 60 ] [ 38 ] [ 12 16 25 ]

[ 42 60 ] [ 38 ] [ 12 16 25 37 ]

[ 42 60 ] [ ] [ 12 16 25 37 38 ]

[ ] [ ] [ 12 16 25 37 38 42 60 ]

Page 9: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

idea:

find a “pivot point”

partition around the pivot

sort halves

}

if ( the array contains more than one element ) {

choose a pivot element

partition the array around the pivot

sort each subarray

}

Page 10: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

Partition must rearrange the array so that the following 3 conditions hold:

1. the element a[p] is in its final place in the array, for some p

2. all elements in a[first..p-1] are ≤ a[p]

3. all elements in a[p+1..last] are ≥ a[p]

Page 11: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

example: [ 30 10 14 37 42 13 51 5 30 ]

pivot

1. select a pivot

2. partition around the pivot

[ 10 14 13 5 | 30 | 37 42 51 30 ]

3. sort the partitions

[ 5 10 13 14 30 30 37 42 51 ]

If the pivot point divides the list exactly in half, then the number of comparisons would be N log N

If the pivot point is chosen so that one partition is always empty, then the running time is N2

Page 12: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

The performance of quicksort heavily depends on the pivot point

best case: pivot element is the median value in the array (divides the array in half)

worst case: the first element of the array is chosen to be the pivot point, but the array is already sorted

in practice, large parts of an input array can be already in sorted order

choose a random element to be the pivot (but then need a call to a pseudorandom number generator)

compromise: choose the median of the first, middle and last element of the array

Page 13: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

example: [ 60 12 37 42 25 38 16 ]

pivot

1. select a pivot: median of [16 42 60] => 42

2. partition around the pivot

[ 12 37 25 38 16 ] 42 [ 60 ]

2. sort the partitions

[ 12 16 25 37 38 42 60 ]

Page 14: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

[ 60 12 37 42 25 38 16 ]

[ 12 37 25 38 16 ] 42 [ 60 ]

[ 12 ] 16 [ 37 25 38 ] 42 [ 60 ]

[ 12 ] 16 [ 25 ] 37 [ 38 ] 42 [ 60 ]

[ 12 ] 16 [ 25 37 38 ] 42 [ 60 ]

[12 16 25 37 38 ] 42 [ 60 ]

[12 16 25 37 38 42 ] [ 60 ]

[ 12 16 25 37 38 42 60 ]

Page 15: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

Quicksort

worst case: ϴ(n2)

best & average case: ϴ(n log n)

easy to write recursively

Mergesort

all cases: ϴ(n log n)

disadvantage: needs extra space proportional to n

good when sequential access is required (e.g. sort a linked list)

easy to write recursively

Page 16: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

use a better partitioning element (pivot) to avoid the worst case

median-of-3 partitioning works well in practice

use a simple sort for small partitions (can reduce running time by 20%)

e.g. if size of array ≤ M then use insertion sort

Page 17: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

The C++ standard library (in <algorithm>) provides the following functions: template<typename RI>

void sort(RI first, RI last);

template<typename RI, typename Compare>

void sort(RI first, RI last, Compare comp);

The template parameter RI is a random-access iterator.

The data to be sorted is in the range first .. last, where last is one past the last data value.

In practice you would tend to use these

Page 18: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

Assume that array items contains 16 integers.

sort(items, items + 16);

● will sort the whole array.

sort(items, items + 8);

● will sort the first half.

sort(items, items + 16, greater<int>()); ● will sort in descending order.

Assume that v is a vector.

sort(v.begin(), v.end()); // sorts vector v

Page 19: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

Objects, classes, and inheritance, plus

An object is a software entity that combines state and

behavior.

A class describes the state (member data) and services

(member functions) provided by objects that are

instances of that class.

Classes can be related by inheritance.

polymorphism and dynamic binding.

Goals of OO: abstraction, encapsulation,

comprehensibility, changeability, and reusability.

Page 20: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

We’ve now looked at classes and objects.

What about inheritance, polymorphism and dynamic binding?

Page 21: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

Object-oriented programming (OOP) is popular because:

It enables reuse of previous code saved as classes

Inheritance and hierarchical organization capture idea:

One thing is a refinement or extension of another

Page 22: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

UML Class Diagram

class Arrow : public Line {...}

An Arrow “is a” Line

Arrow

IS-A (inheritance)

Line

important to

make this public

class Line {...}

Page 23: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

FilledCircle Line Point

HAS-A

(composition)

2

UML Class Diagram

class Line {

private:

Point endpt1, endpt2;

A Line “has a” Point

Page 24: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

Circle

FilledCircle

Point

IS-A (inheritance)

HAS-A

(composition)

1

base class or superclass

derived class or subclass

Page 25: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

Confusing has-a and is-a leads to misusing inheritance

Model a has-a relationship with an attribute (data member/instance variable)

class C { ... private: B part; ...}

Model an is-a relationship with inheritance

If every C is-a B then model C as a derived class (also called subclass) of B

Show this: in C include : public B:

class C : public B { ... }

Page 26: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

/** the Point class represents a 2D point */

class Point {

private:

int xcoord, ycoord;

public:

Point() : xcoord(0), ycoord(0) {}

Point(int newx, int newy) : xcoord(newx), ycoord(newy) {}

int getX() { return xcoord; }

int getY() { return ycoord; }

void display();

};

Page 27: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

class Circle {

private:

Point center;

float radius:

public:

Circle(): center(Point()), radius(1.0f) {}

Circle(int x, int y, float newradius)

: center(Point(x,y)), radius(newradius) {}

float getRadius() { return radius; }

double computeArea();

void display();

};

Page 28: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

class FilledCircle public Circle {

private:

string color;

public:

FilledCircle(): Circle(), color(“black”) {}

FilledCircle(int x, int y, float newradius, string newcolor)

: Circle(x, y, newradius), color(newcolor) {}

};

Page 29: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

/** test program for shapes */

int main() {

/* Test the Point class, starting with constructors & accessors: */

Point p1;

Point p2(100, 200);

Point p3(50, 50);

cout << "Point 1: " << p1.getX() << ":" << p1.getY() << endl;

cout << "Point 2: " << p2.getX() << ":" << p2.getY() << endl;

cout << "Point 3: " << p3.getX() << ":" << p3.getY() << endl;

/* test Point's display method */

p2.display();

/* Next test the Circle class, starting with constructors: */

/* (continued on next slide) */

Page 30: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

/* test program for shapes, continued */

Circle c1(100, 40, 2.5f);

Circle c2;

/* test Circle's accessors and method computeArea */

cout << "Circle c1 radius: " << c1.getRadius() << endl;

cout << “Area of circle c1 is: " << c1.computeArea() << endl;

/* test Circle's display method */

c1.display();

/* Test the FilledCircle class, starting with the constructors */

FilledCircle fc3;

FilledCircle fc4(20, 30, 2.5f, "red");

/* continued on next slide */

Page 31: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

/* test program for shapes, continued */

/* Test the FilledCircle class, starting with the constructors */

FilledCircle fc3;

FilledCircle fc4(20, 30, 2.5f, "red");

/* Test the accessors and inherited method computeArea */

cout << "Filled Circle fc3 color is " << fc3.getColor() << endl;

cout << "fc3 area: " << fc3.computeArea() << endl;

cout << "Filled Circle fc4 color is " << fc4.getColor() << endl;

/* Test the FilledCircle's display method */

fc4.display();

/* Testing completed */

return 0;

}

Page 32: Quicksort 4-18-2013 - Clarkson Universityjsearlem/cs142/sp13/slides/24.qsort.pdf · Sorting Quicksort OOP inheritance Reading: Maciel Chapter 15, Sorting Project#2: Evil Hangman,

Sorting

Maciel: Chapter 15

Software Life Cycle

Maciel: Chapter 11

Error Checking

Maciel, Chapter 4


Recommended