Trở về đầu
Mời các bạn download tại: Click here
#include <iostream>
#include <limits.h>
using namespace std;
const int MAXN = 1000;
const int oo = INT_MAX;
int n;
int a[MAXN];
void Nhap() {
cout << "Nhap N = "; cin >> n;
int i;
for (i=1; i<=n; i++) {
cout << " - Nhap phan tu thu " << i << ": ";
cin >> a[i];
}
}
void SapXepQS(int L, int R) {
if (L >= R) return;
//......: L < R
int i = L;
int j = R;
int x = a[(i+j)/2];
do {
while (a[i] < x) i++;
while (x < a[j]) j--;
if (i <= j) {
int tg = a[i];
a[i] = a[j];
a[j] = tg;
i++;
j--;
}
}
while (i <= j);
SapXepQS(L, j);
SapXepQS(i, R);
}
void InKQ() {
int i;
for (i=1; i<=n; i++) cout << a[i] << " ";
}
int main() {
Nhap();
SapXepQS(1, n);
InKQ();
}