Advertisement
markyrocks

sorting

Jun 5th, 2021 (edited)
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5. void _swap(int* a, int* b) {
  6.  
  7.     *a ^= *b;
  8.     *b ^= *a;
  9.     *a ^= *b;
  10. }
  11.  
  12. void _nsort(int* ar, int size) {
  13.  
  14.     int* begin = ar;
  15.     int* end = ar + size - 1;
  16.     int* a = end;
  17.     int* b = ar + (int)size / 2;
  18.     bool change = true;
  19.     bool flip = false;
  20.     int* cc_b = b;
  21.     while (b != a) {
  22.         a = end;
  23.         if (flip) { b = a - 1; }
  24.         else {b = cc_b;}
  25.        
  26.         change = false;
  27.  
  28.         while (b > begin - 1) {
  29.  
  30.             if (*b > *a) {
  31.                 _swap(a, b);
  32.                 change = true;
  33.             }
  34.             b--;
  35.             a--;
  36.         }
  37.         if (!flip) {
  38.             cc_b++;
  39.         }
  40.         else if (flip && !change) { break; }
  41.         flip = !flip;
  42.  
  43.     }
  44.    
  45.  
  46. }
  47.  
  48.  
  49. void newsort(int* ar, int size) {
  50.     int* begin = ar;
  51.     int* end = ar+size-1;
  52.     int* a;
  53.  
  54.     while (begin < end+1) {
  55.         a = begin;
  56.        
  57.         while (a < end) {
  58.             if (*a > *end) {
  59.                 *a ^= *end;
  60.                 *end ^= *a;
  61.                 *a ^= *end;
  62.             }
  63.             if (*a < *begin) {
  64.                 *a ^= *begin;
  65.                 *begin ^= *a;
  66.                 *a ^= *begin;
  67.             }
  68.             a++;
  69.         }
  70.         end--;
  71.         begin++;
  72.     }
  73. }
  74.  
  75. void newsort2(int* ar, int size) {
  76.     int* begin = ar;
  77.     int* end = ar + size-1;
  78.     int* a = end;
  79.     int* cc_b = ar + size / 2;
  80.     int* b = cc_b;
  81.  
  82.     while (begin < end+1 ) {
  83.         a = end;
  84.         b = begin;
  85.  
  86.         while (b < a) {
  87.             if (*a > *end && *a > *b) {
  88.                 *a ^= *end;
  89.                 *end ^= *a;
  90.                 *a ^= *end;
  91.             }
  92.              if (*a < *begin && *a < *b) {
  93.                 *a ^= *begin;
  94.                 *begin ^= *a;
  95.                 *a ^= *begin;
  96.             }
  97.              if (*b > *end) {
  98.                 *b ^= *end;
  99.                 *end ^= *b;
  100.                 *b ^= *end;
  101.             }
  102.              if (*b < *begin) {
  103.                 *b ^= *begin;
  104.                 *begin ^= *b;
  105.                 *b ^= *begin;
  106.             }
  107.             if (*a < *b) {
  108.                 *a ^= *b;
  109.                 *b ^= *a;
  110.                 *a ^= *b;
  111.  
  112.             }
  113.             a--;
  114.             b++;
  115.         }
  116.         end--;
  117.         begin++;
  118.     }
  119. }
  120.  
  121. void algosort(int* ar, int size) { //not done but i'm liking where this is going
  122.  
  123.     int** copy = new int* [size];
  124.     int** temp = new int* [size];
  125.     int** pp = copy;
  126.     int** pp_t = temp;
  127.     int* c = ar;
  128.     int** lo = copy;
  129.     int** hi = copy + size-1;
  130.  
  131.     while (c < ar + size) { //copy pointers
  132.         *pp = c;
  133.         *pp_t = nullptr;
  134.        
  135.         pp++; c++; pp_t++;
  136.     }
  137.  
  138.     pp = copy;
  139.  
  140.     while (pp < copy + size) { //could probably do some swapping here. its a waste not to.
  141.         if(**pp > **hi){
  142.             hi = pp;
  143.         }
  144.         if (**pp < **lo) {
  145.             lo = pp;
  146.         }
  147.         pp++;
  148.     }
  149.  
  150.     float ratio = (float)abs(**hi - **lo) / (size);
  151.  
  152.     //cout << "hi= " << **hi << " lo= " << **lo << " ratio= " << ratio;
  153.  
  154.     *temp = *lo;
  155.     *(temp + size - 1) = *hi;
  156.     *lo = nullptr;
  157.     *hi = nullptr;
  158.     pp = copy;
  159.  
  160.     while (pp < copy + size) {
  161.  
  162.         if (*pp) {
  163.             int index = abs((**pp-1) / ratio);
  164.             int cc_idx = index;
  165.                 while (1) {
  166.                     if (index > size-1) {
  167.                         index = 1;
  168.                     }
  169.                     if (!temp[index]) {
  170.                         temp[index] = *pp;
  171.                         *pp = nullptr;
  172.                         break;
  173.                     }
  174.                     else if (**pp > *temp[index]) {
  175.                         *lo = temp[index];
  176.                         temp[index] = *pp;
  177.                         *pp = *lo;
  178.                        
  179.                     }
  180.                      if (temp[index] && **pp < *temp[index]) {
  181.                         index--;
  182.                         while(1){
  183.                             if (index == -1) {
  184.                                 index = size - 1;
  185.                             }
  186.                             if (!temp[index]) {
  187.                                 temp[index] = *pp;
  188.                                 *pp = nullptr;
  189.                                 break;
  190.                             }
  191.                             index--;
  192.                         }
  193.                         break;
  194.                     }
  195.  
  196.                 index++;
  197.                 }
  198.         }
  199.         pp++;
  200.     }
  201.  
  202.     for (auto i = temp; i < temp + size; i++) { //not terrible needs tightened up
  203.         cout << **i << " ";
  204.     }
  205.  
  206.  
  207. }
  208.  
  209. int main(){
  210.     int a[20] { 7, 3, 6, 1, 6, 7, 8, 9, 4, 2, 7, 3, 6, 1, 6, 7, 8, 9, 4, 2 };
  211.     ///int* a=new int[1000];
  212.     //for (int i = 0; i < 1000; i++) {
  213.     //  a[i] = rand() % 100;
  214.     //}
  215.  
  216.     ////_nsort(a, 100000);
  217.     ////std::sort(a,a+1000);
  218.     newsort(a, 20);
  219.     //newsort2(a, 20);
  220.    
  221.    
  222.     //algosort(a, 20);
  223.  
  224.     for (int i = 0; i < 20; i++) {
  225.         std::cout << a[i] << " ";
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement