Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <vector>
- using namespace std;
- void _swap(int* a, int* b) {
- *a ^= *b;
- *b ^= *a;
- *a ^= *b;
- }
- void _nsort(int* ar, int size) {
- int* begin = ar;
- int* end = ar + size - 1;
- int* a = end;
- int* b = ar + (int)size / 2;
- bool change = true;
- bool flip = false;
- int* cc_b = b;
- while (b != a) {
- a = end;
- if (flip) { b = a - 1; }
- else {b = cc_b;}
- change = false;
- while (b > begin - 1) {
- if (*b > *a) {
- _swap(a, b);
- change = true;
- }
- b--;
- a--;
- }
- if (!flip) {
- cc_b++;
- }
- else if (flip && !change) { break; }
- flip = !flip;
- }
- }
- void newsort(int* ar, int size) {
- int* begin = ar;
- int* end = ar+size-1;
- int* a;
- while (begin < end+1) {
- a = begin;
- while (a < end) {
- if (*a > *end) {
- *a ^= *end;
- *end ^= *a;
- *a ^= *end;
- }
- if (*a < *begin) {
- *a ^= *begin;
- *begin ^= *a;
- *a ^= *begin;
- }
- a++;
- }
- end--;
- begin++;
- }
- }
- void newsort2(int* ar, int size) {
- int* begin = ar;
- int* end = ar + size-1;
- int* a = end;
- int* cc_b = ar + size / 2;
- int* b = cc_b;
- while (begin < end+1 ) {
- a = end;
- b = begin;
- while (b < a) {
- if (*a > *end && *a > *b) {
- *a ^= *end;
- *end ^= *a;
- *a ^= *end;
- }
- if (*a < *begin && *a < *b) {
- *a ^= *begin;
- *begin ^= *a;
- *a ^= *begin;
- }
- if (*b > *end) {
- *b ^= *end;
- *end ^= *b;
- *b ^= *end;
- }
- if (*b < *begin) {
- *b ^= *begin;
- *begin ^= *b;
- *b ^= *begin;
- }
- if (*a < *b) {
- *a ^= *b;
- *b ^= *a;
- *a ^= *b;
- }
- a--;
- b++;
- }
- end--;
- begin++;
- }
- }
- void algosort(int* ar, int size) { //not done but i'm liking where this is going
- int** copy = new int* [size];
- int** temp = new int* [size];
- int** pp = copy;
- int** pp_t = temp;
- int* c = ar;
- int** lo = copy;
- int** hi = copy + size-1;
- while (c < ar + size) { //copy pointers
- *pp = c;
- *pp_t = nullptr;
- pp++; c++; pp_t++;
- }
- pp = copy;
- while (pp < copy + size) { //could probably do some swapping here. its a waste not to.
- if(**pp > **hi){
- hi = pp;
- }
- if (**pp < **lo) {
- lo = pp;
- }
- pp++;
- }
- float ratio = (float)abs(**hi - **lo) / (size);
- //cout << "hi= " << **hi << " lo= " << **lo << " ratio= " << ratio;
- *temp = *lo;
- *(temp + size - 1) = *hi;
- *lo = nullptr;
- *hi = nullptr;
- pp = copy;
- while (pp < copy + size) {
- if (*pp) {
- int index = abs((**pp-1) / ratio);
- int cc_idx = index;
- while (1) {
- if (index > size-1) {
- index = 1;
- }
- if (!temp[index]) {
- temp[index] = *pp;
- *pp = nullptr;
- break;
- }
- else if (**pp > *temp[index]) {
- *lo = temp[index];
- temp[index] = *pp;
- *pp = *lo;
- }
- if (temp[index] && **pp < *temp[index]) {
- index--;
- while(1){
- if (index == -1) {
- index = size - 1;
- }
- if (!temp[index]) {
- temp[index] = *pp;
- *pp = nullptr;
- break;
- }
- index--;
- }
- break;
- }
- index++;
- }
- }
- pp++;
- }
- for (auto i = temp; i < temp + size; i++) { //not terrible needs tightened up
- cout << **i << " ";
- }
- }
- int main(){
- int a[20] { 7, 3, 6, 1, 6, 7, 8, 9, 4, 2, 7, 3, 6, 1, 6, 7, 8, 9, 4, 2 };
- ///int* a=new int[1000];
- //for (int i = 0; i < 1000; i++) {
- // a[i] = rand() % 100;
- //}
- ////_nsort(a, 100000);
- ////std::sort(a,a+1000);
- newsort(a, 20);
- //newsort2(a, 20);
- //algosort(a, 20);
- for (int i = 0; i < 20; i++) {
- std::cout << a[i] << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement