lecture18 Array Applications Sorting & Searching

lecture18 Array Applications Sorting & Searching - 1 ©...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 © Janice Regan, CMPT 128, Feb. 2007 CMPT 128: Introduction to Computing Science for Engineering Students Applications of Arrays Sorting and Searching © Janice Regan, CMPT 128, February 2007 2 Putting numbers into order There are many approaches to taking an array of numbers and putting those numbers into ascending or descending order. Insertion Sort Selection Sort Bubble Sort Quick Sort © Janice Regan, CMPT 128, February 2007 3 Selection Sort A method to transform the unsorted array into a sorted array. This example sorts into ascending order, sorting into descending order is a parallel process. Apply a simple algorithm to an array of length M 1. Start with N=M 2. Consider the last N elements in the array, A[M-N] to A[M-1] 3. Find the element that contains the smallest value in the group of elements defined in (2). Record the index k at which this smallest value is found. Smallest = A[k] 4. Swap the smallest value with the (M-N)th element A[M-N] swapped with A[k] the smallest element 5. Decrease N by 1 and return to step 2 (until N<=0) © Janice Regan, CMPT 128, February 2007 4 Ascending / Descending Order 23 3 12 19 7 75 46 95 37 86 16 5 32 Unsorted Array A[13] A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[11] A[10] A[12] 95 86 75 46 37 32 23 19 16 12 5 7 3 Sorted Array A[13]: Descending order A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[11] A[10] A[12] 3 5 7 12 16 19 23 32 37 46 86 75 95 Sorted Array A[13]: Ascending order A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[11] A[10] A[12] © Janice Regan, CMPT 128, February 2007 5 Sample Selection Sort: 1 23 3 12 19 7 75 46 95 37 86 16 5 32 Unsorted Array A[13] A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[11] A[10] A[12] 23 3 12 19 7 75 46 95 37 86 16 5 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[11] A[10] A[12] Smallest element is A[1], switch with A[M-N]=A[0] Iteration 1: N=M 3 23 12 19 7 75 46 95 37 86 16 5 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[11] A[10] A[12] After Iteration 1: N=M © Janice Regan, CMPT 128, February 2007 6 Sample Selection Sort: 2 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[11] A[10] A[12] 3 23 12 19 7 75 46 95 37 86 16 5 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[11] A[10] A[12] Smallest element is A[10], switch with A[M-N]=A[1] Iteration 2: N=M-1 3 23 12 19 7 75 46 95 37 86 16 5 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[11] A[10] A[12] After Iteration 2: N=M-1 3 5 12 19 7 75 46 95 37 86 16 23 32 © Janice Regan, CMPT 128, February 2007 7 Sample Selection Sort: 3 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[11] A[10] A[12] 3 5 12 19 7 75 46 95 37 86 16 23 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[11] A[10] A[12] Smallest element is A[4], switch with A[M-N]=A[2] Iteration 3: N=M-2 3 5 12 19 7 75 46 95 37 86 16 23 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[11] A[10] A[12] After Iteration 3: N=M-2 3 5 7 19 12 75 46 95 37 86 16 23 32 © Janice Regan, CMPT 128, February 2007...
View Full Document

This note was uploaded on 05/18/2010 for the course CMPT 128 taught by Professor Regan during the Spring '08 term at Simon Fraser.

Page1 / 58

lecture18 Array Applications Sorting & Searching - 1 ©...

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

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