예제 #1
0
파일: 11-1.c 프로젝트: gsrr/Programs
int main()
{
        int* A = createArr(20, INT);
        print_arr(A, 20);
        int* B = createArr(10, INT);
        print_arr(B, 10);
        int i;
        int size = 10;
        for( i = 0 ; i < 10 ; i++)
        {
                insertionSort(A, B[i], i + 10);
        }
        print_arr(A, 20);
        return 0;
}
예제 #2
0
void algorithm(int *arr, int size, int value, int version, FILE *file) {

    clock_t T1, T2;
    double time;
    int *outputArr;
    
    outputArr = createArr(size);
    initArr(outputArr, size);
    
    T1 = clock();//start timer
    
    //Call the appropriate algorithm on the array
    switch (version) {
        case 1 :
            outputArr = algorithm_1(arr, outputArr, size, value);
            break;
        case 2 :
            outputArr = algorithm_2(arr, outputArr, size, value);
            break;
        case 3 :
            outputArr = algorithm_3(arr, outputArr, size, value);
            break;
    }
    
    T2 = clock();//end timer
    time = (double)(T2 - T1) / CLOCKS_PER_SEC;//get total time
    
    value = sumArray(outputArr, size);
    print_results(outputArr, size, value, file);
    
    fprintf(file, "Time to execute: %fs\n", time);
    
    free(outputArr);
}
예제 #3
0
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;
    
}
예제 #4
0
파일: mergeSort.c 프로젝트: gsrr/Programs
int main()
{
        int* arr = createArr(10, INT);
        print_arr(arr, 10);
        mergeSort(arr, 0 , 9);
        print_arr(arr, 10);
        free(arr);
        return 0;
}
예제 #5
0
파일: 4-9.c 프로젝트: gsrr/Programs
int main()
{
        int num = 20;
        int* arr = createArr(num, INT);
        print_arr(arr, num);
        tNode* root = createBSTRoot(arr, num);
        print_tree(root, print);
        printf("\n");
        node* nl = createNode(root);
        findAllPath(root, &nl, 0);        
        free(nl);

        return 0;
}
예제 #6
0
파일: 4-7.c 프로젝트: gsrr/Programs
int main()
{
        srand(time(NULL));
        int num = 20;
        int* arr = createArr(num, INT);
        print_arr(arr, num);
        tNode* root = createBSTRoot(arr, num);
        print_tree(root,print);
        printf("\n");
        int i;
        for(i = 0 ; i < num ; i++)
        {
                int key1 = rand()%20 + 1;
                int key2 = rand()%20 + 1;
                printf("(%d,%d)\n", key1, key2);
                int tmp = findAncestor(root, key1, key2);
        }
        return 0;
}
예제 #7
0
파일: 4-3.c 프로젝트: gsrr/Programs
int main()
{
        int num = 20;
        int* arr = createArr(num);
        int low = 0 ; 
        int high = num - 1;
        int middle = (low + high)/2;
        //tNode* root = createTreeNode(&arr[middle]);
        //createLTreeByArr(&root, arr, low, middle - 1);
        //createRTreeByArr(&root, arr, middle + 1, high);
        //printf("height:%d\n", treeHeight(root));
        //print_tree(root, print);
        printf("\n");
        tNode* binRoot = createTreeNode(&arr[middle]);
        appendBST(&binRoot, arr, low, middle-1);
        appendBST(&binRoot, arr, middle+1, high);
        printf("height:%d\n", treeHeight(binRoot));
        print_tree(binRoot, print);
        //createBSTByOrder(arr, num);

        return 0;
}