Advertisement
MrEfendi

Metody sortowania ATH Bielsko-Biała C++

Jan 11th, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. #include "time.h"
  2. #include<iostream>
  3.  
  4. using namespace std;
  5. unsigned  long int const n=200000;
  6.  
  7.  
  8. void boubble_sort(  unsigned long int  n, float *  a) {
  9.     unsigned  long int l,k;
  10.     float p;
  11.     l=n;
  12.     do {
  13.         k=0;
  14.         l=l-1;
  15.         for (unsigned  long int i=1; i<=l; i++) {
  16.             if (a[i]>a[i+1]) {
  17.                 p=a[i];
  18.                 a[i]=a[i+1];
  19.                 a[i+1]=p;
  20.                 k=k+1;
  21.             }
  22.         }
  23.     } while (k!=0);
  24. }
  25. void insert_sort(  unsigned  long int n, float * a) {
  26.     unsigned  long int l,p,k,s;
  27.     float  w=0;
  28.     a[0]=w;
  29.     for (unsigned long int i=2; i<=n; i++) {
  30.         float y=a[i];
  31.         l=0;
  32.         p=i-1;
  33.         do {
  34.             s=(l+p+1) / 2;
  35.             if (a[s]<=y) l=s;
  36.             else p=s-1;
  37.         } while (l!=p);
  38.         k=l;
  39.         for (unsigned long int j=i-1; j>=k+1; j--) a[j+1]=a[j];
  40.         a[k+1]=y;
  41.     }
  42. }
  43.  
  44.  
  45. void scalaj(unsigned  long int l,unsigned  long int s, unsigned long int p,float * a) {
  46.  
  47.  
  48.     float  z[n+1];
  49.     unsigned  long int m,k,i,j;
  50.     m=l;
  51.     i=l;
  52.     j=s;
  53.     do {
  54.         if (a[i]<=a[j]) {
  55.             z[m]=a[i];
  56.             i=i+1;
  57.         } else {
  58.             z[m]=a[j];
  59.             j=j+1;
  60.         }
  61.         m=m+1;
  62.     } while(i<s && j<=p);
  63.     if (i<s) {
  64.         k=p;
  65.         for (unsigned long int j=s-1; j>=i; j--) {
  66.             a[k]=a[j];
  67.             k=k-1;
  68.         }
  69.     }
  70.     for (unsigned  long int i=l; i<=m-1; i++) a[i]=z[i];
  71. }
  72.  
  73. void sort_scal(unsigned long int d,unsigned long int g,float *  a) {
  74.     unsigned long int s;
  75.  
  76.     if (d<g) {
  77.         s=(d+g)/2;
  78.  
  79.         sort_scal(d,s,a);
  80.         sort_scal(s+1,g,a);
  81.         scalaj(d,s+1,g,a);
  82.     }
  83. }
  84.  
  85. void quick_sort(unsigned long int d,unsigned  long int g, float * a) {
  86.     unsigned  long int s,l,p;
  87.     float v,u;
  88.     l=d;
  89.     p=g;
  90.     s=(l+p)/2;
  91.     v=a[s];
  92.     do {
  93.         while (a[l]<v) l=l+1;
  94.         while (a[p]>v) p=p-1;
  95.         if (l<=p) {
  96.             u=a[l];
  97.             a[l]=a[p];
  98.             a[p]=u;
  99.             l=l+1;
  100.             p=p-1;
  101.         }
  102.     } while (l<=p);
  103.     if (d<p) quick_sort(d,p,a);
  104.     if (l<g) quick_sort(l,g,a);
  105. }
  106.  
  107.  
  108.  
  109.  
  110. int main() {
  111.  
  112.     float * a= new float[n];
  113.     unsigned long int m;
  114.     int k;
  115.     time_t t;
  116.     clock_t tp,tk;
  117.     double tc;
  118.     srand((unsigned)time(&t));
  119.     cout<<" podaj ilosc wyrazow ciagu ";
  120.     cin>>m;
  121.  
  122.     do {
  123.  
  124.  
  125.         cout <<" podaj nr procedury"<<"\n"<<"1 - babelkowa"<<"\n"<<"2 - wstawianie"<<"\n"<<"3 - scalanie"<<"\n"<<"4 - szybkie"<<"\n";
  126.  
  127.         //cout <<"podaj k"<<"\n";
  128.         cin >> k;
  129.  
  130.         cout <<" ciag wylosowany  k="<<k<<"\n";
  131.         for (unsigned long int i=1; i<=m; i++) {
  132.             a[i]= rand()%10000;
  133.             cout <<a[i] <<" \t" ;
  134.         }
  135.  
  136.         cout <<"\n";
  137.         switch (k) {
  138.  
  139.         case 1: {
  140.             cout <<" babelkowa";
  141.             tp=clock();
  142.             boubble_sort(m,a);
  143.             tk=clock();
  144.             tc=(tk-tp)/double(CLOCKS_PER_SEC);
  145.             cout <<" czas obliczen="<<tc<<"\n";
  146.             break;
  147.         }
  148.         case 2: {
  149.             cout <<" wstawianie";
  150.             tp=clock();
  151.             insert_sort(m,a);
  152.             tk=clock();
  153.             tc=(tk-tp)/double(CLOCKS_PER_SEC);
  154.             cout <<" czas obliczen="<<tc<<"\n";
  155.             break;
  156.         }
  157.         case 3: {
  158.             cout <<" scalanie";
  159.             tp=clock();
  160.             sort_scal(1,m,a);
  161.             tk=clock();
  162.             tc=(tk-tp)/double(CLOCKS_PER_SEC);
  163.             cout <<" czas obliczen="<<tc<<"\n";
  164.             break;
  165.         }
  166.         case 4: {
  167.             cout <<" szybkie";
  168.             tp=clock();
  169.             quick_sort(1,m,a);
  170.             tk=clock();
  171.             tc=(tk-tp)/double(CLOCKS_PER_SEC);
  172.             cout <<" czas obliczen="<<tc<<"\n";
  173.             break;
  174.         }
  175.         case 0:
  176.             break;
  177.         }
  178.         cout <<" ciag posortowany "<<"\n";
  179.         for (unsigned long int i=1; i<=m; i++) {
  180.             cout << a[i]<<"\t";
  181.         }
  182.     } while (k!=0);
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement