void _quicksort(int *p, int lo, int hi) { int piv = p[ (lo + hi) >> 1], i = lo, j = hi; while(i <= j) { while(p[i]<piv) i++; while(p[j]>piv) j--; if(i<=j) { int x = p[i]^p[j]; p[i] = p[i]^x; p[j] = p[j]^x; i++; j--; } } if(lo<j) _quicksort(p, lo, j); if(hi>i) _quicksort(p, i, hi); }
void _quicksort(int *pointlist, int n) { unsigned int i, j, ln, rn; while (n > 1) { SWAP3(pointlist, 0, n/2); for (i = 0, j = n; ; ) { do --j; while (pointlist[3*j+2] < pointlist[2]); do ++i; while (i < j && pointlist[3*i+2] > pointlist[2]); if (i >= j) break; SWAP3(pointlist, i, j); } SWAP3(pointlist, j, 0); ln = j; rn = n - ++j; if (ln < rn) { _quicksort(pointlist, ln); pointlist += 3*j; n = rn; } else { _quicksort(pointlist + 3*j, rn); n = ln; } } }
void _quicksort(long list[],long m,long n) { long key,i,j,k; if( m < n) { k = choose_pivot(m,n); swap(&list[m],&list[k]); key = list[m]; i = m+1; j = n; while(i <= j) { while((i <= n) && (list[i] <= key)) i++; while((j >= m) && (list[j] > key)) j--; if( i < j) swap(&list[i],&list[j]); } /* swap two elements */ swap(&list[m],&list[j]); /* recursively sort the lesser list */ _quicksort(list,m,j-1); _quicksort(list,j+1,n); } }
static void _quicksort(int *target, int s, int e) { int p; if ((e - s) <= 0) { return; } p = partition(target, s, e); _quicksort(target, s, p-1); _quicksort(target, p+1, e); return; }
void _quicksort(std::vector<T>& arr, const int64_t lo, const int64_t hi) { #if 0 if (lo < hi) { int64_t mid = lomuto_partition(arr, lo, hi); _quicksort(arr, lo, mid-1); _quicksort(arr, mid+1, hi); } #else if (lo < hi) { int64_t mid = hoare_partition(arr, lo, hi); _quicksort(arr, lo, mid); _quicksort(arr, mid+1, hi); } #endif }
/* function to be called base_pointer the base pointer to a element number_of_elements the number of elements to sort width offset of an element compar_func the comparison function //*/ void quicksort(void* base_pointer , size_t number_of_elements , size_t width , int (*compar_func)(const void*, const void *)) { static int number_of_processors = -1; /* get the number of processors and do some performance related settings //*/ if(number_of_processors == -1){ // get the number of processors (= cpu's) number_of_processors = sysconf( _SC_NPROCESSORS_ONLN); // lwp for each cpu if((number_of_processors > 1) && (pthread_getconcurrency() < number_of_processors)) pthread_setconcurrency(number_of_processors); // thread count not to exceed THR_PER_LWP per lwp threads_avail = (number_of_processors == 1) ? 0 : (number_of_processors * THR_PER_LWP); } /* set up the sorting arguments //*/ sort_args_t sort_args; sort_args.sortargs_base = base_pointer; sort_args.sortargs_number_of_elements = number_of_elements; sort_args.sortargs_width = width; sort_args.sortargs_compar_func = compar_func; // start sorting _quicksort(&sort_args); }
static void _sortPointList( int *pointlist, int npoints) { #ifdef KLT_USE_QSORT qsort(pointlist, npoints, 3*sizeof(int), _comparePoints); #else _quicksort(pointlist, npoints); #endif }
static void _quicksort(int *target, int s, int e) { int p, size; size = e - s + 1; if (size <= 1) { return; } switch (size) { case 2: NSORT2(target + s); break; case 3: NSORT3(target + s); break; case 4: NSORT4(target + s); break; case 5: NSORT5(target + s); break; case 6: NSORT6(target + s); break; case 7: NSORT7(target + s); break; case 8: NSORT8(target + s); break; default: p = partition(target, s, e); _quicksort(target, s, p-1); _quicksort(target, p+1, e); } return; }
/* Quicksort a doubly linked list */ static void _quicksort(struct rt_info *start, struct rt_info *end, int i, int key, int before) { struct rt_info *pivot = task_list_entry(start->task_list[i].next, i); struct rt_info *it = task_list_entry(pivot->task_list[i].next, i); struct rt_info *next; int low = 0, high = 0; while(it != end) { next = task_list_entry(it->task_list[i].next, i); if(compare(it, pivot, key, before)) { list_move_after(start, it, i); low++; } else high++; it = next; } if(high > 1) _quicksort(pivot, end, i, key, before); if(low > 1) _quicksort(start, pivot, i, key, before); }
void _quicksort (int64_t* l, int64_t start, int64_t stop) { int64_t pivot, left, right, tmp; if (stop - start > 0) { pivot = l[start]; left = start; right = stop; while (left <= right) { while (l[left] < pivot) left += 1; while (l[right] > pivot) right -= 1; if (left <= right) { tmp = l[left]; l[left] = l[right]; l[right] = tmp; left += 1; right -= 1; } } _quicksort(l, start, right); _quicksort(l, left, stop); } }
void qsort(void *base, size_t total_num, size_t size, int (*cmp)(const void *, const void *)) { size_t total_size = size * total_num; if(total_size < 1024) { msort_with_tmp(base, total_num, size, cmp, (char*)alloca(total_size) ); } else { char *tmp = (char*)malloc(size); if(tmp == NULL) { extern void _quicksort(void *base, size_t total_num, size_t size, int (*compare)(const void *, const void *) ); _quicksort(base, total_num, size, cmp); } else { msort_with_tmp(base, total_num, size, cmp, tmp ); free( tmp ); } } }
void sorted() { _quicksort(0, N - 1); };
/****************** quick sort *****************/ void quicksort(T* arr, int n) { if (arr = nullptr) return; _quicksort(arr, 0, n - 1); }
void qsort_r (void *b, size_t n, size_t s, __compar_d_fn_t cmp, void *arg) { size_t size = n * s; char *tmp = NULL; struct msort_param p; /* For large object sizes use indirect sorting. */ if (s > 32) size = 2 * n * sizeof (void *) + s; if (size < 1024) /* The temporary array is small, so put it on the stack. */ p.t = __alloca (size); else { /* We should avoid allocating too much memory since this might have to be backed up by swap space. */ static long int phys_pages; static int pagesize; if (pagesize == 0) { phys_pages = __sysconf (_SC_PHYS_PAGES); if (phys_pages == -1) /* Error while determining the memory size. So let's assume there is enough memory. Otherwise the implementer should provide a complete implementation of the `sysconf' function. */ phys_pages = (long int) (~0ul >> 1); /* The following determines that we will never use more than a quarter of the physical memory. */ phys_pages /= 4; /* Make sure phys_pages is written to memory. */ atomic_write_barrier (); pagesize = __sysconf (_SC_PAGESIZE); } /* Just a comment here. We cannot compute phys_pages * pagesize and compare the needed amount of memory against this value. The problem is that some systems might have more physical memory then can be represented with a `size_t' value (when measured in bytes. */ /* If the memory requirements are too high don't allocate memory. */ if (size / pagesize > (size_t) phys_pages) { _quicksort (b, n, s, cmp, arg); return; } /* It's somewhat large, so malloc it. */ int save = errno; tmp = malloc (size); __set_errno (save); if (tmp == NULL) { /* Couldn't get space, so use the slower algorithm that doesn't need a temporary array. */ _quicksort (b, n, s, cmp, arg); return; } p.t = tmp; }
void _quicksort(T* arr, int low, int high) { if (low >= high) return; // 临界条件,数组元素个数<= 1 int mid = partition(T* arr, low, high); _quicksort(T* arr, low, mid - 1); _quicksort(T* arr, mid + 1, high); }
void quicksort(int *target, int size) { _quicksort(target, 0, size -1); return; }
void quicksort(long list[]){ _quicksort(list, 0, LISTSIZE - 1); }
void quicksort(struct rt_info *head, int i, int key, int before) { if(!list_empty(&head->task_list[i]) && head->task_list[i].next != head->task_list[i].prev) _quicksort(head, head, i, key, before); }
void sorted() { _quicksort(0, n - 1); }
void quicksort(int *p, int n) { _quicksort(p, 0, n - 1); }
void quicksort(std::vector<T>& arr) { if (arr.size() == 0) return; _quicksort(arr, 0, arr.size() - 1); }
void qsort (void *b, size_t n, size_t s, __compar_fn_t cmp) { const size_t size = n * s; if (size < 1024) { void *buf = __alloca (size); /* The temporary array is small, so put it on the stack. */ msort_with_tmp (b, n, s, cmp, buf); } else { /* We should avoid allocating too much memory since this might have to be backed up by swap space. */ static long int phys_pages; static int pagesize; if (phys_pages == 0) { phys_pages = __sysconf (_SC_PHYS_PAGES); if (phys_pages == -1) /* Error while determining the memory size. So let's assume there is enough memory. Otherwise the implementer should provide a complete implementation of the `sysconf' function. */ phys_pages = (long int) (~0ul >> 1); /* The following determines that we will never use more than a quarter of the physical memory. */ phys_pages /= 4; pagesize = __sysconf (_SC_PAGESIZE); } /* Just a comment here. We cannot compute phys_pages * pagesize and compare the needed amount of memory against this value. The problem is that some systems might have more physical memory then can be represented with a `size_t' value (when measured in bytes. */ /* If the memory requirements are too high don't allocate memory. */ if (size / pagesize > phys_pages) _quicksort (b, n, s, cmp); else { /* It's somewhat large, so malloc it. */ int save = errno; char *tmp = malloc (size); if (tmp == NULL) { /* Couldn't get space, so use the slower algorithm that doesn't need a temporary array. */ __set_errno (save); _quicksort (b, n, s, cmp); } else { __set_errno (save); msort_with_tmp (b, n, s, cmp, tmp); free (tmp); } } }
void test(float * const pbase, size_t total_elems) { _quicksort(pbase, (sizeof(float) * total_elems) / sizeof(struct aggregate), sizeof(struct aggregate), less); }
void quicksort (int64_t* a, int64_t n) { _quicksort(a, 0, n - 1); }
/* called by quicksort() - does the actual sorting arg - to pass the structure of the sorting arguments //*/ static void* _quicksort(void *arg) { // set the sorting arguments locally sort_args_t *sargs = (sort_args_t *) arg; // base pointer - current pointer to the element, set to base register void *base_pointer = sargs->sortargs_base; // number of elements (size) int number_of_elements = sargs->sortargs_number_of_elements; // the offset size of the elements (each?) int width = sargs->sortargs_width; // set the comparison function (pointer) to the function set in the arguments int (*compar_func)(const void *, const void *) = sargs->sortargs_compar_func; // counding indexes register int idx; register int jdx; // the result of the comparison int result; int thread_count = 0; // tmp - temporary storage for an element at swapping void *tmp; // tmp_base - temporary base pointer, pointer to an element void *tmp_base[3]; void *pivot = 0; sort_args_t sort_args[2]; pthread_t tid; // find the pivot point switch(number_of_elements){ case 0: case 1: return 0; case 2: if((*compar_func)(SUB(base_pointer, 0), SUB(base_pointer, 1)) > 0){ SWAP(base_pointer, 0, 1, width); } return 0; case 3: // three sort if((*compar_func)(SUB(base_pointer, 0), SUB(base_pointer, 1)) > 0){ SWAP(base_pointer, 0, 1, width); } // the first two are now ordered, now order the second two if((*compar_func)(SUB(base_pointer, 2), SUB(base_pointer, 1)) < 0){ SWAP(base_pointer, 2, 1, width); } // should the second be moved to the first? if((*compar_func)(SUB(base_pointer, 1), SUB(base_pointer, 0)) < 0){ SWAP(base_pointer, 1, 0, width); } return 0; default: if(number_of_elements > 3){ tmp_base[0] = SUB(base_pointer, 0); tmp_base[1] = SUB(base_pointer, number_of_elements / 2); tmp_base[2] = SUB(base_pointer, number_of_elements - 1); // three sort if((*compar_func)(tmp_base[0], tmp_base[1]) > 0){ tmp = tmp_base[0]; tmp_base[0] = tmp_base[1]; tmp_base[1] = tmp; } // the first two are now ordered, now order the second two if((*compar_func)(tmp_base[2], tmp_base[1]) < 0){ tmp = tmp_base[1]; tmp_base[1] = tmp_base[2]; tmp_base[2] = tmp; } // should the second be moved to the first? if((*compar_func)(tmp_base[1], tmp_base[0]) < 0){ tmp = tmp_base[0]; tmp_base[0] = tmp_base[1]; tmp_base[1] = tmp; } if((*compar_func)(tmp_base[0], tmp_base[2]) != 0){ if((*compar_func)(tmp_base[0], tmp_base[1]) < 0){ pivot = tmp_base[1]; }else{ pivot = tmp_base[2]; } } } break; } if(pivot == 0){ for(idx = 1; idx < number_of_elements; ++idx){ if(0 != (result = ((*compar_func) (SUB(base_pointer, 0), SUB(base_pointer, idx))))){ pivot = (result > 0) ? SUB(base_pointer, 0) : SUB(base_pointer, idx); break; } } } if (pivot == 0) return 0; /* sort idx - starting from 0 jdx - starting from the last elements index base_pointer - pointer to the current element //*/ idx = 0; jdx = number_of_elements - 1; while(idx <= jdx){ while((*compar_func)(SUB(base_pointer, idx), pivot) < 0) ++idx; while((*compar_func)(SUB(base_pointer, jdx), pivot) >= 0) --jdx; if(idx < jdx){ SWAP(base_pointer, idx, jdx, width); ++idx; --jdx; } } // sort the sides judiciously switch(idx){ case 0: case 1: break; case 2: if((*compar_func)(SUB(base_pointer, 0), SUB(base_pointer, 1)) > 0) { SWAP(base_pointer, 0, 1, width); } break; case 3: // three sort if((*compar_func)(SUB(base_pointer, 0), SUB(base_pointer, 1)) > 0) { SWAP(base_pointer, 0, 1, width); } // the first two are now ordered, now order the second two if ((*compar_func)(SUB(base_pointer, 2), SUB(base_pointer, 1)) < 0) { SWAP(base_pointer, 2, 1, width); } // should the second be moved to the first? if ((*compar_func)(SUB(base_pointer, 1), SUB(base_pointer, 0)) < 0) { SWAP(base_pointer, 1, 0, width); } break; default: // reinit the sorting arguments sort_args[0].sortargs_base = base_pointer; sort_args[0].sortargs_number_of_elements = idx; sort_args[0].sortargs_width = width; sort_args[0].sortargs_compar_func = compar_func; // call the _quicksort method recursively, creating threads if possible if((threads_avail > 0) && (idx > SLICE_THRESH)) { threads_avail--; pthread_create(&tid, NULL, _quicksort, &sort_args[0]); thread_count = 1; } else _quicksort(&sort_args[0]); break; } // shrink jdx = number_of_elements - idx; switch(jdx){ case 1: break; case 2: if((*compar_func)(SUB(base_pointer, idx), SUB(base_pointer, idx + 1)) > 0){ SWAP(base_pointer, idx, idx + 1, width); } break; case 3: // three sort if((*compar_func)(SUB(base_pointer, idx), SUB(base_pointer, idx + 1)) > 0){ SWAP(base_pointer, idx, idx + 1, width); } // the first two are now ordered, now order the second two if((*compar_func)(SUB(base_pointer, idx + 2), SUB(base_pointer, idx + 1)) < 0){ SWAP(base_pointer, idx + 2, idx + 1, width); } // should the second be moved to the first? if((*compar_func)(SUB(base_pointer, idx + 1), SUB(base_pointer, idx)) < 0) { SWAP(base_pointer, idx + 1, idx, width); } break; default: // reinit the sorting arguments sort_args[1].sortargs_base = SUB(base_pointer, idx); sort_args[1].sortargs_number_of_elements = jdx; sort_args[1].sortargs_width = width; sort_args[1].sortargs_compar_func = compar_func; // call _quicksort recursively, creating threads if possible if((thread_count == 0) && (threads_avail > 0) && (idx > SLICE_THRESH)) { threads_avail--; pthread_create(&tid, NULL, _quicksort, &sort_args[1]); thread_count = 1; }else{ _quicksort(&sort_args[1]); } break; } if(thread_count){ pthread_join(tid, NULL); threads_avail++; } return 0; }