home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1991 / 07_08 / review / ada / shellsor.cpp < prev   
Encoding:
C/C++ Source or Header  |  1991-01-17  |  1001 b   |  45 lines

  1. // Shellsort Testprogramm in C++
  2. #include <iostream.h>
  3. #include <time.h>
  4.  
  5. const int size=4000; // Größe des Feldes
  6. static long iterationen=32; //Durchläufe
  7. int feld[size];
  8.  
  9. // Vertauschen der Inhalte von a und b
  10.  
  11. inline void swap(int& a, int& b)
  12. {  a ^= b; b ^= a; a ^= b; }
  13.  
  14. // Shellsort - v[0] .. v[n-1] werden
  15. // aufsteigend sortiert
  16. // Algorithmus nach:
  17. //   Kernighan/Ritchie,
  18. //   The C Programming Language, 2nd ed.
  19. void shellsort(int* v, int n)
  20. {  int gap, i,j;
  21.    for(gap=n/2; gap>0; gap /= 2)
  22.       for(i = gap; i < n; i++)
  23.          for(
  24.             j = i-gap;
  25.             j>=0 && v[j]>v[j+gap];
  26.             j -= gap
  27.          )
  28.             swap(v[j],v[j+gap]);
  29. }
  30.  
  31. void main(void)
  32. {
  33.    time_t startzeit = clock();
  34.    while(iterationen--)
  35.    {  // Feld initialisieren
  36.       for(int i=0; i<size; i++)
  37.          feld[i] = size-i;
  38.       // Sortieren
  39.       shellsort(feld,size);
  40.    }
  41.    cout << ((clock()-startzeit)/CLK_TCK)
  42.         << "Sekunden\n";
  43. }
  44.  
  45.