int count_inversions(int arr[], int low, int high){ int inversions = 0; if(low < high){ int mid = low + (high-low)/2; inversions += count_inversions(arr, low , mid); inversions += count_inversions(arr, mid+1, high); inversions += merge_inversions(arr, low, mid, high); } return inversions; }
int main(int argc, char **argv) { extern unsigned char transposition[]; char inversions = 0; int i = 0; printf("Session %d\n-->task %d\nversion %d\n", SESSION, TASK, VERSION); printf(DESC1); printf(DESC2); printf("\n\n"); if(read_transposition() == ERROR) { printf("An error occured during entering numbers: possibly you are not entering numbers!\n"); return 0; } printf("\n\n"); // print transposition array onto the screen printf("Transposition: "); for( i = 0; i < MATRIX_LENGTH; i++ ) { printf("%d\t", transposition[i]); } printf("\n\n"); inversions = count_inversions(); // print the number of inversions printf("---------------------\n"); printf("Number of inversions: %d", inversions); printf("\n\n"); return 0; }
int main (int argc, char **argv) { int error = 0; Vector *vector = vector_new (); if (vector_load_from_file (vector, argc, argv)) { Stopwatch *stopwatch = stopwatch_new (); uint64_t inversions = 0; stopwatch_start (stopwatch); inversions = count_inversions (vector); stopwatch_stop (stopwatch); printf ("%llu inversions of %zu numbers in %llu.%03u ms.\n", inversions, vector->length, stopwatch->elapsed, stopwatch->usec); stopwatch_free (stopwatch); } else { error = 1; } vector_free (vector); return error; }
int main(int argc, char *argv[]) { // int_l input_size = readnum(stdin); int_l input_size = strtol(argv[1], NULL, 10); int_l i; int_l *input_array = malloc(input_size * sizeof(int_l)); for (i = 0; i < input_size; i++) { input_array[i] = readnum(stdin); } printf("%u\n", count_inversions(input_size, input_array)); }
void test_case_medium() { unsigned long long inversion_count; int A[15] = {6, 1, 9, 2, 12, 156, 22, 66, 11, 6, 5, 4, 3, 2, 1}; fprintf(stdout, "Running Small Case\n"); inversion_count = count_inversions(A, 15); fprintf(stdout, "Total number of inversions: %llu\n", inversion_count); }
void test_case_small2() { unsigned long long inversion_count; int A[6] = {5, 6, 3, 2, 0, 1}; fprintf(stdout, "Running Small Case2\n"); inversion_count = count_inversions(A, 6); fprintf(stdout, "Total number of inversions: %llu\n", inversion_count); }
void test_case_file() { unsigned long long inversion_count; int* A; float hostTime; double startTime, stopTime; fprintf(stdout, "Running Test Case from File\n"); A = read_array("IntegerArray.txt"); startTime = second(); inversion_count = count_inversions(A, ARRAY_SIZE); stopTime = second(); hostTime = (stopTime - startTime) * 1000; fprintf(stdout, "Computation Time: %lf\n", hostTime); fprintf(stdout, "Total number of inversions: %llu\n", inversion_count); free(A); }
int countInversions(int arr[], int size){ return count_inversions(arr, 0, size-1); }