sorting algorithms

87
Sorting Algorithms

Upload: shannon-adkins

Post on 03-Jan-2016

22 views

Category:

Documents


1 download

DESCRIPTION

Sorting Algorithms. Algorithms, revisited. What is an algorithm? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Sorting Algorithms

Sorting Algorithms

Page 2: Sorting Algorithms

Algorithms, revisited

What is an algorithm?

Wikipedia Definition: an algorithm is a definite list of well-defined instructions for completing a task that, given an initial state, will proceed through a well-defined series of successive states, eventually terminating in an end-state.

Page 3: Sorting Algorithms

Problems, solutions, and algorithms What is the difference between an algorithm

and a problem?

What is the difference between an algorithm and a solution to a problem?

What is the difference between an algorithm and a program?

Page 4: Sorting Algorithms

Properties of a good algorithm1. It clearly defines the state (variables)2. It clearly defines the order of execution for

instructions3. Each instruction is clear to the intended

audience You can leave out details about instructions when you are sure

that the reader can understand it without them.

Page 5: Sorting Algorithms

Example Algorithm: Washing Dishes

# Note: the indentation indicates# where the while loop ends.

Stack dishes by sink. Fill sink with hot soapy water.

while there are more dishes: Get dish from counter, Wash dish, Put dish in drain rack.

Wipe off counter. Rinse out sink.

Page 6: Sorting Algorithms

Example Algorithm: Greatest Common Divisor

# Compute the GCD using the brute # force approach. # Notation: x | y means x divides # y evenly with no remainder. # Inputs: m and n are two integers. # Output: gcf.

factor = 1, gcf = 1

while factor <= minimum of m and n: if factor | m and factor | n:

Let gcf = factor. Let factor = factor + 1.

Page 7: Sorting Algorithms

Algorithms: Why?

Why bother writing an algorithm? Why not just write the code?

An algorithm lets you leave out unimportant details of the program.

That way, you can focus on the hard parts of solving the problem.

Page 8: Sorting Algorithms

The Sorting Problem

Problem Statement:

Given a set of data and a method for comparing two data points to determine which is greater or smaller (or equal),

Organize the data into a sequence so that the smallest element appears first, and every subsequent element is at least as big as the one before it.

Page 9: Sorting Algorithms

The Sorting Problem

Problem Statement, revisited:

Input:A set of dataA comparison method

Output:Data organized in sequence, smallest to largest

Page 10: Sorting Algorithms

A potential solution?

Find the biggest element, move it to the end Of the (size-1) elements left, find the biggest

one, and move it to second-to-last. Repeat.

Is this a good algorithm?NO. It leaves out too much detail – how to find

the biggest element? How to move it to the end?

Page 11: Sorting Algorithms

BubbleSort

-29 40 8 -2 -15 10 -6 -7 31 0

Iteration 1

compare

They’re in the right order.

Don’t do anything.

Page 12: Sorting Algorithms

BubbleSort

-29 40 8 -2 -15 10 -6 -7 31 0

Iteration 1

compare

They’re in the wrong order.

Swap them.

Page 13: Sorting Algorithms

BubbleSort

-29 8 40 -2 -15 10 -6 -7 31 0

Iteration 1

compare

They’re in the wrong order.

Swap them.

Page 14: Sorting Algorithms

BubbleSort

-29 8 40 -2 -15 10 -6 -7 31 0

Iteration 1

compare

They’re in the wrong order.

Page 15: Sorting Algorithms

BubbleSort

-29 8 -2 40 -15 10 -6 -7 31 0

Iteration 1

compare

They’re in the wrong order.

Swap them.

Page 16: Sorting Algorithms

BubbleSort

-29 8 -2 40 -15 10 -6 -7 31 0

Iteration 1

compare

They’re in the wrong order.

Page 17: Sorting Algorithms

BubbleSort

-29 8 -2 -15 40 10 -6 -7 31 0

Iteration 1

compare

They’re in the wrong order.

Swap them.

Page 18: Sorting Algorithms

BubbleSort

-29 8 -2 -15 40 10 -6 -7 31 0

Iteration 1

compare

They’re in the wrong order.

Page 19: Sorting Algorithms

BubbleSort

-29 8 -2 -15 10 40 -6 -7 31 0

Iteration 1

compare

They’re in the wrong order.

Swap them.

Page 20: Sorting Algorithms

BubbleSort

-29 8 -2 -15 10 40 -6 -7 31 0

Iteration 1

compare

They’re in the wrong order.

Page 21: Sorting Algorithms

BubbleSort

-29 8 -2 -15 10 -6 40 -7 31 0

Iteration 1

compare

They’re in the wrong order.

Swap them.

Page 22: Sorting Algorithms

BubbleSort

-29 8 -2 -15 10 -6 40 -7 31 0

Iteration 1

compare

They’re in the wrong order.

Page 23: Sorting Algorithms

BubbleSort

-29 8 -2 -15 10 -6 -7 40 31 0

Iteration 1

compare

They’re in the wrong order.

Swap them.

Page 24: Sorting Algorithms

BubbleSort

-29 8 -2 -15 10 -6 -7 40 31 0

Iteration 1

compare

They’re in the wrong order.

Page 25: Sorting Algorithms

BubbleSort

-29 8 -2 -15 10 -6 -7 31 40 0

Iteration 1

compare

They’re in the wrong order.

Swap them.

Page 26: Sorting Algorithms

BubbleSort

-29 8 -2 -15 10 -6 -7 31 40 0

Iteration 1

compare

They’re in the wrong order.

Page 27: Sorting Algorithms

BubbleSort

-29 8 -2 -15 10 -6 -7 31 0 40

Iteration 1

compare

They’re in the wrong order.

Swap them.

Page 28: Sorting Algorithms

BubbleSort

-29 8 -2 -15 10 -6 -7 31 0 40

Iteration 1

Notice that the biggest element is now at the end.

Page 29: Sorting Algorithms

BubbleSort

-29 8 -2 -15 10 -6 -7 31 0 40

Iteration 2

Repeat the same process as iteration 1

compare

They’re in the right order.

Don’t do anything.

Page 30: Sorting Algorithms

BubbleSort

-29 8 -2 -15 10 -6 -7 31 0 40

Iteration 2

Repeat the same process as iteration 1

compare

They’re in the wrong order.

Page 31: Sorting Algorithms

BubbleSort

-29 -2 8 -15 10 -6 -7 31 0 40

Iteration 2

Repeat the same process as iteration 1

compare

They’re in the wrong order.

Swap them.

Page 32: Sorting Algorithms

BubbleSort

-29 -2 8 -15 10 -6 -7 31 0 40

Iteration 2

Repeat the same process as iteration 1

compare

They’re in the wrong order.

Page 33: Sorting Algorithms

BubbleSort

-29 -2 -15 8 10 -6 -7 31 0 40

Iteration 2

Repeat the same process as iteration 1

compare

They’re in the wrong order.

Swap them.

Page 34: Sorting Algorithms

BubbleSort

-29 -2 -15 8 10 -6 -7 31 0 40

Iteration 2

Repeat the same process as iteration 1

compare

They’re in the right order.

Don’t do anything.

Page 35: Sorting Algorithms

BubbleSort

-29 -2 -15 8 10 -6 -7 31 0 40

Iteration 2

Repeat the same process as iteration 1

compare

They’re in the wrong order.

Page 36: Sorting Algorithms

BubbleSort

-29 -2 -15 8 -6 10 -7 31 0 40

Iteration 2

Repeat the same process as iteration 1

compare

They’re in the wrong order.

Swap them.

Page 37: Sorting Algorithms

BubbleSort

-29 -2 -15 8 -6 10 -7 31 0 40

Iteration 2

Repeat the same process as iteration 1

compare

They’re in the wrong order.

Page 38: Sorting Algorithms

BubbleSort

-29 -2 -15 8 -6 -7 10 31 0 40

Iteration 2

Repeat the same process as iteration 1

compare

They’re in the wrong order.

Swap them.

Page 39: Sorting Algorithms

BubbleSort

-29 -2 -15 8 -6 -7 10 31 0 40

Iteration 2

Repeat the same process as iteration 1

compare

They’re in the right order.

Don’t do anything.

Page 40: Sorting Algorithms

BubbleSort

-29 -2 -15 8 -6 -7 10 31 0 40

Iteration 2

Repeat the same process as iteration 1

compare

They’re in the wrong order.

Page 41: Sorting Algorithms

BubbleSort

-29 -2 -15 8 -6 -7 10 0 31 40

Iteration 2

Repeat the same process as iteration 1

compare

They’re in the wrong order.

Swap them.

Page 42: Sorting Algorithms

BubbleSort

-29 -2 -15 8 -6 -7 10 0 31 40

Iteration 2

Repeat the same process as iteration 1

Stop at second-to-last element this time.

Notice that the second-biggest element is now in the second-to-last place.

Page 43: Sorting Algorithms

BubbleSort

Iteration k

Repeat the same process as iteration 1

Stop at k-to-last element.

The kth iteration will guarantee that the kth-biggest element ends up in the kth-to-last spot.

-29 -2 -15 8 -6 -7 10 0 31 40

Page 44: Sorting Algorithms

BubbleSort

-29 -15 -2 -6 -7 8 0 10 31 40

Iteration k

Repeat the same process as iteration 1

Stop at k-to-last element.

The kth iteration will guarantee that the kth-biggest element ends up in the kth-to-last spot.

Iteration 3 puts 3rd-biggest element here.

Page 45: Sorting Algorithms

BubbleSort

-29 -15 -6 -7 -2 0 8 10 31 40

Iteration k

Repeat the same process as iteration 1

Stop at k-to-last element.

The kth iteration will guarantee that the kth-biggest element ends up in the kth-to-last spot.

Iteration 4 puts 4th-biggest element here.

In this case, iteration 4 also puts the 5th- and 6th-biggest elements in the right places, but that’s not guaranteed to happen in general.

Page 46: Sorting Algorithms

BubbleSort

-29 -15 -7 -6 -2 0 8 10 31 40

Iteration k

Repeat the same process as iteration 1

Stop at k-to-last element.

The kth iteration will guarantee that the kth-biggest element ends up in the kth-to-last spot.

Iteration 5 just swaps these two guys.

Page 47: Sorting Algorithms

BubbleSort

-29 -15 -7 -6 -2 0 8 10 31 40

Iteration k

Repeat the same process as iteration 1

Stop at k-to-last element.

The kth iteration will guarantee that the kth-biggest element ends up in the kth-to-last spot.

Iteration 6 leaves everything alone – everything is in the right spot!

BubbleSort stops when one full iteration performs zero swaps.

Page 48: Sorting Algorithms

A Sorting Algorithm: BubbleSort# Input: an array of elements called D

# Output: D is sorted

performedSwap = true

while performedSwap is true:

performedSwap = false

for i goes from 0 to the end of D, less 1:

if D[i] > D[i+1]:

swap D[i] and D[i+1]

performedSwap = true

Page 49: Sorting Algorithms

BubbleSort is Slow!!!

BubbleSort is very inefficient on large arrays Java does not use the BubbleSort algorithm for its

Arrays.sort() and Collections.sort() In fact, almost nobody does BubbleSort, except

as a teaching device We will talk later on about how you can tell that it’s

not an efficient algorithm

Page 50: Sorting Algorithms

Efficient Sorting Algorithms

There are a large number of algorithms that can sort efficiently, but the three most common are: MergeSort QuickSort HeapSort

QuickSort is often the quickest in practice, but somewhat difficult to understand

We will study MergeSort, and you will implement a version on your homework

Page 51: Sorting Algorithms

First, what is “merging”?

Merging two arrays means The two arrays start off sorted After merging, the resulting array contains all of

the elements of the original two arrays, and they’re all sorted.

Page 52: Sorting Algorithms

Merging sub-Arrays that are sorted

-7 -6 8 29 31 -15 -2 0 10 40

Left1 Right1 Left2 Right2

Page 53: Sorting Algorithms

Merging sub-Arrays that are sorted

-7 -6 8 29 31 -15 -2 0 10 40

Left1 Right1 Left2 Right2

-15 -7 -6 -2 0 8 10 29 31 40

Page 54: Sorting Algorithms

Merging sub-Arrays that are sorted

-7 -6 8 29 31 -15 -2 0 10 40

Left1 Right1 Left2 Right2

? ? ? ? ? ? ? ? ? ?

Page 55: Sorting Algorithms

Merging sub-Arrays that are sorted

-7 -6 8 29 31 -15 -2 0 10 40

Left1 Right1 Left2 Right2

? ? ? ? ? ? ? ? ? ?

next

i1 i2

Page 56: Sorting Algorithms

Merging sub-Arrays that are sorted

-7 -6 8 29 31 -15 -2 0 10 40

Left1 Right1 Left2 Right2

-15 ? ? ? ? ? ? ? ? ?

next

i1 i2

Step 1: Check D[i1], D[i2], move smallest to Temp[next]

Page 57: Sorting Algorithms

Merging sub-Arrays that are sorted

-7 -6 8 29 31 -15 -2 0 10 40

Left1 Right1 Left2 Right2

-15 ? ? ? ? ? ? ? ? ?

next

i1 i2

Step 2: Update next, i1, and i2

Page 58: Sorting Algorithms

Merging sub-Arrays that are sorted

-7 -6 8 29 31 -15 -2 0 10 40

Left1 Right1 Left2 Right2

-15 -7 ? ? ? ? ? ? ? ?

next

i1 i2

Repeat!

Page 59: Sorting Algorithms

Merging sub-Arrays that are sorted

-7 -6 8 29 31 -15 -2 0 10 40

Left1 Right1 Left2 Right2

-15 -7 ? ? ? ? ? ? ? ?

next

i1 i2

Repeat!

Page 60: Sorting Algorithms

Merging sub-Arrays that are sorted

-7 -6 8 29 31 -15 -2 0 10 40

Left1 Right1 Left2 Right2

-15 -7 -6 ? ? ? ? ? ? ?

next

i1 i2

Repeat!

Page 61: Sorting Algorithms

Merging sub-Arrays that are sorted

-7 -6 8 29 31 -15 -2 0 10 40

Left1 Right1 Left2 Right2

-15 -7 -6 -2 ? ? ? ? ? ?

next

i1 i2

Repeat!

Page 62: Sorting Algorithms

Merging sub-Arrays that are sorted

-7 -6 8 29 31 -15 -2 0 10 40

Left1 Right1 Left2 Right2

-15 -7 -6 -2 0 ? ? ? ? ?

next

i1 i2

Repeat!

Page 63: Sorting Algorithms

Merging sub-Arrays that are sorted

-7 -6 8 29 31 -15 -2 0 10 40

Left1 Right1 Left2 Right2

-15 -7 -6 -2 0 8 ? ? ? ?

next

i1 i2

Repeat!

Page 64: Sorting Algorithms

Merging sub-Arrays that are sorted

-7 -6 8 29 31 -15 -2 0 10 40

Left1 Right1 Left2 Right2

-15 -7 -6 -2 0 8 10 29 31 40

next

i1 i2

Repeat!

Page 65: Sorting Algorithms

Exercise: The Merge Algorithm# input array D, with two sorted, adjacent sub-arrays# from left1 to right1, from left2 to right2

# output array D, with one sorted sub-array# from left1 to right2

Page 66: Sorting Algorithms

Exercise: The Merge Algorithmfunction merge(D, left1, right1, left2, right2):

Temp = an array big enough to hold both subarraysi1 = left1, i2 = left2, iTemp = 0while i1 <= right1 and i2 <= right2:

if D[i1] < D[i2]:Temp[iTemp] = D[i1]increment i1, iTemp

else:Temp[iTemp] = D[i2]increment i2, iTemp

while i1<=right1: # for when i2 finishes firstTemp[iTemp] = D[i1]increment i1, iTemp

while i2<=right2: # for when i1 finishes firstTemp[iTemp] = D[i2]increment i2, iTemp

copy Temp[0 to length-1] to D[left1 to right2]

Page 67: Sorting Algorithms

Using Merge to Sort

-29 40 8 -2 -15 10 -6 -7 31 0

Page 68: Sorting Algorithms

Using Merge to Sort

-29 40 8 -2 -15 10 -6 -7 31 0

Step 1: Merge first two elements.

Page 69: Sorting Algorithms

Using Merge to Sort

-29 40 8 -2 -15 10 -6 -7 31 0

Step 2: Merge second two elements.

Page 70: Sorting Algorithms

Using Merge to Sort

-29 40 -2 8 -15 10 -6 -7 31 0

Step 2: Merge second two elements.

Page 71: Sorting Algorithms

Using Merge to Sort

-29 40 -2 8 -15 10 -6 -7 31 0

Step 3: Merge third two elements.

Page 72: Sorting Algorithms

Using Merge to Sort

-29 40 -2 8 -15 10 -6 -7 31 0

Step 4: Merge fourth two elements.

Page 73: Sorting Algorithms

Using Merge to Sort

-29 40 -2 8 -15 10 -7 -6 31 0

Step 4: Merge fourth two elements.

Page 74: Sorting Algorithms

Using Merge to Sort

-29 40 -2 8 -15 10 -7 -6 31 0

Step 5: Merge fifth two elements.

Page 75: Sorting Algorithms

Using Merge to Sort

-29 40 -2 8 -15 10 -7 -6 0 31

Step 5: Merge fifth two elements.

Page 76: Sorting Algorithms

Using Merge to Sort

-29 40 -2 8 -15 10 -7 -6 0 31

End phase 1.

Notice: we now have 5 sub-arrays of length 2, each of which is sorted.

Phase 2 will be: merge sub-arrays of length 2.

Page 77: Sorting Algorithms

Using Merge to Sort

-29 40 -2 8 -15 10 -7 -6 0 31

Phase 2, step 1: merge the first two and second two elements

Page 78: Sorting Algorithms

Using Merge to Sort

-29 -2 8 40 -15 10 -7 -6 0 31

Phase 2, step 1: merge the first two and second two elements

Page 79: Sorting Algorithms

Using Merge to Sort

-29 -2 8 40 -15 10 -7 -6 0 31

Phase 2, step 2: merge the third two and fourth two elements

Page 80: Sorting Algorithms

Using Merge to Sort

-29 -2 8 40 -15 -7 -6 10 0 31

Phase 2, step 2: merge the third two and fourth two elements

Page 81: Sorting Algorithms

Using Merge to Sort

-29 -2 8 40 -15 -7 -6 10 0 31

End phase 2.

(The fifth two elements have nothing to merge with, so we leave those alone for now.)

Notice: we now have two sub-arrays of length 4, which are sorted.

Phase 3 will merge these.

Page 82: Sorting Algorithms

Using Merge to Sort

-29 -2 8 40 -15 -7 -6 10 0 31

Phase 3, step 1: Merge the first 4 and second 4 elements.

Page 83: Sorting Algorithms

Using Merge to Sort

-29 -15 -7 -6 -2 8 10 40 0 31

Phase 3, step 1: Merge the first 4 and second 4 elements.

Page 84: Sorting Algorithms

Using Merge to Sort

-29 -15 -7 -6 -2 8 10 40 0 31

Finally, phase 4: Merge first 8 and last 2 elements.

Page 85: Sorting Algorithms

Using Merge to Sort

-29 -15 -7 -6 -2 0 8 10 31 40

Done.

Page 86: Sorting Algorithms

Exercise: The MergeSort Algorithm# input array D, output sorted D

mergeSize = 1while mergeSize < length of D:left1 = 0while left1 + mergeSize < length of D:

right1 = left1 + mergeSize - 1left2 = left1 + mergeSizeright2 = the smaller of

left1 + 2*mergeSize-1, length of D-1merge(D,left1,right1,left2,right2)left1 = right2 + 1

mergeSize = mergeSize * 2

Page 87: Sorting Algorithms

What you’re expected to know Know how to implement the Comparable interface, and use the

Arrays.sort() and Arrays.binarySearch() methods. Know the requirements for all algorithms

Linear search, bubble sort, merge sort: none Binary search: whole array must be sorted Merge: each sub-array must be sorted

I expect you to know the search algorithms Memorize or understand well enough to reproduce on a quiz or exam

You DO NOT need to know the algorithms for bubble sort, merge, or merge sort by heart.

You DO need to be able to simulate an iteration of each algorithm on a quiz or exam.

If I give you the algorithm for one of these, I expect you to be able to write the code.