Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Algorithms.

Similar presentations


Presentation on theme: "Sorting Algorithms."— Presentation transcript:

1 Sorting Algorithms

2 Bubble Sort 7 2 3 8 9 1 1st loop

3 Bubble Sort 2 7 3 8 9 1 1st loop

4 Bubble Sort 2 3 7 8 9 1 1st loop

5 Bubble Sort 2 3 7 8 9 1 1st loop

6 Bubble Sort 2 3 7 8 9 1 1st loop

7 Bubble Sort 2 3 7 8 1 9 2nd loop

8 Bubble Sort 2 3 7 8 1 9 2nd loop

9 Bubble Sort 2 3 7 8 1 9 2nd loop

10 Bubble Sort 2 3 7 8 1 9 2nd loop

11 Bubble Sort 2 3 7 1 8 9 3rd loop

12 Bubble Sort 2 3 7 1 8 9 3rd loop

13 Bubble Sort 2 3 7 1 8 9 3rd loop

14 Bubble Sort 2 3 1 7 8 9 4th loop

15 Bubble Sort 2 3 1 7 8 9 4th loop

16 Bubble Sort 2 1 3 7 8 9 5th loop

17 Bubble Sort 1 2 3 7 8 9

18 Bubble Sort – pseudocode
Set swap flag to false. For count is set to each subscript in array from 0 through the next-to-last subscript if array[count] is greater than array[count +1] Swap the contents of array[count] and array[count + 1]. End if. End for. While any elements have been swapped.

19 Bubble Sort – Code #include <iostream> const int size = 6; using namespace std; int main() { int myArray[6] = {7, 2, 3, 8, 9, 1}; bool swap = false; int temp = 0; do { swap = false; for (int count = 0; count < (size - 1); count++) { if(myArray[count] > myArray[count+1]){ temp = myArray[count]; myArray[count] = myArray[count + 1]; myArray[count + 1] = temp; swap = true; } } while(swap); // Do this loop until swap turns to false, // which means there was no swap in for loop, // which means all items where sorted, // so the array is sorted, don't execute the loop again. for (int i = 0; i < size; i++) { cout << myArray[i] << " " ; cout << endl; system("pause"); return 0;

20 Selection Sort 50 70 20 80 90 10 1st loop 50 min Index of min

21 Selection Sort 50 70 20 80 90 10 1st loop 20 50 2 min Index of min

22 Selection Sort 50 70 20 80 90 10 1st loop 20 2 min Index of min

23 Selection Sort 50 70 20 80 90 10 1st loop 20 2 min Index of min

24 Selection Sort 50 70 20 80 90 10 1st loop 20 10 5 2 min Index of min

25 Selection Sort 50 70 20 80 90 10 1st loop 2 10 5 2 min Index of min

26 Selection Sort 10 70 20 80 90 50 2nd loop 70 1 min Index of min

27 Selection Sort 10 70 20 80 90 50 2nd loop 20 70 2 1 min Index of min

28 Selection Sort 10 70 20 80 90 50 2nd loop 20 2 min Index of min

29 Selection Sort 10 70 20 80 90 50 2nd loop 20 2 min Index of min

30 Selection Sort 10 70 20 80 90 50 2nd loop 20 2 min Index of min

31 Selection Sort 10 70 20 80 90 50 2nd loop 20 2 min Index of min

32 Selection Sort 10 70 20 80 90 50 2nd loop 20 2 min Index of min

33 Selection Sort 10 20 70 80 90 50 3rd loop 70 2 min Index of min

34 Selection Sort 10 20 70 80 90 50 3rd loop 70 2 min Index of min

35 Selection Sort 10 20 70 80 90 50 3rd loop 50 50 70 2 5 min
Index of min

36 Selection Sort 10 20 50 80 90 70 4th loop 80 3 min Index of min

37 Selection Sort 10 20 50 80 90 70 4th loop 80 70 3 5 min Index of min

38 Selection Sort 10 20 50 70 90 80 5th loop 90 4 min Index of min

39 Selection Sort 10 20 50 70 90 80 5th loop 90 80 4 5 min Index of min

40 Selection Sort 10 20 50 70 80 90

41 Selection Sort – pseudocode
For startScan is set to each subscript in array from 0 through the next-to-last subscript Set index variable to startScan. Set minIndex variable to startScan. Set minValue variable to array[startScan]. For index is set to each subscript in array from (startScan +1) through the last subscript If array[index] is less than minValue Set minValue to array[index]. Set minIndexValue to index. End if. End for. Set array[minIndex] to array[startScan]. Set array[startScan] to minValue. End For.

42 Selection Sort - Code #include <iostream> using namespace std; const int size =6; int main() { int myArray[6] = {50, 70, 20, 80, 90, 10}; for (int startScan = 0; startScan < size; startScan++) { int minIndex = startScan; int minValue = myArray[startScan]; for (int i = startScan + 1; i < size; i++) { if(myArray[i] < minValue){ minValue = myArray[i]; minIndex = i; } myArray[minIndex] = myArray[startScan]; myArray[startScan] = minValue; for (int i = 0; i < size; i++) { cout << myArray[i] << " " ; cout << endl; system("pause"); return 0;


Download ppt "Sorting Algorithms."

Similar presentations


Ads by Google