/***********************SELECTIONSORT************************sortMode=0 void selectionSort(compCount) ************************SELECTIONSORT************************/ void selectionSort(int compCount) { copyArr(); countComp=0; int i,j,min; int forchange; for(i=0; i<numArr; i++) { min=i; for(j=i+1; j<numArr; j++) { countComp++; if(A[j]<A[min]) min=j; } if(min!=i) { forchang=A[min]; A[min]=A[i]; A[i]=forchange; } } compPerSort[0][compCount]=countComp; }
/***********************QUICKSORT************************sortMode=4 ************************QUICKSORT************************/ void quickSort(int compCount) { copyArr(); countComp=0; compPerSort[4][compCount]=countComp; }
/***********************MERGESORT************************sortMode=3 void mergeSort(int compCount) void mergesortrec(low,high) ************************MERGESORT************************/ void mergeSort(int compCount) { copyArr(); countComp=0; mergesortrec(1,n); compPerSort[3][compCount]=countComp; }
/** * Constructor that creates the structure of the 1x1 block and sets its position. */ Block::Block(int xC, int yC){ int structure[4][4] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; copyArr(structure, structBlock); x=xC; y=yC; colour = RED; }
int *algorithm_1(int *arr, int *outputArr, int size, int value) { int i, k, *tempArr1, *tempArr2, *tempArr3; k = value; if (k == 1) { outputArr[0]++; return outputArr; } else if ((i = isCoin(arr, size, k))) { outputArr[i]++; return outputArr; } else { //loop through every integer from 1 to "value" for (i = 1; i < k; i++) { //create and initilize three temprorary arrays of size "size" to all 0's tempArr1 = createArr(size); tempArr2 = createArr(size); tempArr3 = createArr(size); initArr(tempArr1, size); initArr(tempArr2, size); initArr(tempArr3, size); //recursively call algorithm_1 on array from 1 -> i and i + 1 -> "value" tempArr1 = algorithm_1(arr, tempArr1, size, i); tempArr2 = algorithm_1(arr, tempArr2, size, k - i); //add the sum of all elements at each index of tempArr1 and temArr2 together, assign to tempArr3 addArr(tempArr1, tempArr2, tempArr3, size); //if this new sum of elements array (tempArr3) is less than the running min (outputArr) or the current output array is not a solution, then replace outputArr with tempArr3 if ((sumArray(tempArr3, size) < sumArray(outputArr, size)) || !coinCheck(outputArr, arr, size, k)) { copyArr(tempArr3, outputArr, size); } free(tempArr1); free(tempArr2); free(tempArr3); } } return outputArr; }
/***********************INSERTIONSORT************************sortMode=1 void insertionSort(int compCount) ************************INSERTIONSORT************************/ void insertionSort(int compCount) { copyArr(); countComp=0; int i,j,x; for(i=1; i<numArr; i++) { x=A[i]; j=i-1; while(j>=0&&A[j]>x) { countComp++; A[j+1]=A[j]; j--; } countComp++; A[j+1]=x; } compPerSort[1][compCount]=countComp; }
/***********************BOTTOMUPSORT************************sortMode=2 void bottomupSort(int compCount) void merge(int p,int q,int r) ************************BOTTOMUPSORT************************/ void bottomupSort(int compCount) { copyArr(); countComp=0; int t,s,i; t=1; while(t<n) { s=t; t=2s; i=0; while(i+t<=n) { merge(i+1-1,i+s-1,i+t-1); //减一是为了与C语言的数组下标习惯统一 i=i+t; } if(i+s<n) merge(i+1-1,i+s-1,n-1); } compPerSort[2][compCount]=countComp; }
Hospital:: Hospital(const Doctor*const* arr, int size) { this->size = size; this->capacity = size; copyArr(arr); }
void Hospital:: copyHospital(const Hospital& hospital) { this->capacity = hospital.capacity; this->size = hospital.size; copyArr(hospital.arr); }
void Polinom:: setArr(const int* arr) { this-> arr = new int[n]; copyArr(arr); }
/** * Constructor that creates the structure of the block and set the colour based on the blockID. */ Block::Block(int blockID){ switch(blockID){ case 0: // I block { int structure[4][4] = { 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 }; colour = CYAN; copyArr(structure, structBlock); } break; case 1: // L block { int structure[4][4] = { 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 }; colour = ORANGE; copyArr(structure, structBlock); } break; case 2: // T block { int structure[4][4] = { 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; colour = MAGENTA; copyArr(structure, structBlock); } break; case 3: // Z block { int structure[4][4] = { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; colour = RED; copyArr(structure, structBlock); } break; case 4: // O bock { int structure[4][4] = { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; colour = YELLOW; copyArr(structure, structBlock); } break; case 5: // L reverse block { int structure[4][4] = { 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }; colour = BLUE; copyArr(structure, structBlock); } break; case 6: // O bock { int structure[4][4] = { 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; colour = GREEN; copyArr(structure, structBlock); } break; default: // blockID not matching, no block can be created break; } x = GRIDX/2-1; y = 0; }