void run_all_except_insertion(int random_arr[], int sorted_arr[], int length) { //copy of the input arrays to keep them intact int random_arr_copy[length]; int sorted_arr_copy[length]; //double arrays with running times double sorted_times[4]; double random_times[4]; //clock_t struct returned by the clock function for start and end times clock_t start, end; int ndx = 0; copy_array(random_arr, random_arr_copy, length); copy_array(sorted_arr, sorted_arr_copy, length); //run quicksort getting elapsed time for each start = clock(); quick_sort(random_arr_copy, 0, length - 1); end = clock(); copy_array(random_arr, random_arr_copy, length); random_times[ndx] = ((double)(end - start)) / CLOCKS_PER_SEC; start = clock(); quick_sort(sorted_arr_copy, 0, length - 1); end = clock(); copy_array(sorted_arr, sorted_arr_copy, length); sorted_times[ndx++] = ((double)(end - start)) / CLOCKS_PER_SEC; //run heapsort start = clock(); heap_sort(random_arr_copy, length, length); end = clock(); copy_array(random_arr, random_arr_copy, length); random_times[ndx] = ((double)(end - start)) / CLOCKS_PER_SEC; start = clock(); heap_sort(sorted_arr_copy, length, length); end = clock(); copy_array(sorted_arr, sorted_arr_copy, length); sorted_times[ndx++] = ((double)(end - start)) / CLOCKS_PER_SEC; //run merge sort start = clock(); merge_sort(random_arr_copy, 0, length - 1); end = clock(); copy_array(random_arr, random_arr_copy, length); random_times[ndx] = ((double)(end - start)) / CLOCKS_PER_SEC; start = clock(); merge_sort(sorted_arr_copy, 0, length - 1); end = clock(); copy_array(sorted_arr, sorted_arr_copy, length); sorted_times[ndx++] = ((double)(end - start)) / CLOCKS_PER_SEC; output_table(random_times, sorted_times); }
void heap_sort(node_heap *root, int *vec, int *size) { if (root->l != 0) heap_sort(root->l, vec, size); *size = *size + 1; vec[*size] = root->data; if (root->r != 0) heap_sort(root->r, vec, size); }
static int process(const char *fpath_bin, const char *fpath_uniq, const char *fpath_text) { #if INTERFACE_TYPE == 0 FILE *fp_bin = fopen(fpath_bin, "r+b"); if (!fp_bin) return -1; fseek(fp_bin, 0, SEEK_END); // sel_sort(ftell(fp_bin)/sizeof(rec_t), sizeof(rec_t), fp_bin, rec_stdio_getter, rec_stdio_setter, rec_stdio_cmp); heap_sort(ftell(fp_bin)/sizeof(rec_t), sizeof(rec_t), fp_bin, rec_stdio_getter, rec_stdio_setter, rec_stdio_cmp); fclose(fp_bin); #elif INTERFACE_TYPE == 1 || INTERFACE_TYPE == 2 || INTERFACE_TYPE == 3 int fd_bin = open(fpath_bin, O_RDWR); if (fd_bin == -1) return -1; #if INTERFACE_TYPE == 2 off_t f_len = lseek(fd_bin, 0, SEEK_END); lseek(fd_bin, 0, SEEK_SET); rec_t *rec_l = (rec_t*)mmap(NULL, f_len, PROT_WRITE, MAP_SHARED, fd_bin, 0); heap_sort(f_len/sizeof(rec_t), sizeof(rec_t), rec_l, rec_mem_getter, rec_mem_setter, rec_mem_cmp); munmap(rec_l, f_len); #elif INTERFACE_TYPE == 3 off_t f_len = lseek(fd_bin, 0, SEEK_END); rec_t *rec_l = (rec_t*)malloc(f_len); lseek(fd_bin, 0, SEEK_SET); read(fd_bin, rec_l, f_len); heap_sort(f_len/sizeof(rec_t), sizeof(rec_t), rec_l, rec_mem_getter, rec_mem_setter, rec_mem_cmp); lseek(fd_bin, 0, SEEK_SET); write(fd_bin, rec_l, f_len); free(rec_l); #else heap_sort(lseek(fd_bin, 0, SEEK_END)/sizeof(rec_t), sizeof(rec_t), (void*)fd_bin, rec_posix_getter, rec_posix_setter, rec_posix_cmp); #endif close(fd_bin); #endif FILE *fp_uniq = uniq(fpath_bin, fpath_uniq); write_to_text(fpath_text, fp_uniq); fclose(fp_uniq); return 0; }
int main() { int index; heap_t heap; scanf("%d", &heap.size); heap.heap_size = heap.size; heap.array = (int *)malloc(sizeof(int) * heap.size); if(!heap.array) { printf("ERROR: malloc()\n"); return 0; } memset(heap.array, 0, sizeof(int) * heap.size); for(index = 0; index < heap.size; index += 1) scanf("%d", &(heap.array[index])); printf("\nInput array: "); for(index = 0; index < heap.size; index += 1) printf("%d ", heap.array[index]); heap_sort(heap); printf("\nSorted array: "); for(index = 0; index < heap.size; index += 1) printf("%d ", heap.array[index]); free(heap.array); return 0; }
void run_sorts(buf *buf) { fill(buf); while(1) { int r = rand_from(0, 4); fisher_yates_shuffle(buf, 0, buf->length-1); switch (r) { case 0: printf("Insertion Sort.\n"); insertion_sort(buf, 0, buf->length-1); break; case 1: printf("Quick Sort.\n"); quicksort(buf, 0, buf->length-1); break; case 2: printf("Heap Sort.\n"); heap_sort(buf, 0, buf->length-1); break; case 3: printf("Selection Sort.\n"); selection_sort(buf, 0, buf->length-1); break; case 4: printf("Merge Sort.\n"); merge_sort(buf, 0, buf->length-1); break; } } }
int main() { int select, size, *test; while (select = menu()) { size = get_integer("Array size"); while(size <= 0) size = get_integer("Array size must bigger then zero"); test = init_array(test, size); printf("Before sort:\n"); print_array(test, size); switch (select) { case 1: insertion_sort(test,size); break; case 2: shell_sort(test, size); break; case 3: heap_sort(test, size); break; case 4: merge_sort(test, size); break; case 5: quick_sort(test, size); break; } printf("After sort:\n"); print_array(test, size); } return 0; }
void main(void) { int arr[10]={9,8,70,6,5,4,3,2,1,0}; heap_sort(arr,10,1); int i; for (i = 0; i < 10; i++) printf("%d ",arr[i]); }
int main() { u_i len = 12; int arr[12] = {2, 13, 12, 16, 15, 4, 17, 8, 1, 18, 14, 9}; int *temp1 = copy(arr, len); insertion_sort(temp1, len); int *temp2 = copy(arr, len); quick_sort(temp2, len); int *temp3 = copy(arr, len); merge_sort(temp3, len); int *temp4 = copy(arr, len); selection_sort(temp4, len); int *temp5 = copy(arr, len); counting_sort(temp5, len); int *temp6 = copy(arr, len); heap_sort(temp6, len); int *temp7 = copy(arr, len); radix_sort(temp7, len); bool eq = equal(temp1, temp2, len) & equal(temp2, temp3, len) & equal(temp3, temp4, len) & equal(temp4, temp5, len) & equal(temp5, temp6, len) & equal(temp6, temp7, len); printf("\x1B[32m""Equal arrays :%s\n""\033[0m", eq ? "true" : "false"); // Free all arrays allocated. }
int main() { const int LEN = 10000001; int i; int* arr = (int*)malloc(sizeof(int) * LEN); int input_range_start = 0; int input_range_end = 1333; srand(time(NULL)); for (i = 0; i < LEN; i++) { arr[i] = input_range_start + rand() % (input_range_end - input_range_start); } //printf("is sorted? %d\n", is_sorted(arr, LEN)); //print_arr(arr, LEN); time_t start, stop; start = clock(); heap_sort(arr, LEN); stop = clock(); //print_arr(arr, LEN); printf("time: %fs\n", (double)(stop - start) / CLOCKS_PER_SEC); printf("is sorted? %d\n", is_sorted(arr, LEN)); return 0; }
int main() { int *arr = malloc(sizeof(int) * MAX); int i; for (i = 0; i < MAX; i++) { arr[i] = rand(); } // {3,5,2,1,6,8,3,5,42,234,23,22,15,7,9,0}; i = 0; printf("BEFORE:\n"); for (i = 0; i < MAX; i++) { printf("%d\n", arr[i]); } heap_sort(&arr, MAX); // i = 0; // printf("\nAFTER:\n"); // for (i = 0; i < MAX; i++) { // printf("%d\n", arr[i]); // } free(arr); return 0; }
int main(int argc, char* argv[]) { int arr[15] ; arr_init(arr, 15); arr_show("before", arr, 15); buble_sort(arr, 15); arr_show("buble", arr, 15); arr_init(arr, 15); arr_show("before", arr, 15); select_sort(arr, 15); arr_show("select", arr, 15); arr_init(arr, 15); arr_show("before", arr, 15); insert_sort(arr, 15); arr_show("insert", arr, 15); arr_init(arr, 15); arr_show("before", arr, 15); quick_sort(arr, 15); arr_show("quick", arr, 15); arr_init(arr, 15); arr_show("before", arr, 15); heap_sort(arr, 15); arr_show("heap", arr, 15); return 0 ; }
int main() { int testcase[] = { 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; int i; for(i = 0; i< sizeof(testcase)/sizeof(testcase[0]); i++) { printf("%d ", testcase[i]); } printf("\n"); heap_sort(testcase, sizeof(testcase)/sizeof(testcase[0])); for(i = 0; i< sizeof(testcase)/sizeof(testcase[0]); i++) { printf("%d ", testcase[i]); } printf("\n"); return 0; }
result_t sort(list_t *list) { data_t *list_data; len_t size; size = length(list); if(size == 0) { return (ERROR); } else if(size == 1) { return (SUCCESS); } list_data = list_to_array(list, size); if(size < INSERTION_SHORT_SIZE) { insertion_sort(list_data, size); } else { heap_sort(list_data, size); } list_t *temp = array_to_list(list_data, size); list -> next = temp; free(list_data); //TO-DO : delete list return (SUCCESS); }
/****************************************************************************** * MAIN ******************************************************************************/ int main(int argc, char * argv[]) { int A[MAX_NUM_NR]; char * filename_unsorted = get_filename(argc, argv); char * filename_sorted = get_sorted_filename(filename_unsorted); int n = get_A(A, MAX_NUM_NR, filename_unsorted); print_A(A, n, "original:"); heap_sort(A, n); print_A(A, n, "sorted:"); assert(verify_A(A, n, filename_sorted)); /* priority queue */ int heapsize = n; build_max_heap(A, heapsize); ______________________________(A, n, heapsize, "===============\\nafter build_max_heap\\n==============="); printf("heap_maximum: %d\n", heap_maximum(A, heapsize)); printf("heap_extract_max: %d\n", heap_extract_max(A, &heapsize)); print_A(A, heapsize, "after heap_extract_max"); ______________________________(A, n, heapsize, "===============\\nafter heap_extract_max\\n==============="); heap_increase_key(A, heapsize, 3, 88); print_A(A, heapsize, "after heap_increase_key"); ______________________________(A, n, heapsize, "===============\\nafter heap_increase_key\\n==============="); max_heap_insert(A, n, &heapsize, 97); print_A(A, heapsize, "after max_heap_insert"); ______________________________(A, n, heapsize, "===============\\nafter max_heap_insert\\n==============="); return 0; }
int main(int argc, char **argv) { vector *c = NULL; int v1 = 4; int v2 = 1; int v3 = 3; int v4 = 2; int v5 = 16; int v6 = 9; int v7 = 10; int v8 = 14; int v9 = 8; int v10 = 7; c = vector_create(); c->push(c, (void *)(&v1 )); c->push(c, (void *)(&v2 )); c->push(c, (void *)(&v3 )); c->push(c, (void *)(&v4 )); c->push(c, (void *)(&v5 )); c->push(c, (void *)(&v6 )); c->push(c, (void *)(&v7 )); c->push(c, (void *)(&v8 )); c->push(c, (void *)(&v9 )); c->push(c, (void *)(&v10)); Print(c, visit); make_heap(c, compare); Print(c, visit); printf("\n===============================\n"); heap_sort(c, compare); Print(c, visit); vector_destroy(&c, NULL); return 0; }
// program start int main(int argc, char const *argv[]) { int arr[MAX_SIZE] = {0, 2, 5, 1, 6, 4, 3, 9, 7, 8}; // first element is unused int size = 9; heap_sort(arr, size); // call to heap sort print_array(arr, 1, 9); return 0; }
void heap_sort_generic(void **input, size_t len, cmp_fun cmp) { binary_heap *heap = alloca(sizeof(binary_heap)); heap->heap = input; heap->cmp = cmp; heap->len = len; heap->mem = len; heap_sort(heap); return; }
int main() { Sqlist *L=(Sqlist *)malloc(sizeof(Sqlist)); initial(L); print(L); heap_sort(L); print(L); return 0; }
int main() { int arr[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7}; int arr2[] = {5, 13, 2, 25, 7, 17, 20, 8, 4}; int len = arr_len(arr); int len2 = arr_len(arr2); print_arr(arr, len); heap_sort(arr, len); print_arr(arr, len); print_arr(arr2, len2); heap_sort(arr2, len2); print_arr(arr2, len2); return 0; }
//本例程中,从数组的第二个数a[1]开始进行排序 int main() { int a[] = {5, 7, 2, 3, 8, 5,6, 9}; heap_sort(a,LEN-1); int k; for(k = 1; k < LEN; k++) printf("%d ", a[k]); return 0; }
int main() { size_t number_of_players = 0; scanf("%"SCNi32, &number_of_players); Player *players = (Player*)calloc(number_of_players, sizeof(Player));//удалить (int*) if (!players) return 1; for (size_t i = 0; i < number_of_players; ++i)// использовать size_t для индексации { players[i].number = i + 1; scanf("%"SCNi32, &players[i].data); } heap_sort(players, number_of_players, ASCENDING); int i = 0, j = 1; int boss_player = i; int less_player = i; int64_t cur_command = players[0].data; int64_t max_command = cur_command; while (j < number_of_players){ if (players[j].data <= (players[i].data + players[i + 1].data)){ cur_command += players[j].data; if (cur_command > max_command){ less_player = i; boss_player = j; max_command = cur_command; } j++; } else{ cur_command -= players[i].data; i++; } } printf("%"PRIi64"\n", max_command); Player *sort_player_command = (Player*)calloc(boss_player - less_player + 1, sizeof(Player)); for (size_t i = less_player, count = 0; i <= boss_player; i++, count++) sort_player_command[count].data = players[i].number; heap_sort(sort_player_command, boss_player - less_player + 1, ASCENDING); for (size_t i = 0; i < boss_player - less_player + 1; i++) printf("%"PRIi32"%c", sort_player_command[i].data, (i != boss_player - less_player) ? ' ' : '\n'); system("pause"); return 0; }
int main(int argc, char **argv) { // list *list = list_create(); // int a = 5; // int b = 4; // int c = 3; // int d = 2; // int e = 1; // int f = 0; // int j = 10; // list_add_node_tail(list, &a); // list_add_node_tail(list, &b); // list_add_node_tail(list, &c); // list_add_node_tail(list, &d); // list_add_node_tail(list, &e); // list_add_node_tail(list, &f); // list_set_match_method(list, match); // list_node *node = list_search_key(list, &f); // list_insert_node(list, node, &j, 1); // list_del_node(list, node); // printf("%ld\n", list_length(list)); // list_iterator *iter = list_get_iterator(list, AL_START_HEAD); // list_node *current; // while ((current = list_next(iter))) // printf("%d ", *((int *)current->data)); // printf("\n"); // int RANGE_MAX = 100; // // int len = 21; // int a[len]; // get_random_int_array(a, len, RANGE_MAX); // printf("Before sort: "); // print_array(a, len); // merge_sort(a,0,len-1); // printf("After sort: "); // print_array(a, len); // int a[] = {16, 4, 10, 14, 7, 9, 3, 2, 8, 1}; int a[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7}; int len = 10; printf("Before sort: "); print_array(a, len); heap_sort(a, len); printf("After sort: "); print_array(a, len); return EXIT_SUCCESS; }
int main(void) { int arr[10] = {2,4,3,1,8,6,9,5,7,10}; //int arr[2] = {4,2}; print_array(arr,(sizeof(arr)/sizeof(int))); heap_sort(arr,sizeof(arr)/sizeof(int)); printf("\n"); print_array(arr,(sizeof(arr)/sizeof(int))); return 0; }
int main() { int h[]={21,12,33,34,54,63,77}; heap_sort(h,sizeof(h)/sizeof(int)); print_int_array(h,0,sizeof(h)/sizeof(int)-1); return 0; }
int main() { int a[13]; int n = sizeof(a)/sizeof(a[0]); random_array(a, n); print_array(a, n); heap_sort(a, n); print_array(a, n); return 0; }
int main() { int input[] = {3, 5, 6, 1, 2, 6, 9, 4, 8}; int len = sizeof(input) / sizeof(int); heap_sort(input, len); return 0; }
int main(int argc, char argv[]) { int arr[] = {3, 5, 2, 7, 0}; int len = sizeof(arr)/sizeof(int); heap_sort(arr, len, 5); print_res(arr, len); return 0; }
int main(void){ init_heap_and_dp(); #ifdef DEBUG_MODE print_heap(HEAP_MAX_LENGTH); #endif /* Heap sort */ mark_start(); heap_sort(HEAP_MAX_LENGTH); mark_stop(); get_time_difference(&time_count); printf("Time taken by heap sort: %d\n",(int)time_count.tv_usec); #ifdef DEBUG_MODE print_heap(HEAP_MAX_LENGTH); #endif #ifdef DEBUG_MODE printf("Sort with bitmap.\n"); #endif /* Bitmap sort algorithm */ mark_start(); for (i = 1; i < HEAP_MAX_LENGTH; i++){ set_bit_in_map((unsigned)array[i]); } mark_stop(); get_time_difference(&time_count); printf("Time taken by bitmap sort: %d\n",(int)time_count.tv_usec); #ifdef DEBUG_MODE show_bitmap(); for (i = 1; i < MAP_MAX_LENGTH; i++){ if (check_bit_in_map(i)){ printf("%u\t", i); } } printf("\n"); #endif /* Algorithm: dp */ printf("This is dp result: %d\n", dp_demo(5)); /* Algorithm: BP(artificial network) */ test_aritificial_neural_network(); /* Algorithm: manacher */ test_manacher(); return 0; }
int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "usage: %s <sort algorithm> <array size>\n", argv[0]); exit(-1); } int count = atoi(argv[2]); int *array = (int*)malloc(sizeof(int) * count); if (array == NULL) { perror("malloc error"); exit(-1); } int i; srand(time(NULL)); printf("sorting"); for (i = 0; i < count; ++i) { array[i] = rand() & 0xff; printf(" %d", array[i]); } printf("\n"); if (strcmp(argv[1], "bubble") == 0) { bubble_sort(array, count); } else if (strcmp(argv[1], "merge") == 0) { merge_sort(array, 0, count, NULL); } else if (strcmp(argv[1], "insert") == 0) { insert_sort(array, count); } else if (strcmp(argv[1], "heap") == 0) { heap_sort(array, count); } else if (strcmp(argv[1], "quick") == 0) { quick_sort(array, 0, count); } else { fprintf(stderr, "unknown sort algorithm\n"); exit(-1); } printf("result is"); for (i = 0; i < count - 1; ++i) { printf(" %d", array[i]); if (array[i] > array[i+1]) { printf("sort error\n"); for (i = 0; i < count; ++i) { printf("%d ", array[i]); } exit(-1); } } if (count > 0) { printf(" %d", array[i]); } printf("\nsort OK\n"); return 0; }
int main() { int a[100],n,i; printf("enter size of the array :"); scanf("%d",&n); for(i=0;i<n;i++)scanf("%d",&a[i]); heap_sort(a,n); printf("Sorted array : "); for(i=0;i<n;i++) printf("%d ",a[i]); }