Xuất ra sự biến đổi của Array khi dùng quicksort

Cho em xin hỏi vấn đề này ạ, em đang làm quick sort trong class (oop) thì em làm được bth nhưng mà em muốn với mỗi lần Array biến đổi thì em muốn nó xuất ra sự biến đổi đó nhưng vẫn bị lỗi

các file trên em chạy được rồi ạ, giờ em muốn nó xuất từng trường hợp nó biến đổi arr thì nên thêm như thế nào ạ

quicksort.h

#pragma once
#include <iostream>
#include <io.h>
#include <fcntl.h>
#include <cstdlib>
#include <ctime> 
#include <fstream>
#include <algorithm>
#include <windows.h>
#include <stdio.h>
#include <fcntl.h>
#include <conio.h>
//include"thuvien.h"
using namespace std;
class quicksort
{
private:
	int length;
	int* Arr;
	
public:
	quicksort();
	quicksort(int n, int x);
	~quicksort();
	void Swap(int &m, int &n);
	int GetNumber();
	void ChangeArraytoIncreasedArray();
	void Increase(int left, int right);
	//void Decrease(int left, int right);
	//void ChangeArraytoDecreasedArray();
	void Xuat();
	friend istream& operator >> (istream&, quicksort&);
	friend wostream& operator << (wostream&, const quicksort&);
};

quickshort.cpp

#include "quicksort.h"
quicksort::quicksort()
{
	length = 0;
	Arr = new int[length];
}

quicksort::quicksort(int n, int x)
{
	Arr = new int[n];
	for (int i = 0; i < n; i++)
	{
		Arr[i] = x;
	}
}

quicksort::~quicksort()
{
	delete[] Arr;
}

istream& operator >> (istream& is, quicksort& S)
{
	_setmode(_fileno(stdout), _O_U16TEXT);
	do
	{
		wcout << L"\nNhập vào số phần tử: "; is >> S.length;
		if (S.length < 0) wcout << L"\nKhông hợp lệ! Vui lòng nhập lại!\n";
	} while (S.length < 0);
	S.Arr = new int[S.length];
	wcout << L"\nHãy nhập các phần tử của mảng:\n";
	for (int i = 0; i < S.length; i++)
	{
		wcout << "Arr[" << i << "] = ";
		is >> S.Arr[i];
	}
	return is;
}

wostream& operator << (wostream& os, const quicksort& S)
{
	os << "\nArr = ";
	for (int i = 0; i < S.length; i++)
	{
		os << S.Arr[i] << " ";
	}	
	return os;
}

void quicksort::Swap(int& m, int& n)
{
	int temp;
	temp = m;
	m = n;
	n = temp;
}

int quicksort::GetNumber()
{
	return length;
}

void quicksort::Xuat()
{
	for (int k = 0; k < length; k++)
	{
		wcout << Arr[k] << " ";
	}
	cout << endl;
}

void quicksort::Increase(int left, int right)
{
	int i, j, x; //x làgiá trị làm cột để tiến hành quicksort

	int n = length;
	x = Arr[(left + right) / 2];
	i = left; j = right;
	while (i <= j) {
		while (Arr[i] < x) i++;
		while (Arr[j] > x) j--;
		if (i <= j)
		{
			quicksort::Swap(Arr[i], Arr[j]);
			i++; j--;			
		}
	}
	if (left < j)
		quicksort::Increase(left, j);
	if (i < right)
		quicksort::Increase(i, right);
}

void quicksort::ChangeArraytoIncreasedArray()
{
	int x = GetNumber();
	quicksort::Increase(0, x-1);
}

main

#include"quicksort.h"

int main()
{
	quicksort a;
	cin >> a;
	wcout << a << endl;
	int x = a.GetNumber();
	a.ChangeArraytoIncreasedArray();
	wcout << a;
	return 0;
}

em mong được mọi người giúp đỡ ạ

như thế nào là biến đổi? có vẻ như là khi có cặp giá trị trong mảng được swap
thì mỗi lần swap thì xuất ra lúc swap thôi

2 Likes


dạ nó sẽ bị lỗi Debug Asertion Failed ở line 49 (line bắt đầu hàm Swap) á anh


nếu em không cho endl thì các Arr[i] nó sẽ dính vào nhau nhưng nếu em cho endl thì sẽ báo lỗi như hình trên á anh

à rồi em tìm được lỗi rồi ạ, phải chỉnh cout lại thành wcout
cảm ơn anh nhiều

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