void quick_sort (int *a, int n) {
    if (n < 2)
        return;
    int p = a[n / 2];
    int *l = a;
    int *r = a + n - 1;
    while (l <= r) {
        while (*l < p)
            l++;
        while (*r > p)
            r--;
        if (l <= r) {
            int t = *l;
            *l++ = *r;
            *r-- = t;
        }
    }
    quick_sort(a, r - a + 1);
    quick_sort(l, a + n - l);
}

int main () {
    int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
    int n = sizeof a / sizeof a[0];
    quick_sort(a, n);
    return 0;
}

how to print the o/p for this code??

Recommended Answers

All 3 Replies

what is "o/p"? output?

When testing sort routines visually I'll usually use a before and after printout:

void quick_sort (int *a, int n) {
    if (n < 2)
        return;
    int p = a[n / 2];
    int *l = a;
    int *r = a + n - 1;
    while (l <= r) {
        while (*l < p)
            l++;
        while (*r > p)
            r--;
        if (l <= r) {
            int t = *l;
            *l++ = *r;
            *r-- = t;
        }
    }
    quick_sort(a, r - a + 1);
    quick_sort(l, a + n - l);
}

#include <stdio.h>

void display(int* a, int size, int field_width) {
    int i;
    for (i = 0; i < size; ++i) {
        printf("%*d", field_width, a[i]);
    }
    putchar('\n');
}

int main () {
    int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
    int n = sizeof a / sizeof a[0];
    display(a, n, 4);
    quick_sort(a, n);
    display(a, n, 4);
    return 0;
}

Try using this code

void quicksort(int *a,int l,int h)
    {
        int x;
        if(l>=h)
        return;
        x=partition(a,l,h);
        quicksort(a,l,x-1);
        quicksort(a,x+1,h);
    }
    int partition(int *a,int l,int h)
    {
        int i,j,pv;
        pv=a[l];
        i=l,j=h;
        while(i<=j)
        {       
                   while(a[i]<=pv)
                          i++;
                while(a[j]>pv)
                          j--;
                if(i<j)
                          swap(a,i,j);
    }
    swap(a,l,j);
    int k=0;
    printf("\n");
    for(k=0;k<n;k++)
        printf("%4d",a[k]);
    printf("\n");
    return(i-1);
}            
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.