Advertisement
Neveles

© 2020 Neveles. All rights reserved.

Apr 13th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. void HibbardSort(int n, int *arr)       //SHELL SORT - HIBBARD'S INCREMENTS
  2. {
  3.   int i, j, k, increment, temp;
  4.   int swp = 0, comp = 0;
  5.   double val;
  6.   val = (int)log(n + 1) / log(2);
  7.   increment = (int)pow(2, val) - 1;
  8.   while (increment > 0)
  9.   {
  10.     for (i = 0;i < increment;i++)
  11.     {
  12.       for (j = 0;j < n;j += increment)
  13.       {
  14.         temp = arr[j];
  15.         for (k = j - increment;k >= 0 && temp < arr[k];k -= increment)
  16.         {
  17.           comp++;
  18.           swp++;
  19.           arr[k + increment] = arr[k];
  20.         }
  21.         arr[k + increment] = temp;
  22.         swp++;
  23.       }
  24.     }
  25.     comp++;
  26.     val--;
  27.     if (increment != 1)
  28.     {
  29.       increment = pow(2, val) - 1;
  30.     }
  31.     else
  32.     {
  33.       increment = 0;
  34.     }
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement