Code quicksort sai ở đâu?

Chào mọi người em có xem thuật toán quicksort và làm theo nhưng chạy nó lại sắp xếp sai bét k theo thứ tự tăng dần.Mọi người giúp em sai ở đâu với ạ.

#include <iostream>
using namespace std;
class QSort{
	private:
		int n;
		int s[100];
	public:
		void Nhap();
		void Xuat();
       	int Partition(int left, int right);
       	void swap(int &a, int &b);
       	void QuickSort(int left, int right);
};
void QSort::Nhap(){
	cout << "Nhap so phan tu: ";
	cin >> n;
	for(int i = 0; i < n; i++){
		cout << "Phan tu thu " << i+1 << ": ";
		cin >> s[i];
	}
}
void QSort::Xuat(){
	cout << "Xuat: ";
	for(int i = 0; i < n; i++){
		cout << s[i] << "  ";
	}
}
void QSort::swap(int &a, int &b){
	int temp;
	temp = a;
	a = b;
	b = temp;
}
int QSort::Partition(int left, int right){
		int x= s[left];
		int i = left + 1;
		int j = right;
        do{
        	while((i <= j) && (s[i] <= x)) i++;
        	while((i <= j) && (s[i] > x)) j--;
        	if(i < j){
        		swap(s[i], s[j]);
        		i++;
        		j--;
			}
		}while(i <= j);{
			swap(s[left], s[j]);
		}
		return right;
}
void QSort::QuickSort(int left, int right){
	int k;
	if(left < right){
		k = Partition(left, right);
		QuickSort(left, k-1);
		QuickSort(k+1, right);
	}
}
int main(){
	QSort s;
	int left, right;
	s.Nhap();
	s.Xuat();
	s.QuickSort(left, right);
	s.Xuat();
	return 0;

leftright hiện thời bằng bao nhiêu thế?

2 Likes

Bằng 0 hết hay sao ạ?

Không gán bằng 0 thì làm sao nó bằng 0?

3 Likes

gắn rồi mà vẫn sai ạ

=> chưa init mà pass vào thằng dưới, nên giá trị rác

thêm nữa

right đâu có thao tác gì đâu return làm gì, return chỉ số j.

Nhớ là nên nắm chắc mã giả, rồi tập debug code của mình

4 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?