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

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

View Full Document Right Arrow Icon
Sorting Bring order to the world
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
Selection Sort
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
Bubble Sort
Background image of page 10
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
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 --- 74 [CS1102C AY0708S2 Lecture 6] Bubble Sort : Illustration x x Sorted Item Pair of items under comparison
Background image of page 12