L6 - Sorting (4 on 1) - Lecture Outline Sorting Bring order...

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

View Full Document Right Arrow Icon
Sorting Bring order to the world 2 --- 74 [CS1102C AY0708S1 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 AY0708S1 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 4 --- 74 [CS1102C AY0708S1 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 1

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

View Full DocumentRight Arrow Icon
Selection Sort 6 --- 74 [CS1102C AY0708S1 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 AY0708S1 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 8 --- 74 [CS1102C AY0708S1 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 2
9 --- 74 [CS1102C AY0708S1 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 Bubble Sort 11 --- 74 [CS1102C AY0708S1 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 12 --- 74 [CS1102C AY0708S1 Lecture 6] Bubble Sort : Illustration x x Sorted Item Pair of items under comparison
Background image of page 3

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

View Full DocumentRight Arrow Icon
13 --- 74 [CS1102C AY0708S1 Lecture 6] Bubble Sort : Code void bubbleSort (int a[], int N) { for (int i = 0; i < N; ++i) { for (int j = 1; j < N - i; ++j) { if (a[j-1] > a[j]) { int temp = a[j-1] ; a[j-1] = a[j] ; a[j] = temp ; } } } } 13 37 14 10 29 Step 2. Swap if the items are out of order Step 1 .
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/02/2009 for the course ECE CS1102C taught by Professor Dr.tansuntuck during the Spring '07 term at National University of Singapore.

Page1 / 19

L6 - Sorting (4 on 1) - Lecture Outline Sorting Bring order...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online