Cod sursa(job #946732)

Utilizator FoaiaFoaia de Hartie Foaia Data 5 mai 2013 18:20:27
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.94 kb
// QuickSort Implementation
// MergeSort Implementation with n/2 additional memory
// HeapSort Implementation
 
#include <vector>
#include <algorithm>
#include <stdio.h>
 
using namespace std;

class HeapSort
{
    vector <int> arrayHeap;
    
    void heapUp(int node)
    {
        if (node > 1 && arrayHeap[node] < arrayHeap[node >> 1])
        {
            swap(arrayHeap[node], arrayHeap[node >> 1]);
            
            heapUp(node >> 1);
        }
    }
    
    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]]);
            
            heapUp(arrayHeap[0]);
        }
        
        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
{
    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]);
    
    HeapSort().sort(vctSort, 0, n - 1);
    
    for (int i = 0; i < n; i++)
        printf("%d ", vctSort[i]);
        
    return 0;
}