Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- void bubbleSort(int[], int);
- void swapVals(int*, int*);
- void selectionSort(int[], int);
- int main()
- {
- int array1[9] = { 2, 4, 9, 1, 19, 3, 11, 8 };
- cout << "Bubble sort (Original):\n ";
- for (int i = 0; i < 8; i++) {
- cout << array1[i] << " ";
- }
- bubbleSort(array1, 8);
- int array2[9] = { 2, 4, 9, 1, 19, 3, 11, 8 };
- cout << "\nSelection sort (Original):\n ";
- for (int i = 0; i < 8; i++) {
- cout << array2[i] << " ";
- }
- selectionSort(array2, 8);
- system("pause");
- }
- void bubbleSort(int ray[], int size) {
- bool change = false;
- cout << "\nBubble sort (Steps):\n ";
- int steps = 1;
- do {
- change = false;
- for (int i = 0; i < size; i++) {
- if (ray[i + 1] != NULL) {
- if (ray[i] > ray[i + 1]) {
- int temp = ray[i];
- ray[i] = ray[i + 1];
- ray[i + 1] = temp;
- change = true;
- }
- }
- }
- cout << "Step #" << steps << ": ";
- for (int i = 0; i < size; i++) {
- cout << ray[i] << " ";
- }
- steps++;
- cout << "\n";
- } while (change);
- }
- void swapVals(int *val1, int *val2)
- {
- int temp = *val1;
- *val1 = *val2;
- *val2 = temp;
- }
- void selectionSort(int ray[], int size) {
- cout << "\nSelection sort (Steps):\n ";
- int minVal = ray[0];
- int steps = 1;
- for (int i = 0; i < size; i++) {
- minVal = i;
- for (int j = i + 1; j < size; j++) {
- if (ray[j] < ray[minVal]) {
- minVal = j;
- }
- }
- swapVals(&ray[minVal], &ray[i]);
- cout << "Step #" << steps << ": ";
- for (int i2 = 0; i2 < size; i2++) {
- cout << ray[i2] << " ";
- }
- cout << "\n";
- steps++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement