I have the following code for Quick Sort procedure:

``````#include<stdio.h>

void Quick_sort(int *arr,int left,int right);
int Partition(int *arr,int left,int right,int pivotindex);

int main()
{
int a[20],n,ctr=0,j=0;
printf("\nEnter number of elements:");
scanf("%d",&n);
printf("Enter array elements:");
while(ctr<n)
{
scanf("%d ",&a[ctr]);
ctr++;
}
Quick_sort(a,0,n-1);
printf("\nSorted Array:");
while(j<n)
{
printf("%d ",a[j]);
j++;
}
return 0;
}

int Partition(int *arr,int left,int right,int pivotindex)
{
int i,temp1,temp2,temp3,storeindex;
storeindex=left;
temp1=arr[pivotindex];        //value at pivot index

//swap pivot with last element//
arr[pivotindex]=arr[right];
arr[right]=temp1;

for(i=left;i<=right-1;i++)
{
if(arr[i]<=temp1)
{
temp2=arr[i];
arr[i]=arr[storeindex];
arr[storeindex]=temp2;
storeindex++;
}
}

//swap pivot with arr[storeindex] for its final place//
temp3=arr[storeindex];
arr[storeindex]=arr[right];
arr[right]=temp3;

return storeindex;
}

void Quick_sort(int *arr,int left,int right)
{
int pivot,newpivot;
if(right>left)
{
pivot=left+(right-left)/2;
newpivot=Partition(arr,left,right,pivot);

Quick_sort(arr,left,newpivot-1);
Quick_sort(arr,newpivot+1,right);
}

}``````

I am not getting any output on running the above code.. Though it compiles fine.. Any suggestion would be a great help! I am using codeblocks as the compiler.

3
Contributors
9
Replies
11
Views
6 Years
Discussion Span
Last Post by vinitmittal2008

I'm getting correct output for simple test cases. Perhaps you mean the output window is closing before you can see the results? If that's so, add a call to getchar just before returning from main:

``````int main()
{
int a[20],n,ctr=0,j=0;
printf("\nEnter number of elements:");
scanf("%d",&n);
printf("Enter array elements:");
while(ctr<n)
{
scanf("%d ",&a[ctr]);
ctr++;
}
Quick_sort(a,0,n-1);
printf("\nSorted Array:");
while(j<n)
{
printf("%d ",a[j]);
j++;
}
getchar();
return 0;
}``````

No that is not the problem.. The output window stays even after entering the input but there's no output.. Maybe there's some other error.. Which compiler did you use?

@ gaurav
I am also using CODEBLOCKS and i compiled your program and its working fine. its showing sorted output after inputting desired values..

Alright then, since this is clearly reliant on input, please post some sample inputs that fail to produce output. This is a stupid question, but you are entering input such that the first loop terminates, right?

>Which compiler did you use?
Visual Studio 2010 and Pelles C.

It means there's some problem with my codeblocks.. thanks for all replies and it would help me if anyone can provide link on some tutorial to use codeblocks..

>It means there's some problem with my codeblocks
Why do people always assume it's the compiler? 99.9% of the time the problem is with your code, or your assumptions.

``scanf("%d ",&a[ctr]);``

Remove space after %d its causing problem

``scanf("%d",&a[ctr]);``

Well thanks all.. Yes, I realize it was me making a minor mistake and finding faults in codeblocks. But that happened because I spent a lot of time on this program and still was not able to get correct output.

>>>if anyone can provide link on some tutorial to use codeblocks..