TheAlgorithms-C-Plus-Plus/sorting/shell_sort.cpp

47 lines
1015 B
C++
Raw Normal View History

2019-08-21 10:10:08 +08:00
#include <iostream>
2016-07-24 19:47:56 +08:00
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];
}
2016-07-24 19:47:56 +08:00
// 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;
}
}
}
}
2016-07-24 19:47:56 +08:00
// Output
std::cout << "\nSorted array : ";
for (int i = 0; i < size; ++i)
{
std::cout << array[i] << "\t";
}
delete[] array;
return 0;
2016-07-24 19:47:56 +08:00
}