Sorting int s[20], size; size = 5; Original array 17 20 17 15 19 Final array (in Ascending Order) 15 17 17 19 20 Final array (in Descending Order) 20 19.

Slides:



Advertisements
Similar presentations
1. List Static List: no adding or deleting Dynamic List: can add or delete items from the list Both static and dynamic lists: linear search, update item.
Advertisements

Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
One Dimensional Arrays
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Bubble Sort Algorithm It is so named because numbers (or letters) which are in the wrong place “bubble-up” to their correct positions (like fizzy lemonade)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Selection Sort -Reading p Reading p
Bubble Sort Notes David Beard CS181. Bubble Sort for Strings Double pass algorithm to sort a single dimensional array. Inner loop “bubbles” largest element.
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
Sorting and Searching. Problem Read in a parameter value n, then read in a set of n numbers. Print the numbers in their original order. Sort the numbers.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Sorting Arrays. Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the.
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
1 Sorting Arrays Chapter Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.
1 Sorting Arrays Chapter Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
CSI1390 – Java Programming Methods II Instructor: Saeid Nourian
STARTING OUT WITH STARTING OUT WITH Class 3 Honors.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Selection Sorting S[] : array of int or float Size: number of elements of s[] Pseudocode for i = 0 to size - 2 find the index of a smallest element between.
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David.
CS 162 Intro to Programming II Sorting Introduction & Selection Sort 1.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Starting Out with C++, 3 rd Edition 1 Sorting Arrays.
Arrays as Function Parameters. CSCE 1062 Outline  Passing an array argument (section 9.3)  Reading part of an array (section 9.4)  Searching and sorting.
12. Searching/Sorting Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Simple Searching Many.
Selection Sorting Pseudocode (Forward) for i = 0 to size - 2 find the index of the required element between s[i] and s[size - 1] If i not the same as index.
Sort Algorithm.
Chapter 9: Sorting and Searching Arrays
CS212: Data Structures and Algorithms
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Engineering Problem Solving with C++, Etter
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
Pointers & Functions.
CS 1430: Programming in C++.
Sorting Arrays Chapter 10
CS 1430: Programming in C++.
CS150 Introduction to Computer Science 1
Pointers & Functions.
CS 1430: Programming in C++.
Selection Sorting S[] : array of int or float
Presentation transcript:

Sorting int s[20], size; size = 5; Original array Final array (in Ascending Order) Final array (in Descending Order)

Selection Sorting Original array (size is 5, s[0] to s[4]) First iteration (round): Make s[0] correct Find the smallest value between s[0] and s[4]: 15 at index 3 Exchange s[0] and [3] Second iteration (round): Make s[1] correct Find the smallest value between s[1] and s[4]: 17 at index 2 Exchange s[1] and [2] Third iteration (round): Make s[2] correct Find the smallest value between s[2] and s[4]: 17 at index 3 Exchange s[2] and [3] Fourth iteration (round): Make s[3] correct Find the smallest value between s[3] and s[4]: 19 at index 4 Exchange s[3] and [4] Fifth iteration (round): Make s[4] correct NOT NEEDED! Final array: (size – 1) iterations (rounds/passes) 2

Selection Sorting Pseudocode int s[MAX_SIZE], size; for i = 0 to size - 2 find the index of a smallest element between s[i] and s[size - 1] If i not the same as index swap s[i] and s[index] 3

Selection Sorting Pseudocode (Use functions) int s[MAX_SIZE], size; for i = 0 to size - 2 find the index of a smallest element between s[i] and s[size - 1] (Call function IndexOfMin) If i not the same as index swap s[i] and s[index] (Call function SwapInt) 4

Selection Sorting // // The function uses Selection Sorting method // to sort an array s[] with size values // in non-descending order. // Parameters: (, ) // void SelectSort(int s[], int size) { int index; for (int i = 0; i < size - 1; i++) { index = IndexOfMin(s, i, size - 1); if (index != i) SwapInt(s[i], s[index]); } return; } // int s[]: In, Out, InOut? // int size: In, Out, InOut? 5

Be Carefull! // // The function uses Selection Sorting method // to sort an array s[] with size values // in non-descending order. // Parameters: (, ) // void SelectSort(int s[], int size) { int index; //for (int i = 0; i < size; i++) for (int i = 0; i < size - 1; i++) { //index = IndexOfMin(s, size); index = IndexOfMin(s, i, size - 1); if (index != i) SwapInt(s[i], s[index]); } return; } 6

// // The function finds and returns the index of the // first smallest value in s[], which has // size values. // Parameter: (in, in) // int IndexOfMin(const int s[], int size) { int index = 0; for (int i = 1; i < size; i ++) if (s[i] < s[index]) index = i; return index; } // Min over all array elements! 7

Function IndexOfMin // // The function finds and returns the index of the // first smallest element between s[first] and // s[last], assuming first < last. // Parameters: ( In, In, In ) // int IndexOfMin(const int s[], int first, int last) { int index = first; for (int i = first + 1; i <= last; i++) { if (s[i] < s[index]) index = i; } return index; } 8

Be Carefull! // // The function finds and returns the index of the // first smallest element between s[first] and // s[last], assuming first < last. // Parameters: ( In, In, In ) // int IndexOfMin(const int s[], int first, int last) { //int index = 0; int index = first; //for (int i = 1; i < size; i++) for (int i = first + 1; i <= last; i++) { if (s[i] < s[index]) index = i; } return index; } 9

Function SwapInt // // The function exchanges the values // of two integers. // Parameters: (, ) // void SwapInt(int x, int y) { x = y; y = x; } 10

Tracing void SwapInt(int x, int y) { x = y; y = x; } x y // They are the same! 11

Function SwapInt // // The function exchanges the values of two integers. // Parameters: (, ) // void SwapInt(int x, int y) { int temp; temp = x; x = y; y = temp; } 12

Tracing void SwapInt(int x, int y) { int temp; temp = x; x = y; y = temp; } x y temp 3 5 ? // Values are exchanged! 13

Tracing int main() { int num1 = 3, num2 = 5; SwapInt(num1, num2); return 0; } void SwapInt(int x, int y) { int temp; temp = x; x = y; y = temp; } main() SwapInt num1 num2 x y temp 3 5 ? ? ? // num1 and num2 remain the same! 14

Function SwapInt // // The function exchanges the values of two integers. // Parameters: ( InOut, InOut ) // void SwapInt(int& x, int& y) { int temp = x; x = y; y = temp; } 15

Tracing int main() { int num1 = 3, num2 = 5; SwapInt(num1, num2); return 0; } void SwapInt(int& x, int& y) { int temp; temp = x; x = y; y = temp; } main() SwapInt num1 num2 x y temp 3 5 ? ? ? add of add of num1 num // num1 and num2 are exchanged! 16

Selection Sorting void SwapInt(int& x, int& y); int IndexOfMin(const int s[], int first, int last); // // The function uses Selection Sorting method to sort // s[] in non-descending order. // Params: (InOut, In) // void SelectSort(int s[], int size) { int index; for (int i = 0; i < size - 1; i++) { index = IndexOfMin(s, i, size - 1); if (index != i) SwapInt(s[i], s[index]); } return; } 17

void SwapInt(int& x, int& y); int IndexOfMin(const int s[], int first, int last); void SelectSort(int s[], int size); int main() { int scores[30], size; ReadArray(scores, size); cout << endl << "The scores before sorting: "; DisplayScores(scores, size); SelectSort(scores, size); cout << endl << "The scores after sorting: "; DisplayScores(scores, size); return 0; } 18

Selection Sorting int size; float s[MAX_SIZE]; // Same as int s[] Pseudocode for i = 0 to size - 2 find the index of a smallest element between s[i] and s[size - 1] If i not the same as index swap s[i] and s[index] 19

Selection Sorting Exchange at most once for each position! Not bubble sorting! for i = 0 to size - 2 find the index of a smallest element between s[i] and s[size - 1] If i not the same as index swap s[i] and s[index] 20

Selection Sorting without Functions void SelectSort(int s[], int size) { int index, temp; for (int i = 0; i < size - 1; i++) { //index = IndexOfMin(s, i, size - 1); index = i; for (int j = i + 1; j < size; j++) { if (s[j] < s[index]) index = i; } // Swap if (index != i) { temp = s[index]; s[index] = s[i]; s[i] = temp; } } return; } 21

bubble sorting void SelectSort(int s[], int size) { int index, temp; for (int i = 0; i < size - 1; i++) { for (int j = i + 1; j < size; j++) { if (s[j] < s[j - 1]) { temp = s[j]; s[j] = s[j - 1]; s[j - 1] = temp; } } } return; } Do NOT use bubble sorting! 22

Prog6 Must Do it in Pairs Signup 3pm Today! 23

Prog6 Change C++ string to C string class Player { private: char firstName[11], lastName[11];... public: string getFirst()? string getLast()?... } How to pass a c string back? 24

class Player { private: char firstName[11], lastName[11];... public: void getFirst(char first[]) { first = firstName; //NO! strcpy(first, firstName); // Yes! } // same for getLast()... } 25

// In other classes cout << p.getFirst() << “ ” << p.getLast(); char first[11], last[11]; p.getFirst(first); p.getLast(last); cout << first << “ ” << last; 26