All pastes #985229 Raw Edit

Unnamed

public c v1 · immutable
·published 2008-04-14 21:22 UTC
rendered paste body
/****************************************//*                                      *//* Praktikum 2 - Sortier Algorithmen    *//* Tristan Leo - MatrNr. 724529         *//* 14.04.2008                           *//*                                      *//****************************************/#include <stdio.h>#include <stdlib.h>#include <math.h>#include <time.h>#define CLOCKS_PER_MS 1000void mergesort(int *values, int length);void bubblesort(int *values, int length);int isSorted(int *values, int length);int main(void) {    unsigned int i, j, count;    clock_t start, end;    int *testArray;    for (i = 14; i < 23; i++) {        printf("Sortiere 2^%u Felder\n", i);        count = pow(2,i);        testArray = malloc(sizeof(int)*count);        if(testArray) {            for (j = 0; j<count; j++) {                testArray[j] = rand();            }            start = clock();            bubblesort(testArray, count);            end = clock();            if (isSorted(testArray, count) == 1) printf("bubble array nicht sortiert\n");            printf("bubblesort: %li ms\n", (end-start)/CLOCKS_PER_MS);            for (j = 0; j<count; j++) {                testArray[j] = rand();            }            start = clock();            mergesort(testArray, count);            end = clock();            if (isSorted(testArray, count) == 1) printf("merge array nicht sortiert\n");            printf("mergesort: %li ms\n\n", (end-start)/CLOCKS_PER_MS);            free(testArray);        }        else {            printf("malloc fuer testArray lieferte NULL!\n");        }    }    return 0;}void mergesort(int *values, int length) {    int left = length/2;    int right = length-left;    unsigned int i, j;    int * arrLeft = malloc(sizeof(int)*left);    int * arrRight = malloc(sizeof(int)*right);    if(arrLeft && arrRight) {        if (length <= 1)            return;        for (i = 0; i < left; i++) {            arrLeft[i] = values[i];        }        for (i = 0, j = right-1; i < right; i++, j++) {            arrRight[i] = values[j];        }        mergesort(arrLeft, left);        mergesort(arrRight, right);        for (i = 0; i < length; i++) {            if (arrLeft[0] <= arrRight[0]) {                values[i] = left;            } else {                values[i] = right;            }        }        free(arrLeft);        free(arrRight);    }    else {        if(!arrLeft)            printf("malloc fuer arrLeft lieferte NULL!\n");        if(!arrRight)            printf("malloc fuer arrRight lieferte NULL!\n");    }}void bubblesort(int *values, int length) {    unsigned int i, temp;    int test = 1;    while (test) {        test=0;        for (i = 0; i < length-1; i++) {            if (values[i] > values[i+1]) {                temp = values[i];                values[i] = values[i+1];                values[i+1] = temp;                test=1;            }        }        if (!test) break;    }}int isSorted(int *values, int length) {    int i;    for (i = 0; i < length-1; i++) {        if (values[i] > values[i+1])            return 1;    }    return 0;}