0% found this document useful (0 votes)
10 views

Quick Sort

The document describes a quicksort algorithm implemented in C++. It is not working properly when the user tries to run it. The code takes in a number of items, allocates an array to store them, calls functions to enter the data and perform the quicksort, and then prints out the sorted array. However, the quicksort is not sorting the array correctly. The user is asking for help to determine what is wrong with the implementation.

Uploaded by

idealmanju
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Quick Sort

The document describes a quicksort algorithm implemented in C++. It is not working properly when the user tries to run it. The code takes in a number of items, allocates an array to store them, calls functions to enter the data and perform the quicksort, and then prints out the sorted array. However, the quicksort is not sorting the array correctly. The user is asking for help to determine what is wrong with the implementation.

Uploaded by

idealmanju
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

quicksort

Expand Post
this code was workin when i used srand function to fill th e array with random numbers and the
array was a declared globally.But now the code is not working.I cant find anything wrong in
this .Sombody pls help..
CPP Syntax (Toggle Plain Text)
1.
2.
3.
4.
5.
6.
7.
8.

#include<iostream.h>
int *a,NUM_ITEMS;
int main()
{
void enterdata();
void quicksort(int,int);
int i;
cout<<"\n\nEnter the number of items:";
cin>>NUM_ITEMS;
a=new int [NUM_ITEMS];
if(a==NULL)
cout<<"Memory allocation error";
cout<<"\nEnter the numbers:";
enterdata();
quicksort(0,NUM_ITEMS-1);
cout<<"Done with sort.\n";
for (i = 0; i <NUM_ITEMS; i++)
cout<<"\n"<<a[i];
return 0;

9.

10.
11.
12.
13.
14.
15.
16.
17.

18.

19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.

}
void enterdata()
{
int i;
for(i=0;i<NUM_ITEMS;i++)
cin>>a[i];
}

34. void quicksort(int lower,int upper)


35. {
36.
37.
38.
39.

40.
41.

42.
43.

int split(int,int),pivot;
if(upper>lower)
{
pivot=split(lower, upper);
quicksort(lower,pivot-1);
quicksort(pivot+1,upper);
}

44. }
45. int
46.
{
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68. }

split(int lower,int upper)


int i,p,q,t;
p=lower+1;
q=upper;
i=a[lower];
while(q>=p)
{
while(a[p]<i)
p++;
while(a[q]>i)
q--;
if(q>p)
{
t=a[p];
a[p]=a[q];
a[q]=t;
}
}
t=a[lower];
a[lower]=a[q];
a[q]=t;
return q;

You might also like