Pagini recente » Cod sursa (job #2383018) | Cod sursa (job #939207) | Cod sursa (job #615646) | Cod sursa (job #308644) | Cod sursa (job #261139)
Cod sursa(job #261139)
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <stack>
using namespace std;
vector<int> V;
int partition (int Left, int Right){
int piv, aux, i = Left - 1, j = Right + 1;
if (Left == Right - 1){
if (V[Left] > V[Right]){
aux = V[Left];
V[Left] = V[Right];
V[Right] = V[Left];
}
return Left;
}
piv = rand () % (Right - Left - 1) + Left + 1;
while (1){
do {++i;} while (V [i] < piv);
do {--j;} while (V [j] > piv);
if (i < j){
aux = V[i];
V[i] = V[j];
V[j] = aux;
}
else
return j;
}
}
void quick_sort(int Left, int Right){
if (Left < Right){
int Middle = partition (Left, Right);
quick_sort (Left, Middle);
quick_sort (Middle + 1, Right);
}
}
int main(){
int N, i, a;
freopen("algsort.in", "r", stdin);
freopen("algsort.out", "w", stdout);
scanf("%d", &N);
srand(time(NULL));
for (i = 1; i <= N; ++i){
scanf("%d", &a);
V.push_back(a);
}
quick_sort(0, N - 1);
for (i = 0; i < N; ++i)
printf("%d ", V[i]);
}