Advertisement
iamweasel

sorting

May 17th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. struct Elem{
  5.     int * key;
  6.     Elem* link;
  7.     Elem(){
  8.         key = NULL;
  9.         link = NULL;
  10.     }
  11. };
  12.  
  13. int main(){
  14.     //P = new Elem();
  15.     int n;
  16.     cout << "vvedite kol-vo zapisey" << endl;
  17.     cin >> n;
  18.     Elem *R;
  19.     R = new Elem[n];
  20.     cout << "vvedite kolithestvo cifr v kazdom chisle" << endl;
  21.     int t;
  22.     cin >> t;
  23.     cout << "vvedite kluchi" << endl;
  24.     int a[n];
  25.     for (int i = 0; i < n; ++i){
  26.         cin >> a[i];
  27.         a[i] = a[i] % (unsigned int)pow(10,t);
  28.         R[i].key = new int[t];
  29.         for (int j = 0; j < t; ++j){
  30.             R[i].key[j] = a[i] % 10;
  31.             // cout << endl << R[i].key[j];
  32.             a[i] /= 10;
  33.         }
  34.     }
  35.     Elem Top[10];
  36.     Elem Botm[10];
  37.     Elem *P = &R[n-1];
  38.     int p = n-1;
  39.     for (int k = 0; k < t; ++k){
  40.         for (int i = 0; i < 10; ++i){
  41.             Top[i].link = &Botm[i];
  42.             Botm[i].link = NULL;
  43.         }
  44. R3:
  45.         int i = P->key[k];
  46.         Top[i].link->link = P;
  47.         Top[i].link = P;
  48.  
  49.         if(k==0 ){
  50.             if(P!= NULL && p!=0){
  51.                 p--;
  52.                 P = &R[p];
  53.                 goto R3;
  54.             }
  55.         }else{
  56.             P = P->link;
  57.             if (P != NULL){
  58.                 goto R3;
  59.             }
  60.         }
  61.  
  62.         i = 0;
  63. H2:
  64.         P = &Top[i];
  65. H3:
  66.         i++;
  67.         if(i==10){
  68.             P->link = NULL;
  69.             goto end;
  70.         }
  71.         if(Botm[i].link == NULL) goto H3;
  72.         P->link->link = Botm[i].link;
  73.         goto H2;
  74. end:
  75.         P = Botm[0].link;
  76.     }
  77.  
  78.     cout << "Sorted:\n";
  79.     while(P){
  80.         for(int i = t-1;i>=0;i--) cout << P->key[i];
  81.         cout << endl;
  82.         P = P->link;
  83.     }
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement