TheAlgorithms-C-Plus-Plus/sorting/shell_sort.cpp
Krishna Vedala 231c99f880
fix dynamic array issues in sorting folder
(cherry picked from commit 01b69fcb24)
2020-05-26 13:32:41 -04:00

38 lines
935 B
C++

#include <iostream>
int main() {
int size = 10;
int* array = new int[size];
// Input
std::cout << "\nHow many numbers do want to enter in unsorted array : ";
std::cin >> size;
std::cout << "\nEnter the numbers for unsorted array : ";
for (int i = 0; i < size; i++) {
std::cin >> array[i];
}
// Sorting
for (int i = size / 2; i > 0; i = i / 2) {
for (int j = i; j < size; j++) {
for (int k = j - i; k >= 0; k = k - i) {
if (array[k] < array[k + i]) {
break;
} else {
int temp = array[k + i];
array[k + i] = array[k];
array[k] = temp;
}
}
}
}
// Output
std::cout << "\nSorted array : ";
for (int i = 0; i < size; ++i) {
std::cout << array[i] << "\t";
}
delete[] array;
return 0;
}