why doesnt this work

1
2
3
4
5
6
7
8
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
34
35
36
37
38
39
40
41
#include <iostream>;
using namespace std;


void Quicksort(int info[],int left,int right){
int pivot = left + (right - left)/2;//it is the middle (will change sometimes but will end up in the middle
int temp;

while(left<=right){
	while(info[left] < pivot){
	left++;
	}

	while(info[right] > pivot){
	right--;
	}

	//swap feature
	if(left<=right){
	temp =	info[left];
	info[left]=info[right];
	info[right] = temp;

	left ++;
	right --;
	}


}

}

int main(){
	int Test[20]={1,2,5,6,7,8,9,21,42,3,57,22,36,4,9,10,11,12,20,35};
	Quicksort(Test,0,19);
	for(int i=0;i<20;i++){
	cout << Test[i]<< "\n";
	}
	system("PAUSE");
	return 0;
}




can someone explain why this isn't sorting
Quicksort is a recursive function. You are not calling it again after you split the array.
1
2
3
4
5
6
7
8
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
34
35
36
37
void Quicksort(int info[],int left,int right){
int pivot = left + (right - left)/2;//it is the middle (will change sometimes but will end up in the middle
int temp;
int pivotvalue = info[pivot];
	
	temp =	info[pivotvalue];
	info[pivotvalue]=info[right];
	info[right] = temp;

while(left<=right){
	while(info[left] < info[pivot]){
	left++;
	}

	while(info[right] > info[pivot]){
	right--;
	}

	//swap feature
	if(left<=right){
	temp =	info[left];
	info[left]=info[right];
	info[right] = temp;

	left ++;
	right --;
	}

	

}
if (left < right){
	Quicksort(info,left,pivot-1);
	Quicksort(info,pivot+1,right);
}

}



I tried recursion but its still not working
Last edited on
1
2
3
4
5
6
7
8
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
34
35
36
37
void Quicksort(int info[],int left,int right){
int pivot = left + (right - left)/2;//it is the middle (will change sometimes but will end up in the middle
int temp;
int pivotvalue = info[pivot];
	
	temp =	pivotvalue;
	pivotvalue=info[right];
	info[right] = temp;

while(left<=right){
	while(info[left] < info[pivot]){
	left++;
	}

	while(info[right] > info[pivot]){
	right--;
	}

	//swap feature
	if(left<=right){
	temp =	info[left];
	info[left]=info[right];
	info[right] = temp;

	left ++;
	right --;
	}

	

}
if (left < right){
	Quicksort(info,left,pivot-1);
	Quicksort(info,pivot+1,right);
}

}


it works for left side of the pivot now but not the right?
ideas?
I would suggest referring to whatever algorithmic source you're using, because you have the algorithm wrong.
Topic archived. No new replies allowed.