#include <iostream>
using namespace std;
void quicksort( int *array, int start, int end );
int divide( int *array, int start, int end );
void imprimir( int *array, int size );
int main(){
int size;
cout << "Tamanyo del array: "; cin >> size;
int a[size];
cout << "\nIntroduzca los valores del array:\n";
for (int i = 0; i < size; i++) {
cout << "(" << i << "/" << size << "): ";
cin >> a[i];
}
cout << "\nArray introducido:\n";
imprimir( a, size );
cout << endl << endl;
quicksort(a, 0, size - 1);
cout << "\nArray ordenado:\n";
imprimir( a, size );
cout << endl;
return 0;
}
void quicksort(int *array, int start, int end){
int pivot;
if (start < end) {
pivot = divide(array, start, end);
quicksort(array, start, pivot - 1);
quicksort(array, pivot + 1, end);
}
}
int divide(int *array, int start, int end) {
int left;
int right;
int pivot;
int temp;
pivot = array[start];
left = start;
right = end;
while (left < right) {
while (array[right] > pivot) {
right--;
}
while ((left < right) && (array[left] <= pivot)) {
left++;
}
if (left < right) {
temp = array[left];
array[left] = array[right];
array[right] = temp;
}
}
temp = array[right];
array[right] = array[start];
array[start] = temp;
return right;
}
void imprimir( int *array, int size ){
for (int i = 0; i < size; i++) {
cout << array[i] << " ";
}
}