int QuickSort::sort3(Array &array, int left, int right) const { if(left >= right) { // do nothing if less than 1 elements return 0; } int t = array[left]; int i = left; int j = right + 1; for(;;) { do { i++; } while (i <= right && array[i] < t); do { j--; } while (array[j] > t); if(i>j) { break; } array.Swap(i,j); } array.Swap(left, j); sort3(array, left, j - 1); sort3(array, j + 1,right); return 0; }
int* sort3_wrapper(int *conds, int *out, int *in) { __disjoint_regions(conds,3,out,3); __disjoint_regions(conds,3,in,3); __disjoint_regions(out,3,in,3); /* Boilerplate */ public_in(__SMACK_value(conds)); public_in(__SMACK_value(out)); public_in(__SMACK_value(in)); /* Useful */ declassified_out(__SMACK_values(conds,3)); /* Testing out more of the assertion generation */ public_in(__SMACK_values(conds,3)); public_out(__SMACK_values(conds,3)); public_out(__SMACK_return_value()); // This is broken for now, but we should ignore it until we see an // example that works like this... //declassified_out(__SMACK_return_values(__SMACK_return_value(),3)); sort3(conds,out,in); return conds; }
int main() { int length, index; printf("Enter length of array\n"); scanf("%d", &length); int *array, *array1, *array2; array = (int*)malloc(100*sizeof(int)); array1 = (int*)malloc(100*sizeof(int)); array2 = (int*)malloc(100*sizeof(int)); for (index = 0; index < length; index++) { scanf("%d", &array[index]); if ((array[index]<0) || (array[index]>1)) { printf("Invalid input-enter either o or 1"); return 0; } array1[index] = array[index]; array2[index] = array[index]; } sort1(array, length); for (index = 0; index < length; index++) printf("%d\t", array[index]); sort2(array1, length); for (index = 0; index < length; index++) printf("%d\t", array1[index]); sort3(array2, length); for (index = 0; index < length; index++) printf("%d\t", array2[index]); getch(); }
void MMA8451::pickle(float *result, uint32_t still_msk) { (void)still_msk; int32_t raw[3]; raw[0] = complement2signed(rxbuf[1], rxbuf[2]); raw[1] = complement2signed(rxbuf[3], rxbuf[4]); raw[2] = complement2signed(rxbuf[5], rxbuf[6]); /* convert to NUE coordinate system */ sort3(raw, *sortmtrx); raw[0] *= *xpol; raw[1] *= *ypol; raw[2] *= *zpol; mavlink_out_raw_imu_struct.xacc = raw[0]; mavlink_out_raw_imu_struct.yacc = raw[1]; mavlink_out_raw_imu_struct.zacc = raw[2]; comp_data.acc_i16[0] = (1000 * (raw[0] + *xoffset)) / *xsens; comp_data.acc_i16[1] = (1000 * (raw[1] + *yoffset)) / *ysens; comp_data.acc_i16[2] = (1000 * (raw[2] + *zoffset)) / *zsens; result[0] = comp_data.acc_i16[0] / 1000.0f; result[1] = comp_data.acc_i16[1] / 1000.0f; result[2] = comp_data.acc_i16[2] / 1000.0f; mavlink_out_scaled_imu_struct.xacc = comp_data.acc_i16[0]; mavlink_out_scaled_imu_struct.yacc = comp_data.acc_i16[1]; mavlink_out_scaled_imu_struct.zacc = comp_data.acc_i16[2]; //mavlink_out_scaled_imu_struct.zacc = vector3d_modulus(result) * 1000; }
int main() { int v1 = 10; int v2 = 5; int v3 = 1; sort3(&v1, &v2, &v3); printf("v1=%d, v2=%d, v3=%d\n", v1, v2, v3); }
int main () { int v = 3; int w = 4; int x = 1; sort3(v,w,x); cout << "v : " << v << endl<< "w : " << w << endl << "x : " << x << endl; }
int main() { for(;;) { int a,b,c; scanf("%d,%d,%d",&a,&b,&c); sort3(&a,&b,&c); printf("%d,%d,%d\n",a,b,c); } return 0; }
int main(void){ int l = 89; int m = 1; int n = 13; printf("before: l=%2d, m=%2d, n=%2d\n", l, m, n); sort3(&l, &m, &n); printf(" after: l=%2d, m=%2d, n=%2d\n", l, m, n); return 0; }
int main (void) { double x, y, z; printf("Enter three numbers: "); scanf("%lf %lf %lf", &x, &y, &z); sort3(&x, &y, &z); printf("The ordered sequence is: %.1lf %.1lf %.1lf\n", x, y, z); return (0); }
int main (int argc, char **argv) { int i, count = 1000000; double stime; int *unsorted, *sorted, num_threads; if (argc >= 2) count = strtoul (argv[1], NULL, 0); unsorted = malloc (count * sizeof (int)); sorted = malloc (count * sizeof (int)); if (unsorted == NULL || sorted == NULL) { puts ("allocation failure"); exit (1); } srand (0xdeadbeef); for (i = 0; i < count; i++) unsorted[i] = rand (); omp_set_nested (1); omp_set_dynamic (0); #pragma omp parallel #pragma omp single nowait num_threads = omp_get_num_threads (); printf ("Threads: %d\n", num_threads); memcpy (sorted, unsorted, count * sizeof (int)); stime = omp_get_wtime (); sort1 (sorted, count); verify ("sort1", stime, sorted, count); memcpy (sorted, unsorted, count * sizeof (int)); stime = omp_get_wtime (); sort2 (sorted, count); verify ("sort2", stime, sorted, count); #if _OPENMP >= 200805 memcpy (sorted, unsorted, count * sizeof (int)); stime = omp_get_wtime (); sort3 (sorted, count); verify ("sort3", stime, sorted, count); #endif return 0; }
void main() { int a[100], b[100]; int n, i, sum = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i]!=0 && a[i]!=1) { printf("Please Enter only 0's and 1's\n"); i--; } else { sum = sum + a[i]; } } sort1(a, n,b); for (i = 0; i < n; i++) { printf("%d", b[i]); } printf("\n"); sort2(a, n, b,sum); for (i = 0; i < n; i++) { printf("%d", b[i]); } printf("\n"); sort3(a, n); for (i = 0; i < n; i++) { printf("%d", a[i]); } printf("\n"); getch(); }
void sort3(int iFrom, int iTo) { // iTo=19; int d=iFrom+getPow(iTo-iFrom); //if(d<2)return; sort2(iFrom,d); if(d!=iTo) { // printf("-------------marl=%d\n",d-iFrom); int iDif=iTo-d; if(iDif==1) { } else if(iDif==2) { join(d,d+1,iTo); } else if(iDif==3) { join(d,d+1,d+2); join(d,d+2,iTo); } /* else if(iTo-d<16)//shis var buut 0 { sort4(d,iTo); } */ else { sort3(d,iTo); } join(iFrom,d,iTo); } }
int main(void){ int input[3]; int i; int* pInput=input; printf("Bitte geben Sie 3 Zahlen an, " "um sie der Groesse nach zu sortieren.\n"); for(i=0;i<ARRSIZE(input);i++){ scanf("%d", &*(pInput+i)); } /* Nachdem sizeof() bei Pointer nicht so funktioniert wie bei Arrays wird die Arraygroesse der Funktion mit uebergeben. */ sort3(input,ARRSIZE(input)); printf("\n\nIhre Zahlen in sortierter Reihenfolge:\n"); for(i=0;i<ARRSIZE(input);i++){ printf("%d\n", *(pInput+i)); } return 0; }
static void compare(int32_t *a, int32_t n) { int32_t *b; int32_t i; double runtime; b = (int32_t *) safe_malloc((n + 1) * sizeof(int32_t)); runtime = get_cpu_time(); for (i=0; i<5000; i++) { copy_array(b, a, n); insertion_sort(b, n); } runtime = get_cpu_time() - runtime; // printf("isort: %.3f s\n", runtime); itime += runtime; runtime = get_cpu_time(); for (i=0; i<5000; i++) { copy_array(b, a, n); sort(b, n); } runtime = get_cpu_time() - runtime; // printf("sort1: %.3f s\n", runtime); time1 += runtime; runtime = get_cpu_time(); for (i=0; i<5000; i++) { copy_array(b, a, n); sort2(b, n); } runtime = get_cpu_time() - runtime; // printf("sort2: %.3f s\n", runtime); time2 += runtime; runtime = get_cpu_time(); for (i=0; i<5000; i++) { copy_array(b, a, n); sort3(b, n); } runtime = get_cpu_time() - runtime; // printf("sort3: %.3f s\n", runtime); time3 += runtime; runtime = get_cpu_time(); for (i=0; i<5000; i++) { copy_array(b, a, n); sort4(b, n); } runtime = get_cpu_time() - runtime; // printf("sort4: %.3f s\n\n", runtime); time4 += runtime; runtime = get_cpu_time(); for (i=0; i<5000; i++) { copy_array(b, a, n); sort4var(b, n); } runtime = get_cpu_time() - runtime; // printf("sort4var: %.3f s\n\n", runtime); time4var += runtime; runtime = get_cpu_time(); for (i=0; i<5000; i++) { copy_array(b, a, n); sort4var2(b, n); } runtime = get_cpu_time() - runtime; // printf("sort4var2: %.3f s\n\n", runtime); time4var2 += runtime; safe_free(b); }
int main(int argc, char ** argv) { size_t n = DB::parse<size_t>(argv[1]); size_t method = DB::parse<size_t>(argv[2]); std::vector<Key> data(n); // srand(time(0)); { Stopwatch watch; for (auto & elem : data) elem = rand(); watch.stop(); double elapsed = watch.elapsedSeconds(); std::cerr << "Filled in " << elapsed << " (" << n / elapsed << " elem/sec., " << n * sizeof(Key) / elapsed / 1048576 << " MB/sec.)" << std::endl; } if (n <= 100) { std::cerr << std::endl; for (const auto & elem : data) std::cerr << elem << ' '; std::cerr << std::endl; } { Stopwatch watch; if (method == 1) sort1(&data[0], n); if (method == 2) sort2(&data[0], n); if (method == 3) sort3(&data[0], n); watch.stop(); double elapsed = watch.elapsedSeconds(); std::cerr << "Sorted in " << elapsed << " (" << n / elapsed << " elem/sec., " << n * sizeof(Key) / elapsed / 1048576 << " MB/sec.)" << std::endl; } { Stopwatch watch; size_t i = 1; while (i < n) { if (!(data[i - 1] <= data[i])) break; ++i; } watch.stop(); double elapsed = watch.elapsedSeconds(); std::cerr << "Checked in " << elapsed << " (" << n / elapsed << " elem/sec., " << n * sizeof(Key) / elapsed / 1048576 << " MB/sec.)" << std::endl << "Result: " << (i == n ? "Ok." : "Fail!") << std::endl; } if (n <= 1000) { std::cerr << std::endl; std::cerr << data[0] << ' '; for (size_t i = 1; i < n; ++i) { if (!(data[i - 1] <= data[i])) std::cerr << "*** "; std::cerr << data[i] << ' '; } std::cerr << std::endl; } return 0; }
main() { // variable to store the users option selection, char used instead of int for error checking purposes char option; // array to store the user's selected numbers int user_nums[USER_SIZE]; // validity check, variable for passing to function enter_nums1(), to check if user's input is valid before allowing access to options 2-6 int valid = 0; // frequency check, variable for passing to function check_frequency5(), to check if the user has entered new numbers before incrementing the frequency again int flag_freq = 0; // welcome message to user print_welcome(); // do-while option !=6, this loops around after each option ends and reprints the menu, asking for the user's next option choice do { // print the menu for user and ask their menu option print_menu(); // scanf to hold the user option choice, using a char type to assist in error checking the user's input scanf("%1s", &option); // switch statement to control the menu options (1-6) switch(option) { // Menu Option 1: user selects 6 numbers from the range 1 to 42 case '1': { // call function 'enter_nums1' enter_nums1(&valid, &flag_freq, user_nums); // break out of option 1 break; } // end case 1 // Menu Option 2: display user numbers case '2': { // control to prevent the user beginning the game without having first selected menu option 1 (enter numbers) if (valid == 0) { printf("\n*** ERROR: You must enter valid numbers first!\n"); } // end if() else { // call function 'display_user_nums2' display_user_nums2(user_nums); } // end else // break out of option 2 break; } // end case 2 // Menu Option 3: Sort the user's numbers case '3': { // control to prevent the user beginning the game without having first selected menu option 1 (enter numbers) if (valid == 0) { printf("\n*** ERROR: You must enter valid numbers first!\n"); } // end if() else { // call function 'sort3' sort3(user_nums); } // end else // break out of option 3 break; } // end case 3, sorting numbers // Menu Option 4: Check winnings case '4': { // control to prevent the user beginning the game without having first selected menu option 1 (enter numbers) if (valid == 0) { printf("\n*** ERROR: You must enter valid numbers first!\n"); } // end if() else { // call function 'check_nums4' check_wins4(user_nums); } // end else // break out of option 4 break; } // end case 4, check for winnings // Menu Option 5: Check Frequency case '5': { // control to prevent the user beginning the game without having first selected menu option 1 (enter numbers) if (valid == 0) { printf("\n*** ERROR: You must enter valid numbers first!\n"); } // end if() else { // call function 'check_frequency5' check_frequency5(&flag_freq, user_nums); } // end else // break out of option 5 break; } // end case 5, check frequency // Menu Option 6: Exit Game case '6': { // if the user selects char '6', it must be converted to an int value in order to exit the do-while loop and therefore the program option = 6; getchar(); // print exit message to user printf("\n\t\t\tYou selected Option 6 - Exit Game\n"); printf("\n Thank You for Playing - Goodbye!\n"); printf("\n - Press 'enter' to exit."); // break out of option 6 break; } // end case 6, game ends // Default, if option != 1, 2, 3, 4, 5, 6 default: { // print error message printf("\n*** ERROR: Please enter a VALID OPTION 1-6\n"); // break out of switch_default break; } // end default } // end switch (option) } // end do-while while (option !=6); // to keep the window open for the user getchar(); } // end main()