Cod sursa(job #946708)

Utilizator FoaiaFoaia de Hartie Foaia Data 5 mai 2013 17:30:03
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
// QuickSort Implementation
// MergeSort Implementation with n/2 additional memory
// HeapSort Implementation
 
#include <vector>
#include <algorithm>
#include <stdio.h>
 
using namespace std;
 
class QuickSort
{
    pair <int, int> partition(vector <int> &arrayToSort, int startIndex, int endIndex)
    {
        for (int pivot = arrayToSort[startIndex]; startIndex < endIndex; )
        {
            for (; arrayToSort[startIndex] < pivot; startIndex++);
            for (; arrayToSort[endIndex] > pivot; endIndex--);
            
            if (startIndex <= endIndex)
            {
                swap(arrayToSort[startIndex], arrayToSort[endIndex]);
                
                startIndex++;
                endIndex--;
            }
        }
        
        return make_pair(startIndex, endIndex);
    }
    
public:
    void sort(vector <int> &arrayToSort, int startIndex, int endIndex)
    {
        if (startIndex >= endIndex)
            return;
            
        pair <int, int> part = partition(arrayToSort, startIndex, endIndex);
        
        sort(arrayToSort, startIndex, part.second);
        sort(arrayToSort, part.first, endIndex);
    }
};
 
int main()
{
	freopen("algsort.in", "r", stdin);
	freopen("algsort.out", "w", stdout);
    int n;
    scanf("%d", &n);
    
    vector <int> vctSort(n);
    
    for (int i = 0; i < n; i++)
        scanf("%d", &vctSort[i]);
    
    random_shuffle(vctSort.begin(), vctSort.end());
    QuickSort().sort(vctSort, 0, n - 1);
    
    for (int i = 0; i < n; i++)
        printf("%d ", vctSort[i]);
        
    return 0;
}