Pagini recente » Cod sursa (job #3294931) | Cod sursa (job #3295002) | Cod sursa (job #3292729) | Cod sursa (job #2678036) | Cod sursa (job #3294303)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
int partition(vector<int>& arr, int low, int high) {
// Choose the pivot
int pivot = arr[high];
// Index of smaller element and indicates
// the right position of pivot found so far
int i = low - 1;
// Traverse arr[low..high] and move all smaller
// elements on left side. Elements from low to
// i are smaller after every iteration
for (int j = low; j <= high - 1; j++) {
if (arr[j] <= pivot) {
i++;
swap(arr[i], arr[j]);
}
}
// Move pivot after smaller elements and
// return its position
swap(arr[i + 1], arr[high]);
return i + 1;
}
// The QuickSort function implementation
void quickSort(vector<int>& arr, int low, int high) {
if (low < high) {
// pi is the partition return index of pivot
int pi = partition(arr, low, high);
// Recursion calls for smaller elements
// and greater or equals elements
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int n;
vector<int> v;
int main()
{
ios::sync_with_stdio(false);
fin.tie(nullptr);
fin >> n;
for(int i = 0; i < n; ++i)
{
int x;
fin >> x;
v.push_back(x);
}
// sort(v.begin(), v.end());
quickSort(v, 0, n - 1);
for(int i: v)
{
fout << i << " ";
}
fin.close();
fout.close();
return 0;
}