C
camotito
Hi, I want to sort an array of pointers, each position of this array
point to a position in an integer array.
There is a strange behavior when the 'pivot' is '0'. I am using Visual
C++, and when executing the OS tell me "quicksort.exe has encountered
a problem and needs to close". When there is no zeros in the integer
array, this doesn't happen.
The pivot is always the last element. Try to change the last element in
the array for a non zero value and you will se it works.
Tell me something please. Thanks
#include <iostream>
using namespace std;
int *buffer[10];
int array[10] = {4,1,14,9,2,3,6,11,8,0};
void quicksort(int **vector, int inf, int sup)
{
int *temp;
int pivot = *vector[sup];
int i = inf-1;
int j = sup;
int cont = 1;
if(inf>=sup) return;
cout << "pivot " << pivot << endl;
while(cont)
{
while(*vector[++i] < pivot);
while(*vector[--j] > pivot);
if(i < j)
{
temp = vector;
vector = vector[j];
vector[j] = temp;
}
else cont = 0;
}
temp = vector;
vector = vector[sup];
vector[sup] = temp;
quicksort(vector, inf, i-1);
quicksort(vector, i+1, sup);
}
int main()
{
for(int i=0; i<10; i++)
buffer = &array;
for(i=0; i<10; i++) cout << *buffer << " ";
cout << endl;
quicksort(buffer, 0, 9);
cout << "Sorted array : " << endl;
for(i=0; i<10; i++) cout << *buffer << " ";
cout << endl;
return 0;
}
point to a position in an integer array.
There is a strange behavior when the 'pivot' is '0'. I am using Visual
C++, and when executing the OS tell me "quicksort.exe has encountered
a problem and needs to close". When there is no zeros in the integer
array, this doesn't happen.
The pivot is always the last element. Try to change the last element in
the array for a non zero value and you will se it works.
Tell me something please. Thanks
#include <iostream>
using namespace std;
int *buffer[10];
int array[10] = {4,1,14,9,2,3,6,11,8,0};
void quicksort(int **vector, int inf, int sup)
{
int *temp;
int pivot = *vector[sup];
int i = inf-1;
int j = sup;
int cont = 1;
if(inf>=sup) return;
cout << "pivot " << pivot << endl;
while(cont)
{
while(*vector[++i] < pivot);
while(*vector[--j] > pivot);
if(i < j)
{
temp = vector;
vector = vector[j];
vector[j] = temp;
}
else cont = 0;
}
temp = vector;
vector = vector[sup];
vector[sup] = temp;
quicksort(vector, inf, i-1);
quicksort(vector, i+1, sup);
}
int main()
{
for(int i=0; i<10; i++)
buffer = &array;
for(i=0; i<10; i++) cout << *buffer << " ";
cout << endl;
quicksort(buffer, 0, 9);
cout << "Sorted array : " << endl;
for(i=0; i<10; i++) cout << *buffer << " ";
cout << endl;
return 0;
}