// Similar to above, but "bool" type needs special handling template<typename T1> void quick_sort(vector<T1> &one, vector <bool> &two, unsigned int beg, unsigned int end) { if (end > beg + 1) { T1 piv = one[beg]; unsigned int l = beg + 1; unsigned int r = end; while (l < r) { if (one[l] <= piv) { l++; } else { --r; quick_swap(one[l], one[r]); bool two_l = two[l]; bool two_r = two[r]; quick_swap(two_l, two_r); two[l] = two_l; two[r] = two_r; } } --l; quick_swap(one[l], one[beg]); bool two_l = two[l]; bool two_beg = two[beg]; quick_swap(two_l, two_beg); two[l] = two_l; two[beg] = two_beg; quick_sort(one, two, beg, l); quick_sort(one, two, r, end); } }
void rra(t_stack *stack, int x) { t_ilist *tmp; if (stack->number_in_a == 2) quick_swap(stack); else { tmp = stack->a_begin; stack->a_begin = stack->a_begin->next; stack->a_begin->prev = NULL; i_list_push(&stack->a_begin, tmp); stack->a_end = tmp; } ++stack->n_moves; if (x && !stack->no_command) s_list_push(&stack->moves, s_list_new(ft_strdup("rra"))); if (stack->status && x) { print_stack(&stack->a_begin, stack->number_in_a, 'a', stack->color); print_stack(&stack->b_begin, stack->number_in_b, 'b', stack->color); if (!stack->no_command) print_command(stack); write(1, "\n", 1); } }
/* A regular single vector sorting routine */ void quick_sort(vector < ustring > &one, unsigned int beg, unsigned int end) { if (end > beg + 1) { ustring piv = one[beg]; unsigned int l = beg + 1; unsigned int r = end; while (l < r) { if (one[l] <= piv) { l++; } else { --r; quick_swap(one[l], one[r]); } } --l; quick_swap(one[l], one[beg]); quick_sort(one, beg, l); quick_sort(one, r, end); } }
int quick_swap( int *array, int size ) { if( size < 1 ) return 0; //int somewhere = size / 2; int somewhere = rand()%size; int pivot = array[somewhere]; array[somewhere] = *(array); int p = 0; for ( int i = 0 + 1; i <= size; i++) { if ( *(array+i) < pivot) { p++; swap( array+p, array+i); } } *(array) = *(array+p); *(array+p) = pivot; disp( array, p ); quick_swap( array, p-1 ); disp( array+ p, size - p ); quick_swap( array+ p+1, size - p - 1 ); return 0; }
// Function template for sorting the containers template<typename T1, typename T2> void quick_sort(vector<T1> &one, vector <T2> &two, unsigned int beg, unsigned int end) { if (end > beg + 1) { T1 piv = one[beg]; unsigned int l = beg + 1; unsigned int r = end; while (l < r) { if (one[l] <= piv) { l++; } else { --r; quick_swap(one[l], one[r]); quick_swap(two[l], two[r]); } } --l; quick_swap(one[l], one[beg]); quick_swap(two[l], two[beg]); quick_sort(one, two, beg, l); quick_sort(one, two, r, end); } }
int quicksort( int *array, int size ) { int *input = ( int* )malloc( sizeof(int) * size ); for( int i = 0; i < size; i++ ) { *(input+i) = *(array+i); } printf( "////////////////////////////////////////\n" ); printf( "// quick sort start\n" ); disp( input , size ); quick_swap( input, size - 1 ); printf( "// result\n" ); disp( input , size ); free( input ); return 0; }