sorting algorithms n 2 sorts ◦selection sort ◦insertion sort ◦bubble sort better sorts...

33
Sorting Algorithms Sorting Algorithms n 2 Sorts Selection Sort Insertion Sort Bubble Sort Better Sorts Merge Sort Quick Sort Radix Sort

Upload: moris-chapman

Post on 18-Dec-2015

261 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

Sorting AlgorithmsSorting Algorithms

n2 Sorts◦Selection Sort◦Insertion Sort◦Bubble Sort

Better Sorts◦Merge Sort◦Quick Sort◦Radix Sort

Page 2: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

Radix SortRadix Sort

Sorts all values as StringsLoop thru the Strings backwards,

arranging Stings in sets based on the character being compared.

Reload the array from the sets.

Page 3: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

weiner, bankemper, caldwell, goldston, rechtin, rhein, rust, walters, whelan

{ weiner, caldwell, goldston, rechtin, rhein, rust, walters, whelan }{ bankemper }weiner, caldwell, goldston, rechtin, rhein, rust, walters, whelan, bankemper

{ weiner, rechtin, rhein, rust, walters, whelan }{ bankemper }{ caldwell }{ goldston }weiner, rechtin, rhein, rust, walters, whelan, bankemper, caldwell, goldston

{ weiner, rhein, rust, whelan }{ caldwell }{ rechtin }{ goldston }{ bankemper }{ walters }weiner, rhein, rust, whelan, caldwell, rechtin, goldston, bankemper, walters

Page 4: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

weiner, rhein, rust, whelan, caldwell, rechtin, goldston, bankemper, walters

{ rhein, rust }{ caldwell }{ rechtin }{ bankemper }{ whelan }{ weiner, walters }{ goldston }rhein, rust, caldwell, rechtin, bankemper, whelan, weiner, walters, goldston

{ rust }{ whelan}{ bankemper, weiner, walters }{ rhein }{ goldston }{ rechtin }{ caldwell }rust, whelan, bankemper, weiner, walters, rhein, goldston, rechtin, caldwell

Page 5: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

rust, whelan, bankemper, weiner, walters, rhein, goldston, rechtin, caldwell

{ goldston, caldwell }{ rechtin }{ rhein }{ bankemper }{ whelan }{ weiner }{ rust, walters }goldston, caldwell, rechtin, rhein, bankemper, whelan, weiner, rust, walters

{ rechtin }{ rhein, whelan }{ weiner }{ goldston, caldwell, walters }{ bankemper }{ rust }rechtin, rhein, whelan, weiner, goldston, caldwell, walters, bankemper, rust

Page 6: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

rechtin, rhein, whelan, weiner, goldston, caldwell, walters, bankemper, rust

{ caldwell, walters, bankemper }{ rechtin, weiner }{ rhein, whelan }{ goldston }{ rust }caldwell, walters, bankemper, rechtin, weiner, rhein, whelan, goldston, rust

{ bankemper }{ caldwell }{ goldston }{ rechtin, rhein, rust }{ walters, weiner, whelan }

bankemper, caldwell, goldston, rechtin, rhein, rust, walters, weiner, whelan

Page 7: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

Merge SortMerge Sort

One of the Divide-And-Conquer algorithmsSplit the array in half, sort each half, then

merge the two halves together.The trick is to call it recursively, splitting

the array in half each time, until you get one element in each half, then merge them into proper sequence.

Page 8: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

69, 68, 19, 84, 60, 1, 23, 35, 10, 37, 16

69, 68, 19, 84, 60, 1 23, 35, 10, 37, 16

69, 68, 19 84, 60, 1

69, 68 19

23, 35, 10 37, 16

84, 60 1 23, 35 10 37 16

69 68 84 60 23 35

68, 69 19 60, 84 1 23, 35 10 37 16

19, 68, 69 1, 60, 84 10, 23, 35 16, 37

1, 19, 60, 68, 69, 84 10, 16, 23, 35, 37

1, 10, 16, 19, 23, 35, 37, 60, 68, 69, 84

Page 9: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

Merge Sort AlgorithmMerge Sort Algorithm

Find the mid-point of the array I'm responsible for.

Mergesort the low half of the array.Mergesort the high half of the array.Merge the two halves back together.

Page 10: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

Merge AlgorithmMerge Algorithm

Starting from the first element of each array section, find the lowest of the two values, and move it to the final position.

Incrementing the pointer in the array section that just lost a value, find the new lowest of the two, and move it.

When all of the values in one array section have been moved, move all the remaining values in the other section.

Page 11: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

QuicksortQuicksort

Another Divide-And-Conquer algorithm.Generally considered to be the fastest

sorting algorithm available, but...Average cases are O(n log n)Two cases move it towards O(n2)

◦Small n◦When list is already close to sorted.

Page 12: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

Quicksort AlgorithmQuicksort Algorithm

1. Partition the portion of the array I'm responsible for.

2. Quicksort the low part of the array.

3. Quicksort the high part of the array.

Page 13: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

Partition AlgorithmPartition Algorithm

Select a Pivot value.Move up the array, stopping at the first value

greater than the Pivot value.Move down the array, stopping at the first value less than the Pivot value.

Swap the two values.Repeat until the counter moving up the array

crosses the counter moving down the array.Finally, swap the pivot value with the value at the

down counter.By the time you're done, all the values < than

Pivot are in the left part of the array and all the values > than Pivot are in the right part of the array.

Page 14: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

6968 19 84 60 1 23 35 1037 16

Page 15: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

6968 19 84 60 1 23 35 1037 16

Pivot Value = 37

Page 16: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

6968 19 84 60 1 23 35 1037 16

Pivot Value = 37

Page 17: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 84 60 1 23 35 1037 16

Pivot Value = 37

Page 18: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 84 60 1 23 35 1037 16

Pivot Value = 37

Page 19: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 84 60 1 23 35 1037 16

Pivot Value = 37

Page 20: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 84 60 1 23 35 1037 16

Pivot Value = 37

Page 21: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 84 60 1 23 35 1037 16

Pivot Value = 37

Page 22: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 8460 1 23 351037 16

Pivot Value = 37

Page 23: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 8460 1 23 351037 16

Pivot Value = 37

Page 24: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 8460 1 23 351037 16

Pivot Value = 37

Page 25: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 84601 23351037 16

Pivot Value = 37

Page 26: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 84601 23351037 16

Pivot Value = 37

Page 27: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 84601 23351037 16

Pivot Value = 37

Page 28: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 84601 23351037 16

Pivot Value = 37

Page 29: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 84601 23351037 16

Pivot Value = 37

Page 30: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 8460123 3510 3716

Pivot Value = 37

Page 31: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 8460123 3510 3716

Pivot Value = 37

Page 32: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

A Partition VisualizationA Partition Visualization

69 6819 8460123 3510 3716

Pivot Value = 37

Page 33: Sorting Algorithms n 2 Sorts ◦Selection Sort ◦Insertion Sort ◦Bubble Sort Better Sorts ◦Merge Sort ◦Quick Sort ◦Radix Sort

Comparison of Sorting Comparison of Sorting AlgorithmsAlgorithms