Cod sursa(job #947271)

Utilizator FoaiaFoaia de Hartie Foaia Data 7 mai 2013 06:42:14
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.87 kb
// QuickSort Implementation
// MergeSort Implementation with n/2 additional memory
// HeapSort Implementation
 
// Each tested for all corner case

#include <vector>
#include <algorithm>
#include <stdio.h>
 
using namespace std;





class HeapSort
{
    vector <int> arrayHeap; // arrayHeap[0] stores the size of the heap
    
    void heapDown(int node)
    {
        int candidate = 0;
        
        if ((node << 1) <= arrayHeap[0])
            candidate = node << 1;
        else return;
        
        if ((node << 1) < arrayHeap[0] && arrayHeap[node << 1] > arrayHeap[(node << 1) + 1])
            candidate++;
            
        if (arrayHeap[node] > arrayHeap[candidate])
        {
            swap(arrayHeap[node], arrayHeap[candidate]);
            
            heapDown(candidate);
        }
    }
    
public:
    void sort(vector <int> &arrayToSort, int startIndex, int endIndex)
    {
        for (arrayHeap.push_back(0); arrayHeap[0] < arrayToSort.size(); arrayHeap[0]++)
            arrayHeap.push_back(arrayToSort[arrayHeap[0]]);
            
        for (int i = arrayHeap[0]; i; i--)
            heapDown(i);
        
        for (int i = 0; i < arrayToSort.size(); i++)
        {
            arrayToSort[i] = arrayHeap[1];
            
            arrayHeap[1] = arrayHeap[arrayHeap[0]--];
            heapDown(1);
        }
    }
};






class MergeSort
{
    void join(vector <int> &arrayToSort, int startIndex, int endIndex)
    {
        int midIndex = (startIndex + endIndex) >> 1;
        int length = midIndex - startIndex + 1;
        
        vector <int> auxArray(length);
        
        for (int i = startIndex; i <= midIndex; i++)
            auxArray[i - startIndex] = arrayToSort[i];
            
        for (int i = 0, j = midIndex + 1, poz = startIndex; i < length || j <= endIndex; poz++)
            if (i == length || (j <= endIndex && auxArray[i] > arrayToSort[j]))
                arrayToSort[poz] = arrayToSort[j++];
            else arrayToSort[poz] = auxArray[i++];
    }
    
public:
    void sort(vector <int> &arrayToSort, int startIndex, int endIndex)
    {
        if (startIndex == endIndex)
            return;
            
        int midIndex = (startIndex + endIndex) >> 1;
        
        sort(arrayToSort, startIndex, midIndex);
        sort(arrayToSort, midIndex + 1, endIndex);
    
        join(arrayToSort, startIndex, endIndex);
    }
};






class QuickSort
{
    int partition(vector <int> &arrayToSort, int startIndex, int endIndex)
    {
	int place = startIndex;
        for (int pivot = arrayToSort[endIndex]; startIndex < endIndex; startIndex++)
        {
            if (arrayToSort[startIndex] < pivot)
            {
                swap(arrayToSort[startIndex], arrayToSort[place++]);
                
                startIndex++;
                endIndex--;
            }
        }
	swap(arrayToSort[place], arrayToSort[endIndex]);
        
        return place;
    }
    
public:
    void sort(vector <int> &arrayToSort, int startIndex, int endIndex)
    {
        if (startIndex >= endIndex)
            return;
            
        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]);
    
    //HeapSort().sort(vctSort, 0, n - 1);

    //MergeSort().sort(vctSort, 0, n - 1);

    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;
}