c++ - Should checking loop conditions be counted towards total number of comparisons? -
i have implemented 3 different sorting algorithms , want confirm approach of counting total number of comparisons correct. in mind, number of comparisons shouldn't tied conditional branches because if condition isn't met, comparison still made compare values. use same thought process add 1 comparison count exit loop conditions.
the implementations algorithms below. setting counter correctly in each scenario?
insertion sort
int insertionsort(double* a, int arraylength) { int count = 1; // initialized 1 count exit conditions of loop (int j = 1; j < arraylength; j++) { count++; double key = a[j]; int = j - 1; while (i > -1 && a[i] > key) { count++; a[i + 1] = a[i]; = - 1; } count+=2 // plus 2 while loop exit condition a[i + 1] = key; } return count; }
heap sort
void heapsort(double* a, int arraylength, int* c) { int heapsize = 0; buildmaxheap(a, arraylength, &heapsize, c); (int = arraylength - 1; > 0; i--) { swap(a[0], a[i]); heapsize = heapsize - 1; maxheapify(a, 0, &heapsize, c); } } void buildmaxheap(double* a, int arraylength, int* heapsize, int* c) { *heapsize = arraylength - 1; *c = *c + 1; // counts comparison of loop exit condition (int = floor((arraylength)/ 2); > -1; i--) { *c = *c + 1; maxheapify(a, i, heapsize, c); } } void maxheapify(double* a, int i, int* heapsize, int* c) { int l = (2 * i) + 1; int r = (2 * i) + 2; int largest; if (l <= *heapsize && a[l] > a[i]) largest = l; else largest = i; if (r <= *heapsize && a[r] > a[largest]) largest = r; if (largest != i) { swap(a[i], a[largest]); maxheapify(a, largest, heapsize, c); } *c = *c + 5; }
quick sort
void quicksort(double* a, int p, int r, int* c) { if (p < r) { int q = partition(a, p, r, c); quicksort(a, p, q - 1, c); quicksort(a, q + 1, r, c); } *c = *c + 1; } int partition(double* a, int p, int r, int* c) { double x = a[r]; int = p - 1; (int j = p; j < r; j++) { if (a[j] <= x) { = + 1; swap(a[i], a[j]); } *c = *c + 2; } *c = *c + 1 // adding 1 for loop exit condition swap(a[i + 1], a[r]); return + 1; }
if @ inserstion sort
as put count =1 because exits on exit condition of loop.
for same reason make sense when while loop cancels count++ inside not executed there comparison made. count+=2. why 2? makes sense because added 2 2 comparisons in while loop
(i > -1 && a[i] > key)
- i>-1
- a[i] > key
but need increase counter 2 inside while loop everytime while correct 2 comparisons made.
int insertionsort(double* a, int arraylength) { int count = 1; // initialized 1 count exit conditions of loop (int j = 1; j < arraylength; j++) { count++; double key = a[j]; int = j - 1; while (i > -1 && a[i] > key) { count++; a[i + 1] = a[i]; = - 1; } count+2 // plus 2 while loop exit condition a[i + 1] = key; } return count; }
same way can check other algorithms also.
still better approach suggest read starting chapter in book analysis of algorithm. explains how estimate running time of algorithm understand how analyse better.
Comments
Post a Comment