Implement Shell sort algorithm in C++
Code
// shell sort#include <iostream>using namespace std;int main(){int n, i, j, temp, gap;int arr[50];cout << "Enter the size of array: ";cin >> n;cout << "Enter the elements of array: " << endl;for (i = 0; i < n; i++){cin >> arr[i];}for (gap = n / 2; gap >= 1; gap = gap / 2){for (i = gap; i < n; i++){temp = arr[i];j = i - gap;while (temp < arr[j] && j >= 0){arr[j + gap] = arr[j];j = j - gap;}arr[j + gap] = temp;}}cout << "Sorted array: ";for (i = 0; i < n; i++){cout << arr[i] << " ";}cout << endl;return 0;}
Output
Enter the size of array: 6
Enter the elements of array:
23
45
23
45
47
42
Sorted array: 23 23 42 45 45 47
0 Comments
Post a Comment