[ create a new paste ] login | about

Link: http://codepad.org/yVIia9yb    [ raw code | output | fork ]

pi8027 - C, pasted on Jul 17:
#include <stdio.h>

#define ARRAY_SIZE 32

#define SWAP(a,b) ((void)(temp = (a),(a) = (b),(b) = temp))

void quicksort(int array[],size_t begin,size_t end)
{
    if(begin+1 < end){
        int temp;
        size_t pivot = (begin+end)>>1,first2last = begin,last2first = end;
        while(first2last < last2first){
            while(array[first2last] < array[pivot]){
                first2last++;
            }
            while(array[pivot] <= array[last2first]){
                last2first--;
            }
            if(first2last < last2first){
                if(pivot == first2last){
                    pivot = last2first;
                }
                SWAP(array[first2last],array[last2first]);
            }
        }
        SWAP(array[pivot],array[first2last]);
        quicksort(array,begin,last2first);
        quicksort(array,first2last+1,end);
    }
}

#undef SWAP

int main(void)
{
    int array[ARRAY_SIZE];
    unsigned long random_seed = time(NULL);
    size_t counter = 0;
    while(counter != ARRAY_SIZE){
        array[counter] = rand()%64;
        printf("%02d ",array[counter]);
        counter++;
    }
    printf("\n");
    quicksort(array,0,ARRAY_SIZE-1);
    counter = 0;
    while(counter != ARRAY_SIZE){
        printf("%02d ",array[counter]);
        counter++;
    }
    printf("\n");
    return 0;
}


Output:
1
2
39 06 41 51 17 63 10 44 41 13 58 43 50 59 35 06 60 02 20 56 27 40 39 13 54 26 46 35 51 31 09 26 
02 06 06 09 10 13 17 13 20 26 26 27 31 35 35 39 39 40 41 41 43 44 46 50 51 51 54 58 56 59 60 63 


Create a new paste based on this one


Comments: