Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Algorithms Ellysa N. Kosinaya.

Similar presentations


Presentation on theme: "Sorting Algorithms Ellysa N. Kosinaya."— Presentation transcript:

1 Sorting Algorithms Ellysa N. Kosinaya

2 Overview Icebreaker KLA: The “Human Sorting”
Summary of the 4 sorting algorithms

3 icebreaker Need 6-8 volunteers Birthday sort Height sorting

4 Kla: Human sorting Divide up to 4 groups
Each group perform a specific sorting algorithm (e.g. bubble, selection, merge, quick) similar to icebreaker. Discuss the time complexities (i.e. best, worst, average)

5 summary Selection Sort Bubble Sort
Merge Sort (Divide-and-Conquer method) Quick Sort (Divide-and-Conquer method)

6 Selection Sort Strategy
Scan whole list to find smallest/largest element and place item in correct final position (e.g. smallest in first index, largest in last index) Scan for next smallest/largest element among last n – 1 elements and place in correct final position Repeat until list is sorted (i.e. after n – 1 passes)

7 Selection Sort Example
| n=5 1 | n | n | | |

8 Selection Sort Analysis

9 Bubble Sort Strategy Traversing a collection of elements
Compare adjacent elements of the list and swapping them if out of order Doing it repeatedly, end up “bubbling up” the largest element to the last position on the list

10 Bubble Sort Example (2 comp) (4 comp) (1 comp) (3 comp)

11 Bubble Sort Analysis Time complexity is O(n2) for all cases – best case, worst case, or average case Not a practical sorting algorithm when n is large.

12 Merge Sort Strategy Divide and conquer method
Given an array with n items (let n be a power of 2): Divide the array into two subarrays each with n/2 items. Conquer (solve) each subarray by sorting it. Unless the array is sufficiently small, use recursion to do this. Combine the solutions to the subarrays by merging them into a single sorted array.

13 Merge Sort Example

14 Merge Sort Analysis

15 Quick Sort Strategy Divide and conquer method
Similar to merge sort; however, it divides its input’s elements according to their value in the array Partition is used; where all elements before a pivot point s is smaller than or equal to that pivot and all the elements after the pivot point s is larger than or equal to that pivot. A[0] … A[s – 1] A[s] A[s + 1] … A[n – 1] all are ≤ A[s] all are ≥ A[s]

16 Quick Sort Example

17 Quick Sort Analysis Worst-Case: O(n2). Call Partition O(n) times, each time takes O(n) steps. So O(n2), and it is worse than Merge Sort in the Worst-Case. Best-Case & Average-Case: O(nLogn). Split the list evenly each time.


Download ppt "Sorting Algorithms Ellysa N. Kosinaya."

Similar presentations


Ads by Google