Sorting Algorithms
Bubble Sort 7 2 3 8 9 1 1st loop
Bubble Sort 2 7 3 8 9 1 1st loop
Bubble Sort 2 3 7 8 9 1 1st loop
Bubble Sort 2 3 7 8 9 1 1st loop
Bubble Sort 2 3 7 8 9 1 1st loop
Bubble Sort 2 3 7 8 1 9 2nd loop
Bubble Sort 2 3 7 8 1 9 2nd loop
Bubble Sort 2 3 7 8 1 9 2nd loop
Bubble Sort 2 3 7 8 1 9 2nd loop
Bubble Sort 2 3 7 1 8 9 3rd loop
Bubble Sort 2 3 7 1 8 9 3rd loop
Bubble Sort 2 3 7 1 8 9 3rd loop
Bubble Sort 2 3 1 7 8 9 4th loop
Bubble Sort 2 3 1 7 8 9 4th loop
Bubble Sort 2 1 3 7 8 9 5th loop
Bubble Sort 1 2 3 7 8 9
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.
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;
Selection Sort 50 70 20 80 90 10 1st loop 50 min Index of min
Selection Sort 50 70 20 80 90 10 1st loop 20 50 2 min Index of min
Selection Sort 50 70 20 80 90 10 1st loop 20 2 min Index of min
Selection Sort 50 70 20 80 90 10 1st loop 20 2 min Index of min
Selection Sort 50 70 20 80 90 10 1st loop 20 10 5 2 min Index of min
Selection Sort 50 70 20 80 90 10 1st loop 2 10 5 2 min Index of min
Selection Sort 10 70 20 80 90 50 2nd loop 70 1 min Index of min
Selection Sort 10 70 20 80 90 50 2nd loop 20 70 2 1 min Index of min
Selection Sort 10 70 20 80 90 50 2nd loop 20 2 min Index of min
Selection Sort 10 70 20 80 90 50 2nd loop 20 2 min Index of min
Selection Sort 10 70 20 80 90 50 2nd loop 20 2 min Index of min
Selection Sort 10 70 20 80 90 50 2nd loop 20 2 min Index of min
Selection Sort 10 70 20 80 90 50 2nd loop 20 2 min Index of min
Selection Sort 10 20 70 80 90 50 3rd loop 70 2 min Index of min
Selection Sort 10 20 70 80 90 50 3rd loop 70 2 min Index of min
Selection Sort 10 20 70 80 90 50 3rd loop 50 50 70 2 5 min Index of min
Selection Sort 10 20 50 80 90 70 4th loop 80 3 min Index of min
Selection Sort 10 20 50 80 90 70 4th loop 80 70 3 5 min Index of min
Selection Sort 10 20 50 70 90 80 5th loop 90 4 min Index of min
Selection Sort 10 20 50 70 90 80 5th loop 90 80 4 5 min Index of min
Selection Sort 10 20 50 70 80 90
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.
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;