Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1430: Programming in C++.

Similar presentations


Presentation on theme: "CS 1430: Programming in C++."— Presentation transcript:

1 CS 1430: Programming in C++

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

3 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: 4 (size – 1) iterations (rounds/passes)

4 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]

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

6 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?

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

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

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

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

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

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

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

14 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 // Values are exchanged!

15 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 // num1 and num2 remain the same!

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

17 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 ? ? ? add of add of num1 num2 3 5 // num1 and num2 are exchanged!

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

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

20 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]

21 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]

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

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

24 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.


Download ppt "CS 1430: Programming in C++."

Similar presentations


Ads by Google