// Pre-condition: low and high are valid indexes into numbers and rank is // in between 1 and high-low+1, inclusive. // Post-condition: The rank smallest element within numbers[low...high] // will be returned. Also, some elements within numbers may // change positions. int quickselect(int* numbers, int low, int high, int rank) { // Base case, this is the only number in the search range. if (low == high) return numbers[low]; // Partition the array. int split = partition(numbers,low,high); // The partition element is the correct one, so return it! if (rank == split-low+1) return numbers[split]; // We're looking for a smaller element than the partition element, so // just look to the left. else if (rank < split-low+1) return quickselect(numbers, low, split-1, rank); // We're looking for a larger element than the partition element, so // look to the right. Also, in our new array, the rank of the element // we are looking for has changed. else return quickselect(numbers, split+1, high, rank-(split-low+1)); }
/*Quick selection, avg=best=O(n) time, wrost in O(n^2)*/ int quickselect(int *p, int left, int right, int k) { int idx; if (left == right) return p[left]; idx = partition(p, left, right); if (k==idx) return p[idx]; else if (k<idx) return quickselect(p, left, idx-1, k); else return quickselect(p, idx+1, right, k); }
int AlgorithmSortQuick::quickselect(int* numbers, int left, int right, int k) { //for (int i = left; i <= right; i++) //{ // std::cout << numbers[i] << " - "; //} //std::cout << "\n "; if (left + 10 <= right) { int pivot = madian3(numbers, left, right); int i = left, j = right - 1; for ( ; ; ) { while (numbers[++i] > pivot) { } while (pivot > numbers[--j]) { } if (i < j) swap(&numbers[i], &numbers[j]); else break; } //std::cout << "\n " << numbers[i] << " - " << numbers[right - 1]; swap(&numbers[i], &numbers[right - 1]); //std::cout << "\n " << numbers[i] << " - " << numbers[right - 1]; if (k < i) { quickselect(numbers, left, i-1, k); } else if (k == i) { return numbers[i]; } else { quickselect(numbers, i + 1, right, k); } } else { int j; for (int p = 1; p < right+1; p++) { int temp = numbers[p]; for (j = p; j > 0 && temp >= numbers[j - 1]; j--) numbers[j] = numbers[j - 1]; numbers[j] = temp; } return numbers[k - 1]; } }
int Quickselect::quickselect(vector<int>& input, int l, int r, int k) { if(r-l+1 <= 10) { sort(input.begin()+l,input.begin()+r); return input[l+k-1]; } if (l >= r) return input[l]; int pivot = pivotSelection(input,l,r); int p = partition(input,l,r,pivot); int length = p - l + 1; if (length == k) return input[p]; else if (k < length) return quickselect(input,l,p-1,k); else return quickselect(input,p+1,r,k-length); }
int main() { srand(time(0)); int *list; // Allocate space for our list. list = (int*)malloc(SIZEOFLIST*sizeof(int)); // Fill it with random values. int i; for (i=0;i<SIZEOFLIST;i++) list[i] = rand(); // Print out the original values. print(list, SIZEOFLIST); // Get the rank of element they want. int rank; printf("Which ranked element do you want? (Please enter a 1-based number.)\n"); scanf("%d", &rank); // Get the answer and print it out. int answer = quickselect(list,0,SIZEOFLIST-1, rank); printf("The %d smallest element is %d\n", rank, answer); free(list); system("PAUSE"); return 0; }
int AlgorithmSortQuick::select() { int theNumber = 0; int n = 0; int input = 0; //std::cin >> n; std::ifstream file(myfile); if (file.is_open()) { file >> n; file >> this->k; file >> n; int* numbers = new int[n]; for (int i = 0; i < n; i++) { //std::cin >> input; file >> input; numbers[i] = input; } file.close(); theNumber = quickselect(numbers, 0, n-1, k); }
// Find the median in an array of values using quickselect int quickselect(int* data, size_t nelements, size_t lo, size_t hi) { if(lo < hi) { // Select the rightmost element as the pivot int pivot = data[hi]; // Partition using Lomuto partitioning size_t partition = lo; for(size_t i = lo; i < hi; i++) { // Move smaller elements to the left if(data[i] <= pivot) { // Swap the element with the partition int temp = data[i]; data[i] = data[partition]; data[partition] = temp; // Move the partition rightwards partition++; } } // Swap the pivot with the partition data[hi] = data[partition]; data[partition] = pivot; print("During: ", data, nelements); // Is the partition at the mid-point? if(partition == nelements/2) { return data[nelements/2]; } // Is the partition to the right of the mid-point? else if(partition > nelements/2) { // Recurse into the left partition return quickselect(data, nelements, lo, partition - 1); } // Is the partition to the left of the mid-point? else { // Recurse into the right partition return quickselect(data, nelements, partition + 1, hi); } } else { // Only one element left return data[lo]; } }
int main() { int a[] = {34,12,0,7,83,21,3,5,12,2}; std::cout<<"the array:\n"; for (int i=0; i!=10; ++i) std::cout<<a[i]<<' '; std::cout<<"\n---------------------------------------\n"; quickselect(a,10,4); std::cout<<"the 4-th smallest element is: "<<a[3]<<"\n"; return 0; }
int main(void) { // Create an array of unsorted data int data[] = { 23, 21, 76, 16, 43, 52, 18 }; print("Unsorted: ", data, NELEMENTS(data)); // Find the median using the quick select algorithm int median = quickselect(data, NELEMENTS(data), 0, NELEMENTS(data) - 1); print("Partly sorted: ", data, NELEMENTS(data)); printf("Median value: %d\n", median); return EXIT_SUCCESS; }
int quickselect(int B[],int start,int end,int k){ int m,i,tmp; int pivot; if(end ==start+1) return B[start]; pivot=B[start]; m=start+1; for(i=start+1;i<end;i++){ if(B[i]<=pivot){ tmp=B[m]; B[m]=B[i]; B[i]=tmp; m++; n+=1; } } if(end==m+k) return pivot; else if(end<m+k) return quickselect(B,start+1,m,k-(end-m)-1); else return quickselect(B,m,end,k); }
int main (void) { // Read the array in first int ** matrix = malloc (sizeof (int*) * DIMENSIONS); int index = 0; while (index < DIMENSIONS) { matrix[index] = malloc (sizeof (int) * DIMENSIONS); index++; } srand (time(NULL)); int col = 0; while (col < DIMENSIONS) { int row = 0; while (row < DIMENSIONS) { int value = rand() % 100; if (value < 100-FREQUENCY) { matrix[col][row] = 0; } else { matrix[col][row] = 1; } row++; } col++; } // Show the matrix show_array (matrix); // Keep track of values in an array int nextFree = 0; int * areas = malloc (sizeof (int) * DIMENSIONS * DIMENSIONS); // Parse matrix parse_array (matrix, areas, &nextFree); // See what values we come up with index = 0; printf ("AREAS: "); while (index < nextFree) { printf ("%d ", areas[index]); index++; } printf("\n"); // Perform quick select for median. printf ("Median Area: %d\n", quickselect (areas, nextFree - 1)); return EXIT_SUCCESS; }
int main(){ int i; int A[]={6,1,20,3,8,9,2,7,10,6}; int C[10]; for(i=0;i<10;i++){ C[9-i]=quickselect(A,0,10,i); } for(i=0;i<10;i++){ printf("%d ",C[i]); } printf("O=%d",n); return 0; }
int main(int argc, char **argv) { int n, k, *p; n = (argc>1)? atoi(argv[1]) : 10; k = (argc>2)? atoi(argv[2]) : n/2; k = (k > n)? k%n:k; p = create_array(n); printf("n=%d k=%d\n", n, k); print_array(p, n); printf("%dth=%d\n", k, quickselect(p, 0, n-1, k-1)); print_array(p, n); /*after full sorting, for manual review*/ printf("sort\n"); qsort(p, n, 4, cmp); print_array(p, n); free(p); return 0; }
int main() { int n = 1000; int a[n], j, r; srand(time(NULL)); for (int i = 0; i < n; i++) { a[i] = rand() % 1000; printf("%d\t", a[i]); } j = 100; r = quickselect(a, 0, n - 1, j); printf("\n\n%dth: %d\n", j, a[r]); for (int i = 0; i < n; i++) { if (r == i) { printf("\033[0;31m"); } printf("%d\t\033[0m", a[i]); } return 0; }
int kd_build(Kdtree *data, Kdtree *kd_tree) { int axis, i, level, c_level; int left, right, current, c_index, parent; //int ind[3]; Interval ind; Interval *res; Queue q; if (data->size == 0) { kd_tree->nodes = NULL; kd_tree->size = 0; return 0; } level = ceil(log(data->size + 1)/log(2)); kd_tree->size = (int)pow(2, level) - 1; kd_tree->nodes = (Node*)calloc(kd_tree->size, sizeof(Node)); if (NULL == kd_tree->nodes) return -1; init_queue(&q, data->size); left = 0; right = data->size - 1; current = (left + right) / 2; ind.left = left; ind.right = right; ind.parent = -1; enqueue(&q, &ind); i = 0; while(!empty(&q)) { res = (Interval*)dequeue(&q); left = res->left; right = res->right; current = (left + right) / 2; c_level = ((int)floor(log(2 * (i + 1))/log(2)) - 1); axis = c_level % 3; c_index = i; parent = res->parent; if (c_level == level - 1 && ((data->size + 1) & data->size) != 0) { c_index = 2 * parent + 2; } else { quickselect(data, left, right, axis); } memcpy(&(kd_tree->nodes[c_index]), &(data->nodes[current]), sizeof(Node)); kd_tree->nodes[c_index].fill = true; if (left <= current - 1) { ind.left = left; ind.right = current - 1; ind.parent = i; enqueue(&q, &ind); } if (current + 1 <= right) { ind.left = current + 1; ind.right = right; ind.parent = i; enqueue(&q, &ind); } i++; } // Free array of indexes /*for(i = 0; i < data->size; i++) { free(q.q[i]); } free(q.q); */ return 0; }