Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- using namespace std;
- template<typename T> void show(T *, const long);
- template<typename T> void shell(T *, const long);
- int sedgewick(long *, const long);
- int main() {
- const long size = 12L;
- int vector[size];
- srand((unsigned)time(NULL));
- for (long n = 0; n < size; n++) vector[n] = rand();
- show(vector, size);
- cout << endl;
- shell(vector, size);
- show(vector, size);
- cin.get();
- return 0;
- }
- template<typename T> void show(T * _vector, const long _size) {
- for (long n = 0; n < _size; n++) cout << _vector[n] << ' ';
- cout << endl;
- }
- template<typename T> void shell(T * _vector, const long _size) {
- const long lim = 40;
- long sequence[lim], increment, n, m;
- int size = sedgewick(sequence, _size);
- while (size >= 0) {
- increment = sequence[size--];
- for (n = increment; n < _size; n++) {
- T temp = _vector[n];
- for (m = n - increment; (m >= 0) && (_vector[m] > temp); m -= increment)
- _vector[m + increment] = _vector[m];
- _vector[m + increment] = temp;
- }
- }
- }
- int sedgewick(long * _increment, const long _size) {
- unsigned long x1, x2, x3;
- x1 = x2 = x3 = 1;
- int step = -1;
- do {
- if (++step & 1) _increment[step] = 8 * x1 - 6 * x2 + 1;
- else {
- _increment[step] = 9 * x1 - 9 * x3 + 1;
- x2 *= 2;
- x3 *= 2;
- }
- x1 *= 2;
- } while (3 * _increment[step] < _size);
- return step > 0 ? --step : 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement