quick sort.seems like the function quicksort(int [],int,int) is not at all called

#include <iostream>

using namespace std;
int split(int a[],int p,int r)
{
int x=a[r];
int i=p-1;int j=r+1;
while(1)
{
do{
i++;
}while(a[i]<=x);


do{
j--;
}while(a[j]>=x);

if(i<j)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else{
return j;
}
}
}

int* quicksort(int a[],int p,int r)
{
if(p<r)
{

int q=split(a,p,r);

quicksort(a,p,q);
quicksort(a,q+1,r);
}
else{
return a;}

}
int main()
{
int a[50],s;
cout<<"enter num of elemenst\n";
cin>>s;
cout<<"enetr "<<s<<" elemets\n";
for(int i=0;i<s;i++)
{cin>>a[i];}
cout<<"\nbefore sorting\n";
for(int i=0;i<s;i++)
{cout<<a[i]<<"\t";}

int* s_arr= quicksort(a,0,s-1);
for(int i=0;i<s;i++)
cout<<"\t"<<s_arr[i];


return 0;
}
Last edited on
Previously I used this code for sorting:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void selectionSort(string name[], int elems)
{
	int startScan, minIndex;
	string strName;
	for(startScan = 0; startScan < (elems - 1); startScan++)
	{
		minIndex = startScan;
		strName = name[startScan];
		for(int index = startScan + 1; index < elems; index++)
		{
			if(name[index] < strName)
			{
				strName = name[index];
				minIndex = index;
			}
		}
		name[minIndex] = name[startScan];
		name[startScan] = strName;
	}
}



You can call it like that:

 
selectionSort(here_what_you_want_to_sort, the_number_of_elements)


The input, what you would like to sort have to be string array.
quicksort is called, what does this line look like to you:
int* s_arr= quicksort(a,0,s-1);
Topic archived. No new replies allowed.