Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 17 17 15 1

2 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) 2

3 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

4 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

5 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

6 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

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

8 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

9 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

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

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

12 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

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

14 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 ? ? ? 3 5 3 5 3 // num1 and num2 remain the same! 14

15 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

16 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 num2 3 5 3 // num1 and num2 are exchanged! 16

17 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

18 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

19 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

20 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

21 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

22 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

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

24 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

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

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


Download ppt "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."

Similar presentations


Ads by Google