Created
December 11, 2024 08:54
-
-
Save vlaleli/259aa8a41b7b7abf6e02cea955bc209d 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 = 50; | |
| int array1[SIZE], array2[SIZE]; | |
| int insertionIterations = 0, shellIterations = 0; | |
| srand(time(0)); | |
| for (int i = 0; i < SIZE; i++) { | |
| array1[i] = rand() % 100; | |
| array2[i] = array1[i]; | |
| } | |
| for (int i = 1; i < SIZE; i++) { | |
| int key = array1[i]; | |
| int j = i - 1; | |
| while (j >= 0 && array1[j] > key) { | |
| array1[j + 1] = array1[j]; | |
| j--; | |
| insertionIterations++; | |
| } | |
| array1[j + 1] = key; | |
| insertionIterations++; | |
| } | |
| for (int gap = SIZE / 2; gap > 0; gap /= 2) { | |
| for (int i = gap; i < SIZE; i++) { | |
| int temp = array2[i]; | |
| int j = i; | |
| while (j >= gap && array2[j - gap] > temp) { | |
| array2[j] = array2[j - gap]; | |
| j -= gap; | |
| shellIterations++; | |
| } | |
| array2[j] = temp; | |
| shellIterations++; | |
| } | |
| } | |
| cout << "Array after insertion sort:\n"; | |
| for (int i = 0; i < SIZE; i++) { | |
| cout << array1[i] << " "; | |
| } | |
| cout << "\n\nArray after Shell sort:\n"; | |
| for (int i = 0; i < SIZE; i++) { | |
| cout << array2[i] << " "; | |
| } | |
| cout << "\n\nNumber of iterations of insertion sort: " << insertionIterations << "\n"; | |
| cout << "Number of Shell sort iterations: " << shellIterations << "\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment