CS 1430: Programming in C++.

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
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)
Sorting CMSC 201. Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!
Sorting int s[20], size; size = 5; Original array Final array (in Ascending Order) Final array (in Descending Order)
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.
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.
CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
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.
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,
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
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.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015.
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.
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.
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Chapter 9: Sorting and Searching Arrays
CS212: Data Structures and Algorithms
Data Structures I (CPCS-204)
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++, Etter
Sorting Algorithms.
Sorting Algorithms: Selection, Insertion and Bubble
CS 1430: Programming in C++.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
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.
Selection Sort Sorted Unsorted Swap
CS 2430 Object Oriented Programming and Data Structures I
CS Two Basic Sorting Algorithms Review Exchange Sorting Merge Sorting
Pointers & Functions.
Sorting … and Insertion Sort.
CS 1430: Programming in C++.
Sorting Arrays Chapter 10
Sorting Damian Gordon.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pointers & Functions.
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
Introduction to Sorting Algorithms
CSE 373 Sorting 2: Selection, Insertion, Shell Sort
Insertion Sort Array index Value Insertion sort.
CS148 Introduction to Programming II
CS 1430: Programming in C++.
Module 8 – Searching & Sorting Algorithms
Selection Sorting S[] : array of int or float
Presentation transcript:

CS 1430: Programming in C++

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

Selection Sorting Original array (size is 5, s[0] to s[4]) 17 20 17 15 19 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] 15 20 17 17 19 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] 15 17 20 17 19 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] 15 17 17 20 19 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] 15 17 17 19 20 Fifth iteration (round): Make s[4] correct NOT NEEDED! Final array: 15 17 17 19 20 4 (size – 1) iterations (rounds/passes)

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]

Pseudocode (Use functions) 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)

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?

Be Carefull! //---------------------------------------------- // The function uses Selection Sorting method // to sort an array s[] with size values // in non-descending order. // Parameters: ( InOut , In ) 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;

//------------------------------------------------ // 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!

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;

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;

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

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

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; }

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

Tracing main() SwapInt num1 num2 x y temp 3 5 ? ? ? 3 5 3 5 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 ? ? ? 3 5 3 5 // num1 and num2 remain the same!

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; }

Tracing int main() main() SwapInt { num1 num2 x y temp 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 num2 3 5 // num1 and num2 are exchanged!

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;

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: "; return 0; }

Selection Sorting Pseudocode 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]

Exchange at most once for each position! 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]

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]) } // Swap if (index != i) temp = s[index]; s[index] = s[i]; s[i] = temp; return;

Do NOT use bubble sorting for Prog6! 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 for Prog6!

Let me know if you want to change your partner. Prog6 Must Do it in Pairs Let me know if you want to change your partner.