Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

48
Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples

description

Selection Sort 3 Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples

Transcript of Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Page 1: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Review

1

Merge SortMerge Sort AlgorithmTime Complexity

Best caseAverage caseWorst case

Examples

Page 2: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Example

2

Page 3: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

3

Selection SortSelection Sort AlgorithmTime Complexity

Best caseAverage caseWorst case

Examples

Page 4: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection SortThe list is divided into two sublists, sorted and

unsorted, which are divided by an imaginary wall. We find the smallest element from the unsorted

sublist and swap it with the element at the beginning of the unsorted data.

After each selection and swapping, the imaginary wall between the two sublists move one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones.

Each time we move one element from the unsorted sublist to the sorted sublist, we say that we have completed a sort pass.

A list of n elements requires n-1 passes to completely rearrange the data.

Page 5: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort Algorithm1.Scan the array to find its smallest element and

swap it with the first element. 2.Then, starting with the second element, scan the

elements to its right to find the smallest among them and swap it with the second elements.

3.Generally, on pass i (0 i n-2), find the smallest element in A[i..n-1] and swap it with A[i]:

A[0] . . . A[i-1] | A[i], . . . , A[min], . . ., A[n-1]

in their final positions

Page 6: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection SortSelection Sort1. Start with the 1st element, scan the entire list to find its smallest

element and exchange it with the 1st element2. Start with the 2nd element, scan the remaining list to find the the

smallest among the last (N-1) elements and exchange it with the 2nd element

Example: 89 45 68 90 29 34 17 17 | 45 68 90 29 34 89 29 | 68 90 45 34 89 34 | 90 45 68 89 45 | 90 68 89 68 | 90 89 89 | 90 90

Page 7: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

23 78 45 8 32 56

8 78 45 23 32 56

8 23 45 78 32 56

8 23 32 78 45 56

8 23 32 45 78 56

8 23 32 45 56 78

Original List

After pass 1

After pass 2

After pass 3

After pass 4

After pass 5

Sorted Unsorted

Page 8: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort Algorithm for i 0 to N-2 do { min i; for j i+1 to N-1 do { if (A[j] < A[min]) min j; } swap A[i] and A[min] ; }

Page 9: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort -- AnalysisIn general, we compare keys and move items (or

exchange items) in a sorting algorithm (which uses key comparisons). So, to analyze a sorting algorithm we should count the number of key comparisons and the number of moves.

Ignoring other operations does not affect our final result.In selection Sort function, the outer for loop executes

n-1 times.We invoke swap function once at each iteration.

Total Swaps: n-1 Total Moves: 3*(n-1) (Each swap has three moves)

Page 10: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort – Analysis (cont.)

The inner for loop executes the size of the unsorted part minus 1 (from 1 to n-1), and in each iteration we make one key comparison. # of key comparisons = 1+2+...+n-1 = n*(n-1)/2 So, Selection sort is O(n2)

The best case, the worst case, and the average case of the selection sort algorithm are same. all of them are O(n2)This means that the behavior of the selection sort algorithm

does not depend on the initial organization of data.Since O(n2) grows so rapidly, the selection sort algorithm is

appropriate only for small n.Although the selection sort algorithm requires O(n2) key

comparisons, it only requires O(n) moves.A selection sort could be a good choice if data moves are costly

but key comparisons are not costly (short keys, long records).

Page 11: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 12: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 13: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 14: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 15: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 16: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 17: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 18: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Largest

Page 19: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Page 20: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Page 21: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Page 22: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Page 23: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Page 24: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Page 25: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Page 26: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Largest

Page 27: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 28: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 29: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 30: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 31: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 32: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 33: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Largest

Page 34: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 35: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 36: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 37: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 38: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 39: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Largest

Page 40: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 41: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 42: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 43: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 44: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Largest

Page 45: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted

Page 46: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Selection Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted

DONE!

Page 47: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Example

47

Page 48: Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.

Summary

48

Selection SortSelection Sort AlgorithmTime Complexity

Best caseAverage caseWorst case

Examples