Thursday, August 18, 2011

Quick sort in C

#define maxsize 6


int A[maxsize];



void quicksort(int a, int b)

{

int rtidx=0,ltidx=0,k=a,l=0,pivot;

int leftarr[maxsize],rtarr[maxsize];

pivot=A[a];

if(a==b)return;

while(k

{

++k;

if(A[k]

{

leftarr[ltidx]=A[k];

ltidx++;

}

else

{

rtarr[rtidx]=A[k];

rtidx++;

}

}



k=a;

for(l=0;l

A[k++]=pivot;

for(l=0;l

if(ltidx>0)quicksort(a,a+ltidx-1);

if(rtidx>0)quicksort(b-rtidx+1,b);

}



void printarr(int a)

{

int i;

for(i=0;i

{

printf("%d",A[i]);

printf("\n");

}

}



main()

{

int i,s;

printf("enter the number of numbers to be entered \n");

scanf("%d",&s);

for(i=0;i

{

printf("enter the number \n" );

scanf("%d",&A[i]);

}

printf("array before sorting ");

printarr(s);

quicksort(0,s-1);

printf("array after sorting");

printarr(s);

}
 
OUTPUT : -
enter the number of numbers to be entered
5
enter the number
8
enter the number
0
enter the number
-5
enter the number
-1
enter the number
7
array before sorting 8
0
-5
-1
7
array after sorting -5
-1
0
7
8
 
 
 
r u unable to understand this program??????????TRY THESE










1. Email us at chelperr@gmail.com



2. Post your question in the comment box.



3. You can give us your email ID also.

4. If you want to ask a program/topic or wanted introduce any discussion topic then just email at chelperr@gmail.com or drop a comment in the comment box.

1 comment:

  1. *corrected form

    #define maxsize 6
    int A[maxsize];

    void quicksort(int a, int b)
    {
    int rtidx=0,ltidx=0,k=a,l=0,pivot;
    int leftarr[maxsize],rtarr[maxsize];
    pivot=A[a];
    if(a==b)return;
    while(k < b)
    {
    ++k;
    if(A[k]0)quicksort(a,a+ltidx-1);
    if(rtidx>0)quicksort(b-rtidx+1,b);
    }

    void printarr(int a)
    {
    int i;
    for(i=0;i<a;i++)
    {
    printf("%d",A[i]);
    printf("\n");
    }
    }

    main()
    {
    int i,s;
    printf("enter the number of numbers to be entered \n");
    scanf("%d",&s);
    for(i=0;i<s;i++)
    {
    printf("enter the number \n" );
    scanf("%d",&A[i]);
    }
    printf("array before sorting ");
    printarr(s);
    quicksort(0,s-1);
    printf("array after sorting");
    printarr(s);
    }

    ReplyDelete