1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#include <iostream>
//I know it's not popular but for now im forced to use it
#include <conio.h>
//custom library at my school, i dont think its used in this code part
#include"console(v1.9).h"
#include"QuickSort.h"
using namespace std;
int pivot=0;
int nbElements = 0;
void DisplayArray(int tab[], int nbElements)
{
for (int n = 0; n < nbElements; n++)
cout << tab[n] << ' ';
cout << endl;
}
void Swap(int tab[], int left, int right)
{
int temp = tab[left];
tab[left] = tab[right];
tab[right] = temp;
}
int Partition(int tab[], int left, int right)
{
int l = left, r = right, temp=0;
int pivot = l;
while (tab[l] <= tab[pivot])
l++;
while (tab[r] > tab[pivot])
r--;
if (l <= r)
{
Swap(tab, l, r);
DisplayArray(tab, nbElements);
pivot = l;
}
else
{
Swap(tab, pivot, r);
DisplayArray(tab, nbElements);
pivot = d;
}
return pivot;
}
void QuickSort(int tab[], int left, int right)
{
if (left < right)
{
pivot = Partition(tab, left, right);
QuickSort(tab, left, pivot - 1);
QuickSort(tab, pivot + 1, right);
}
}
void UI_QuickSort()
{
const int NB_NOMBRES_MIN = 10;
const int NB_NOMBRES_MAX = 20;
int nbNombres = 10;
int tabNombres[NB_NOMBRES_MAX] = {10,55,22,40,45,31,5,3,60,11};
cout << "Numbers : ";
DisplayArray(tab, nbElements);
QuickSort(tab, 0, nbElements - 1);
DisplayArray(tabNombres, nbNombres);//should display sorted numbers
_getch();
}
|