Student Of Fortune

Quick Sort In C language

Share on :
One of the sorting algorithm that is not less important to learn the Quick Sort algorithm. This algorithm is popularly used in the programming world. Therefore, I will explain a little about the Quick Sort algorithm in C + + programming language.

Quick Sort algorithm discovered by C.A.R Hoare. Quick sort, as the name suggests, is claimed as a sorting algorithm that is faster than other sorting algorithms.

However, this algorithm, according to my own too, is considered quite difficult to be understood than others, due to master these algorithms, necessary knowledge about algorithms and patterns of recursive divide-and-concuer. What the hell is that? Below I ulaskan little (very little:)) on both schemes.
Divide
Sorting data group into two sub-groups of data.
Conquer
Sort the elements in the sub-series recursively.
Recursive
The method in which the contents of a function contains function calls itself.
For more details can be seen in the syntax below:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define MAX 30
#define INPUT 'i'
#define OUTPUT 'o'
#define TRUE 1
#define FALSE 0
#define _MY_DEBUG
#if defined(_MY_DEBUG)
    #define TRACE_LINE printf("\n\n- Program Statistics :\n1. File : %s\n2. Date : %s\n3. Time : %s\n",__FILE__,__DATE__,__TIME__);
#else
    #define TRACE_LINE
#endif

int ChoosePivot(int, int);
    void InputOutput(int*, const int, const char),
        QuickSort(int*, int, int),
    Swap(int*, int*),
FreeBuffer(int*);

int main(int argc, char *argv[]) {
    system("COLOR 5");
    int *buffer = NULL, max;
    printf("Implementasi Quick Sort [Ascending]\nJumlah data [MAX:30] : ");
    scanf("%d",&max);
    fflush(stdin);
    if((max > FALSE) && (max <= MAX)) {
        buffer = (int*)calloc(max,sizeof(int));
        InputOutput(buffer,max,INPUT);
        printf("\n1. Data yang anda masukkan : ");
        InputOutput(buffer,max,OUTPUT);
        QuickSort(buffer,FALSE,(max-TRUE));
        printf("\n2. Data setelah disorting : ");
        InputOutput(buffer,max,OUTPUT);
        FreeBuffer(buffer);
    }
    TRACE_LINE;
    getch();
    fflush(stdin);
    return(EXIT_SUCCESS);
}

void InputOutput(int* buffer, int max, const char STAT) {
    int i;
    if(INPUT == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d. Data ke-%d : ",(i+TRUE),(i+TRUE));
            scanf("%d",&buffer[i]);
            fflush(stdin);
        }
    } else if(OUTPUT == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d ",buffer[i]);
        }
    }
}

int ChoosePivot(int top, int bottom) {
    return((top+bottom)/2);
}

void QuickSort(int* buffer, int bottom, int top) {
    int i, j, k, pivot; // m = bottom, n = top;
    if(bottom < top) {
        pivot = ChoosePivot(bottom,top);
        Swap(&buffer[bottom],&buffer[pivot]);
        i = bottom+TRUE; j = top; k = buffer[bottom];
        while(i <= j) {
            while((i <= top) && (buffer[i] <= k)) {
                ++i;
            }
            while((j >= bottom) && (buffer[j] > k)) {
                --j;
            }
            if(i < j) {
                Swap(&buffer[i],&buffer[j]);
            }
        }
        Swap(&buffer[bottom],&buffer[j]);
        QuickSort(buffer,bottom,(j-TRUE));
        QuickSort(buffer,(j+TRUE),top);
    }
}

void Swap(int* buffer1, int* buffer2) {
    int tmp = *buffer1;
    *buffer1 = *buffer2;
    *buffer2 = tmp;
}

void FreeBuffer(int* buffer) {
    free(buffer);
    buffer = NULL;
}
See The Out Put :

2 comments on Quick Sort In C language :

caribbean said... August 5, 2011 at 12:57 PM

I'd like a simple example of sorting 3 integer arrays at once.

I have to sort one of 3 arrays by size so that the largest values are at the front of the array and the smaller values are at the back.
I have two other other arrays as well.
The values in the other arrays are related to the values in the first array.
So I need to sort the first array by size, and keep each element of the other two arrays in the same relative position as the elements in the array being sorted.

for example
The arrays before being sorted
a[1,2,3,4];
b[7,4,6,5];
c[9,8,11,10];

The arrays after a[] is sorted by decreasing size

a[4,3,2,1];
b[5,6,4,7];
c[10,11,8,9];

Can you show me how to use quick sort for this?
my email address is dragonbalzy@gmail.com

Unknown said... August 5, 2011 at 2:39 PM

Sorting is a process of arranging objects in a certain order and / or in a different set, and therefore he has two different common meanings:
1. sorting: assemble similar objects, class, etc., in regular order,
2. categorizing: grouping and labeling the objects with similar properties.
we can sort the first values ​​input into the final output

probably with little to edit source on the input values ​​into an array and value for(i = 0; i < max; ++i) the change in inverted initially 1 to max we coud change the max to 1 i --i Tq

Post a Comment and Don't Spam!

Dont Spam please

 
Recommended Post Slide Out For Blogger

Recent Comments

My Rank