TheAlgorithms-C-Plus-Plus/Sorting/Shell Sort.cpp

46 lines
728 B
C++
Raw Normal View History

2019-08-21 10:10:08 +08:00
#include <iostream>
2016-07-24 19:47:56 +08:00
using namespace std;
int main()
{
2019-08-21 10:10:08 +08:00
int size = 10;
2016-07-24 19:47:56 +08:00
int array[size];
// Input
2019-08-21 10:10:08 +08:00
cout << "\nHow many numbers do want to enter in unsorted array : ";
cin >> size;
cout << "\nEnter the numbers for unsorted array : ";
2016-07-24 19:47:56 +08:00
for (int i = 0; i < size; i++)
{
2019-08-21 10:10:08 +08:00
cin >> array[i];
2016-07-24 19:47:56 +08:00
}
// Sorting
2019-08-21 10:10:08 +08:00
for (int i = size / 2; i > 0; i = i / 2)
2016-07-24 19:47:56 +08:00
{
2019-08-21 10:10:08 +08:00
for (int j = i; j < size; j++)
2016-07-24 19:47:56 +08:00
{
2019-08-21 10:10:08 +08:00
for (int k = j - i; k >= 0; k = k - i)
2016-07-24 19:47:56 +08:00
{
2019-08-21 10:10:08 +08:00
if (array[k] < array[k + i])
2016-07-24 19:47:56 +08:00
{
break;
}
else
{
2019-08-21 10:10:08 +08:00
int temp = array[k + i];
array[k + i] = array[k];
array[k] = temp;
2016-07-24 19:47:56 +08:00
}
}
}
}
// Output
2019-08-21 10:10:08 +08:00
cout << "\nSorted array : ";
2016-07-24 19:47:56 +08:00
for (int i = 0; i < size; ++i)
{
2019-08-21 10:10:08 +08:00
cout << array[i] << "\t";
2016-07-24 19:47:56 +08:00
}
return 0;
}