Pagini recente » Cod sursa (job #1569699) | Cod sursa (job #1432490) | Cod sursa (job #1791476) | Cod sursa (job #990074) | Cod sursa (job #946735)
Cod sursa(job #946735)
// 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;
}