Application of arrays, sorting and searching

Application of - CMPT 128 Introduction to Computing Science for Engineering Students Applications of Arrays Sorting and Searching Janice Regan CMPT

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

View Full Document Right Arrow Icon
1 0 © 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 1 Putting numbers into order D There are many approaches to taking an array of numbers and putting those numbers into ascending or descending order. 6 Insertion Sort 6 Selection Sort 6 Bubble Sort 6 Quick Sort
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 © Janice Regan, CMPT 128, February 2007 2 Selection Sort D 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. D 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 z 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 3 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]
Background image of page 2
3 © Janice Regan, CMPT 128, February 2007 4 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 5 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
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 © Janice Regan, CMPT 128, February 2007 6 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 7 Sample Selection Sort: 4 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 19 12 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[3] Iteration 4: N=M-3 3 5 7 19 12 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 4: N=M-3 3 5 7 12 19 75 46 95 37 86 16 23 32
Background image of page 4
5 © Janice Regan, CMPT 128, February 2007 8
Background image of page 5

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

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

This note was uploaded on 12/05/2010 for the course CS 128 taught by Professor Janiceregan during the Fall '10 term at Simon Fraser.

Page1 / 29

Application of - CMPT 128 Introduction to Computing Science for Engineering Students Applications of Arrays Sorting and Searching Janice Regan CMPT

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

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