int main(void) { int len = 50, array[len], backup[len]; random_int_array(array, len, 100); for(int i = 0; i < len; i++) { backup[i] = array[i]; } int_array_output(array, len); // insert_sort(array, len); shell_sort(array, len); int_array_output(array, len); int idx = binary_search(array, len, 10); printf("%d \n", idx); /* if(in_array(array, backup, len) == false) { printf("Fail in test \n"); } */ if(assert_int_array_increasing(array, len) == false) { printf("Fail in test \n"); } int a = 2, b = 3; printf("(%d + %d) / 2 = %d \n", a, b, (int)floor((a + b) / 2.0)); return 0; }
Point run(void* arri_1, void* arri_2, size_t num, size_t size, int (*compar) (const void*, const void*)) { clock_t start; //assert(are_equal(arri_1, arri_2, num, size, compar)); clock_t difference; clock_t difference_std; start = clock(); qsort(arri_2, num, size, compar); difference_std = clock() - start; //assert(!is_ordered(arri_2, num, size, compar)); //time the standard library start = clock(); my_qsort(arri_1, num, size, compar); difference = clock() - start; //we're just testing for speed now //print_int_array(arri_1, num); //print_int_array(arri_2, num); assert(are_equal(arri_1, arri_2, num, size, compar)); free(arri_1); free(arri_2); Point p = { .x = difference * 1000/CLOCKS_PER_SEC, .y = difference_std * 1000/CLOCKS_PER_SEC}; return p; } Point test_int_array(size_t num, size_t size) { void* compar = &compar_int; int* arri_1 = random_int_array(num); int* arri_2 = (int*) duplicate_array(arri_1, num, size); return run(arri_1, arri_2, num, size, compar); }
int main(int argc, char **argv) { int elements = ArraySz; if(argc > 1) elements = atoi(argv[1]); struct timespec start, stop; int numtask=0; // no of tasks int work[ArraySz/2]; // max size // Created random array int *a = random_int_array(elements, elements/2, 13); printf("\nArray before Sorting:\n"); // Print array print_array(a, elements); clock_gettime(CLOCK_MONOTONIC, &start); // quicksort(a, 0, elements-1); // create threads pthread_t * threads; struct pthread_args * t_arg; threads = malloc(NUMTHREADS * sizeof(*threads)); t_arg= malloc(NUMTHREADS * sizeof(*t_arg)); for( int i=0; i < NUMTHREADS; i++ ) { t_arg[i].numtask = &numtask; t_arg[i].work = work; t_arg[i].a = a; t_arg[i].id = i; pthread_create( threads+i, NULL, &doWork, t_arg+i ); pthread_join( threads[i], NULL ); } free(threads); free(t_arg); clock_gettime(CLOCK_MONOTONIC, &stop); #if VERBOSE // print workarray for( int i=0; i< ArraySz/2; i++) { printf("\nwork[%d]=%d\t", i,work[i]); i++; printf("work[%d]=%d", i,work[i]); } printf("\n\n"); #endif printf("\nArray after Sorting:\n"); print_array(a, elements); printf("\nTime = %lf\n", time_diff(&start, &stop, NULL)); return 0; }
int main(int argc, char **argv) { int elements = ELEMS; int num_threads = NTHREADS; int dump_array = 0; int c; while ((c = getopt(argc, argv, "t:e:d:")) != -1) { switch (c) { case 't': if (sscanf(optarg, "%d", &num_threads) != 1) goto error; break; case 'e': if (sscanf(optarg, "%d", &elements) != 1) goto error; break; case 'd': if (sscanf(optarg, "%d", &dump_array) != 1) goto error; break; case '?': error: printf( "Usage:\n" "-t \t number of threads used in computation\n" "-e \t number of elements to be sorted using quicksort\n" "-d \t dump array option (0 or 1)\n" "\n" "Example:\n" "%s -t 4 -e 10000000 -d 1\n", argv[0]); exit(EXIT_FAILURE); break; } } struct timespec start, stop; int *a = random_int_array(elements, elements/2, 13); if (dump_array == 1) print_array(a, elements); clock_gettime(CLOCK_MONOTONIC, &start); quicksort(a, 0, elements-1, num_threads); clock_gettime(CLOCK_MONOTONIC, &stop); if (dump_array == 1) print_array(a, elements); printf("\nTime: %lf seconds\n", time_diff(&start, &stop, NULL)); return 0; }
int main (int argc, char *argv[]) { int *numbers = random_int_array(10); print_int_array(numbers); printf("=====================\n"); gsort(numbers, bubble_sort, int_natural_order); print_int_array(numbers); printf("=====================\n"); gsort(numbers, bubble_sort, int_reverse_order); print_int_array(numbers); return 0; }
void main(int argc, char **argv) { int elements = 27; if(argc > 1) elements = atoi(argv[1]); struct timespec start, stop; int *a = random_int_array(elements, elements/2, 13); print_array(a, elements); clock_gettime(CLOCK_MONOTONIC, &start); quicksort(a, 0, elements-1); clock_gettime(CLOCK_MONOTONIC, &stop); print_array(a, elements); printf("\nTime = %lf\n", time_diff(&start, &stop, NULL)); }
void basic_test(size_t times, size_t size, size_t increment) { int i, j; double int_time = 0, double_time = 0, float_time = 0, long_time = 0, point_time = 0; double int_time_single, double_time_single, long_time_single, point_time_single, float_time_single; for (j = 0; j < size; j += increment) { #pragma omp parallel for for (i = 0; i < times; ++i) { int_time_single = test_array(random_int_array(j), j, sizeof(int), *compar_int); double_time_single = test_array(random_double_array(j), j, sizeof(double), *compar_double); float_time_single = test_array(random_float_array(j), j, sizeof(float), *compar_float); long_time_single = test_array(random_long_array(j), j, sizeof(long), *compar_long); point_time_single = test_array(random_point_array(j), j, sizeof(Point), *compar_point); #pragma omp atomic int_time += int_time_single; #pragma omp atomic double_time += double_time_single; #pragma omp atomic long_time += long_time_single; #pragma omp atomic float_time += float_time_single; #pragma omp atomic point_time += point_time_single; } printf("(int) array_size: %d, sort_time (ms): %f\n", j, int_time/times); printf("(double) array_size: %d, sort_time (ms): %f\n", j, double_time/times); printf("(float) array_size: %d, sort_time (ms): %f\n", j, float_time/times); printf("(long) array_size: %d, sort_time (ms): %f\n", j, long_time/times); printf("(point) array_size: %d, sort_time (ms): %f\n", j, point_time/times); printf("\n"); int_time = double_time = float_time = long_time = point_time = 0; } }