Pagini recente » Cod sursa (job #2678552) | Cod sursa (job #1537231) | Cod sursa (job #418385) | Cod sursa (job #672171) | Cod sursa (job #947277)
Cod sursa(job #947277)
// QuickSort Implementation
// MergeSort Implementation with n/2 additional memory
// HeapSort Implementation
// The algorithms were not optimised, I have implemented them mostly for code clarity
// 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++]);
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 - 1);
sort(arrayToSort, part + 1, endIndex);
}
};
int main()
{
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;
}