Created
December 2, 2024 14:26
-
-
Save vlaleli/0ca0b9e43dfb2eafc1d418135e7f6615 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| const int SIZE = 1000; | |
| int array[SIZE]; | |
| int max = 30; | |
| int min = -30; | |
| for (int i = 0; i < SIZE; ++i) | |
| { | |
| array[i] = rand() % (max - min) + min; | |
| } | |
| int stupid[SIZE], gnome[SIZE], bubble[SIZE]; | |
| for (int i = 0; i < SIZE; ++i) | |
| { | |
| stupid[i] = array[i]; | |
| gnome[i] = array[i]; | |
| bubble[i] = array[i]; | |
| } | |
| int itr_stupid = 0; | |
| bool sorted = false; | |
| while (!sorted) | |
| { | |
| sorted = true; | |
| for (int i = 0; i < SIZE - 1; ++i) | |
| { | |
| itr_stupid++; | |
| if (stupid[i] > stupid[i + 1]) | |
| { | |
| swap(stupid[i], stupid[i + 1]); | |
| sorted = false; | |
| } | |
| } | |
| } | |
| int itr_gnome = 0; | |
| int index = 0; | |
| while (index < SIZE) | |
| { | |
| itr_gnome++; | |
| if (index == 0 || gnome[index] >= gnome[index - 1]) | |
| { | |
| index++; | |
| } | |
| else | |
| { | |
| swap(gnome[index], gnome[index - 1]); | |
| index--; | |
| } | |
| } | |
| int itr_bubble = 0; | |
| for (int i = 0; i < SIZE - 1; ++i) | |
| { | |
| for (int j = 0; j < SIZE - i - 1; ++j) | |
| { | |
| itr_bubble++; | |
| if (bubble[j] > bubble[j + 1]) | |
| { | |
| swap(bubble[j], bubble[j + 1]); | |
| } | |
| } | |
| } | |
| cout << "Stupid Sort iterations: " << itr_stupid << endl; | |
| cout << "Gnome Sort iterations: " << itr_gnome << endl; | |
| cout << "Bubble Sort iterations: " << itr_bubble << endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment