Cod sursa(job #3208635)

Utilizator WilIiamperWilliam Damian Balint WilIiamper Data 29 februarie 2024 08:52:38
Problema Sortare prin comparare Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.39 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <vector>
#include <map>
#include <iomanip>
#include <bitset>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <climits>
#include <cstring>

#define LL long long
#define billion 1000000000
const LL mod = 666013;
const double pi = 3.14159265359;

#define maxn 505

using namespace std;

ifstream fin("algsort.in");
ofstream fout("algsort.out");

int n;
vector<int> arr;

int Partition(int low, int high) {
    int pivot = low;

    int newPivotPos = high; 
    for (int i = low; i <= newPivotPos;) {
        if (arr[i] > arr[pivot]) {
            swap(arr[i], arr[newPivotPos]);
            newPivotPos--;
        }
        else
            i++;
    }

    swap(arr[pivot], arr[newPivotPos]);
    return newPivotPos;
}

void QuickSort(int low, int high) {
    if (low >= high)
        return;

    int newPivotPos = Partition(low, high);
    QuickSort(low, newPivotPos-1);
    QuickSort(newPivotPos+1, high);
}

void read() {
    fin >> n;

    int x;
    for (int i = 1; i <= n; i++) {
        fin >> x;
        arr.push_back(x);
    }
}

void print() {
    for (int i = 0; i < n; i++)
        fout << arr[i] << " ";
}

int main() 
{   
    read();
    QuickSort(0, n-1);
    print();
    return 0;
}