L6_-_Sorting_(1_on_1)

# L6_-_Sorting_(1_on_1) - Sorting Bring order to the world...

This preview shows pages 1–14. Sign up to view the full content.

Sorting Bring order to the world

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 --- 74 [CS1102C AY0708S2 Lecture 6] Lecture Outline ± Iterative sort algorithms (comparison based) ² Selection Sort ² Bubble Sort ² Insertion Sort ± Recursive sort algorithms (comparison based) ² Merge Sort ² Quick Sort ± Radix sort (non-comparison based) ± Properties of Sorting ² In-place sort, stable sort ² Comparison of sort algorithms Note : we only consider sorting data in ascending order
3 --- 74 [CS1102C AY0708S2 Lecture 6] Why study sorting ± When an input is sorted, many problems becomes easy (eg. searching, min, max, k th smallest , . ..). ± Sorting has a variety of interesting algorithmic solutions that embody many ideas: ² Iterative ² Recursive ² Divide-and-Conquer ² Best/Worst/Average-case bounds ² Randomized Algorithms

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 --- 74 [CS1102C AY0708S2 Lecture 6] Sorting Applications ± Uniqueness testing ± Deleting duplicates ± Prioritizing events ± Frequency counting ± Reconstructing the original order ± Set intersection/union ± Finding a target pair x , y such that x+y = z ± Efficient searching
Selection Sort

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 --- 74 [CS1102C AY0708S2 Lecture 6] Selection Sort : Idea ± Given an array of N items: 1. Find the largest item M , in the range of [0…N-1] 2. Swap M with the (N-1) th item 3. Go to step 1, reduce N by 1
7 --- 74 [CS1102C AY0708S2 Lecture 6] Selection Sort : Illustration 13 37 14 10 29 37 13 14 10 29 37 29 14 10 13 37 29 14 10 13 37 29 14 13 10 x x x Unsorted items Largest item for current iteration Sorted items

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 --- 74 [CS1102C AY0708S2 Lecture 6] Selection Sort : Implementation void selectionSort(int a[], int N) { for (int i = N-1; i>=1; --i) { int maxIdx = i; for (int j=0; j<i; ++j) { if (a[j] >= a[maxIdx]) maxIdx = j; } int temp = a[maxIdx] ; a[maxIdx] = a[i] ; a[i] = temp ; } } Step 1. Searching for Maximum element Step 2. Swap maximum element with the last item 13 37 14 10 29 Trace the execution here
9 --- 74 [CS1102C AY0708S2 Lecture 6] Selection Sort : Analysis •c 1 and c 2 = cost of stmts in outer and inner block Data movement is minimal void selectionSort (int a[], int N) { for (int i = N-1; i>=1; --i) { int maxIdx = i; for (int j=0; j<i; ++j) { if (a[j] >= a[maxIdx]) maxIdx = j; } SWAP(); } } ± n-1 ± n-1 ± (n-1)+(n-2)+…+1 = n(n-1)/2 ± n-1 Total = c 1 (n-1) + c 2 *n*(n-1)/2 = O(n 2 ) Number of times executed

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Bubble Sort
11 --- 74 [CS1102C AY0708S2 Lecture 6] Bubble Sort : Idea ± Given an array of n items 1. Compare pair of adjacent items 2. Swap if the items are out of order 3. Repeat until the end of array ² The largest item will be at the last position 4. Go to step 1 with n reduced by 1 ± Analogy: ² Large item is like “bubble” that floats to the end of the array

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
12 --- 74 [CS1102C AY0708S2 Lecture 6] Bubble Sort : Illustration x x Sorted Item Pair of items under comparison