lecture09_4on1 - CS1010E Lecture 9 Problem Solving With One...

Info iconThis preview shows pages 1–3. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS1010E Lecture 9 Problem Solving With One Dimensional Arrays Henry Chia hchia@comp.nus.edu.sg Semester 1 2011 / 2012 Department of Computer Science School Of Computing National University Of Singapore CS1010E Lecture 9 – p.1/29 Lecture Outline Selection sort Searching Linear search Binary search Random number generation Integer sequences Floating-point sequences Array of Structures CS1010E Lecture 9 – p.2/29 Sorting Algorithms Sorting a group of data values is an operation that is routinely used when analyzing massive amounts of data. Sorting is typically used as a pre-processing step for the following tasks: Counting occurrences of similar data. Finding duplicate values. Efficient search for a particular data. We will present the selection sort algorithm. CS1010E Lecture 9 – p.3/29 Selection Sort Find the minimum value and exchange it with the value in the first position in the array. Find the minimum value beginning with the second element, and exchange with the second element. This process continues until reaching the next-to-last element. At this point, the entire array of values will be in ascending order. CS1010E Lecture 9 – p.4/29 Selection Sort Original order: 5 3 12 8 1 9 Exchange the minimum with the value in the first position: 1 3 12 8 5 9 Exchange the minimum with the value in the second position: 1 3 12 8 5 9 Exchange the minimum with the value in the third position: 1 3 5 8 12 9 CS1010E Lecture 9 – p.5/29 Selection Sort Exchange the minimum with the value in the fourth position: 1 3 5 8 12 9 Exchange the minimum with the value in the fifth position: 1 3 5 8 9 12 Array values are now in ascending order! CS1010E Lecture 9 – p.6/29 Selection Sort void selectionSort( int list[], int numElem) { int i, j, minIndex, temp; for (i = 0; i < numElem-1; i++) { minIndex = i; for (j = i+1; j < numElem; j++) if (list[j] < list[minIndex]) minIndex = j; if (minIndex != i) // to avoid redundant swaps { temp = list[minIndex]; list[minIndex] = list[i]; list[i] = temp; } } return ; } CS1010E Lecture 9 – p.7/29 Selection Sort In general, a selection sort technique requires locating an extremum (maximum or minimum value) and exchanging it with a value at an extreme (rightmost or leftmost) end....
View Full Document

This note was uploaded on 12/13/2011 for the course ELECTRICAL CS1010E taught by Professor Henrytan during the Spring '11 term at National University of Singapore.

Page1 / 8

lecture09_4on1 - CS1010E Lecture 9 Problem Solving With One...

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

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