[ create a new paste ] login | about

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

C, pasted on Aug 10:
#import "stdio.h"
#import "stdlib.h"
#import <math.h>

int main()
{
    int myReturn = 1;
    
    const int SIZE = 10;
    
    int myArray[SIZE];
    int i = 0;
    for(i = 0;i<SIZE;i++)
    {
       myArray[i] = rand();
    }
    
    /* Display unsorted array */
    Display(myArray,SIZE);
    
    QuickSort(myArray, 0 , SIZE - 1);
    
    Display(myArray,SIZE);
    


    return myReturn;
}


void Display(int myParam[], const int SIZE)
{
    printf("\n");
    int x = 0;
    for(x=0;x<SIZE;x++)
    {
        printf("\t%d",myParam[x]);
    }
}

void QuickSort(int myParam[], int start, int end)
{
    int pivot, key, subStart,subEnd = 0;
    
    

    if(start < end)
    {    
    
        pivot = PickPivot(start, end);
        Swap(&myParam[start],    &myParam[pivot]);
        key = myParam[pivot];
        subStart = start + 1;
        subEnd = end;
        while(subStart <= subEnd)
        {
            while((subStart <= end) && (myParam[subStart] <= key))
            {    
                subStart++;
            }

            while((subEnd > subStart) && (myParam[subEnd] > key))
            {    
                subEnd--;
            }
            if(subStart < subEnd)
            {
                Swap(&myParam[subStart], &myParam[subEnd]);
            }
        }
        Swap(&myParam[start],    &myParam[subEnd]);
        QuickSort(myParam,start,subEnd-1);
        QuickSort(myParam,subEnd+1,end);
    }

}

int PickPivot(int start, int end)
{
    return ((start + end) / 2);
}

void Swap(int *var1, int *var2)
{
    int temp = 0;
    temp = *var1;
    *var1 = *var2;
    *var2 = temp;
}


Output:
1
Timeout


Create a new paste based on this one


Comments: