prepare to incorporate quickselect into squiggle_more

This commit is contained in:
NunoSempere 2023-11-29 21:45:42 +00:00
parent 578bfa2798
commit 03ca3e3b0c
2 changed files with 11 additions and 31 deletions

Binary file not shown.

View File

@ -2,13 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
void swap_pointers(double *x, double* y){ void swp(int i, int j, double xs[]){
double tmp = *x;
*x = *y;
*y = tmp;
}
void swap_in_array(int i, int j, double xs[]){
double tmp = xs[i]; double tmp = xs[i];
xs[i] = xs[j]; xs[i] = xs[j];
xs[j] = tmp; xs[j] = tmp;
@ -23,32 +17,20 @@ void array_print(double xs[], int n){
} }
int partition(int low, int high, double xs[], int length){ int partition(int low, int high, double xs[], int length){
// srand(2); // To understand this function:
array_print(xs, length); // - see the note after gt variable definition
int pivot = low + floor((high-low)/2); // low + floor(rand() % (high - low + 1)); // - go to commit 578bfa27 and the scratchpad/ folder in it, which has printfs sprinkled throughout
int pivot = low + floor((high-low)/2);
double pivot_value = xs[pivot]; double pivot_value = xs[pivot];
printf("pivot_value: #%f\n", pivot_value); swp(pivot, high, xs);
swap_in_array(pivot, high, xs);
int gt = low; /* This pointer will iterate until finding an element which is greater than the pivot. Then it will move elements that are smaller before it--more specifically, it will move elements to its position and then increment. As a result all elements between gt and i will be greater than the pivot. */ int gt = low; /* This pointer will iterate until finding an element which is greater than the pivot. Then it will move elements that are smaller before it--more specifically, it will move elements to its position and then increment. As a result all elements between gt and i will be greater than the pivot. */
for(int i=low; i<high; i++){ for(int i=low; i<high; i++){
printf("Step: #%d\n", i);
printf(" Before: \n");
printf(" gt: %d\n", gt);
array_print(xs, length);
if(xs[i] < pivot_value){ if(xs[i] < pivot_value){
printf(" Swaping element #%d: %f and element #%d: %f\n", gt, xs[gt], i, xs[i]); swp(gt, i, xs);
swap_in_array(gt, i, xs);
gt++; gt++;
} }
printf(" After: \n");
array_print(xs, length);
printf(" gt: %d\n", gt);
} }
printf("Step: Swap with last item\n"); swp(high, gt, xs);
swap_in_array(high, gt, xs);
printf(" After: \n");
array_print(xs, length);
printf(" gt: %d\n", gt);
return gt; return gt;
} }
@ -74,11 +56,9 @@ int main(){
double xs[] = {2.1, 1.0, 6.0, 4.0, 7.0, -1.0, 2.0, 10.0}; double xs[] = {2.1, 1.0, 6.0, 4.0, 7.0, -1.0, 2.0, 10.0};
int length = 8; int length = 8;
int k = 2; int k = 2;
// partition(0, length-1, xs, length);
double result = quickselect(k, xs, length);
array_print(xs, 8); array_print(xs, 8);
double result = quickselect(k, xs, length);
printf("The item in pos #%d is: %f\n", k, result); printf("The item in pos #%d is: %f\n", k, result);
array_print(xs, 8);
return 0; return 0;
} }