473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Quicksort: I don't understand what is going on!

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 a segment violation error occur . When there is
no zeros in the integer
array, or when the pivot is never zero, 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 see it works.
Tell me something please. Thanks.
Also feel free to tell me any improvements you would do.

#include <iostream>
using namespace std;

void quicksort(int **vector, int inf, int sup)
{
int *temp;
int i = inf-1;
int j = sup;
int cont = 1;

if(inf>=sup) return;
int pivot = *vector[sup];
cout << "pivot " << pivot << endl;
while(cont)
{
while(*vector[++i] < pivot);
while(*vector[--j] > pivot);
if(i < j)
{
temp = vector[i];
vector[i] = vector[j];
vector[j] = temp;
}
else cont = 0;
}
temp = vector[i];
vector[i] = vector[sup];
vector[sup] = temp;

quicksort(vector, inf, i-1);
quicksort(vector, i+1, sup);
}

int main()
{
int *buffer[10];
int array[10] = {4,1,14,9,2,3,6,11,8,0};

for(int i=0; i<10; i++)
buffer[i] = &array[i];
for(i=0; i<10; i++) cout << *buffer[i] << " ";
cout << endl;

quicksort(buffer, 0, 9);

cout << "Sorted array : " << endl;
for(i=0; i<10; i++) cout << *buffer[i] << " ";
cout << endl;

return 0;
}

Apr 25 '06 #1
2 2135

camotito escreveu:
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 a segment violation error occur . When there is
no zeros in the integer
array, or when the pivot is never zero, 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 see it works.
Tell me something please. Thanks.
Also feel free to tell me any improvements you would do.

#include <iostream>
using namespace std;

void quicksort(int **vector, int inf, int sup)
{
int *temp;
int i = inf-1;
int j = sup;
int cont = 1;

if(inf>=sup) return;
int pivot = *vector[sup];
cout << "pivot " << pivot << endl;
while(cont)
{
while(*vector[++i] < pivot);
while(*vector[--j] > pivot);
I didn't test your code, but I realized that if pivot is the smallest
value in the sequence (as is the case in your example) you keep
decreasing j beyond the start of the array. You have to stop decreasing
j when you hit the 0 value. Try
while(*vector[--j] > pivot)
if (j == 0)
break;
if(i < j)
{
temp = vector[i];
vector[i] = vector[j];
vector[j] = temp;
}
else cont = 0;
}
temp = vector[i];
vector[i] = vector[sup];
vector[sup] = temp;

quicksort(vector, inf, i-1);
quicksort(vector, i+1, sup);
}

int main()
{
int *buffer[10];
int array[10] = {4,1,14,9,2,3,6,11,8,0};

for(int i=0; i<10; i++)
buffer[i] = &array[i];
for(i=0; i<10; i++) cout << *buffer[i] << " ";
cout << endl;

quicksort(buffer, 0, 9);

cout << "Sorted array : " << endl;
for(i=0; i<10; i++) cout << *buffer[i] << " ";
cout << endl;

return 0;
}


Apr 25 '06 #2

camotito wrote:
using namespace std;

void quicksort(int **vector, int inf, int sup)


That doesn't seem like robust code....

-Brian

Apr 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: why | last post by:
Hi, just been curious. i have learn that quicksorting algorithm is more widely used then heapsort, despite having the same performance indication of O(n log n) anyone knows why? newbie
3
by: Lieven | last post by:
I want to write a generic quicksort over STL containers. We have to parallelize this algorithm afterwards. This is what I've got for now: template<typename containerIterator> void...
2
by: nkharan | last post by:
Hey, Here is the pseudocode of quicksort. It can be observed that there are as many as (n-1) unresolved stack calls in the worst case. Now, how do i reduce this unresolved stack calls? what...
2
by: camotito | last post by:
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...
6
by: Baltazar007 | last post by:
Does anyone know how to make quicksort for single linked list without using array? I know that mergesort is maybe better but I need quicksort!! thnx
2
by: simo | last post by:
Hello to all... I am trying to write an algorithm parallel in order to realize the quicksort. They are to the first crews with C and MPI. The algorithm that I am trying to realize is PARALLEL...
8
by: aparnakakkar2003 | last post by:
hello can any one tell me how i can create program to sort string list(standard template library) using quicksort.
12
by: aparnakakkar2003 | last post by:
can any one tell me if I give the followiing string in input: ABC abc BBC then how I can get ABC abc BBC
3
by: jollyfolly | last post by:
Could you please help me find the error. I myself have (i might be wrong) boiled it down to the for loop because it somehow magically converts a list into an int and tries to iterate over that. But I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.