473,387 Members | 1,592 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,387 software developers and data experts.

QuickSort . I don't understand this behavior

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[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()
{
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 24 '06 #1
2 2142
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

camotito wrote:
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++,
Sorry, but C++ is off topic in comp.lang.c

You might want to try comp.lang.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>

[snip]

Yup. Definitely not C

- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFETPWgagVFX4UWr64RAinNAKDJ6CTecHbTbcFKZEfh4i n/ZtC1GwCgur1R
dUDzcfgJRmVQQdjO3xJ9jlM=
=wb/c
-----END PGP SIGNATURE-----
Apr 24 '06 #2
camotito schrieb:
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
I will ignore the C++ specific stuff; read the comp.lang.c++
FAQ or post your code for code review to comp.lang.c++ -- your code
leaves much room for improvement.

Your problem is that you do not control the indices. Print the
indices within the corrected loops and at the beginning of
quicksort to see what is going wrong.

<snip>
void quicksort(int **vector, int inf, int sup)
{
int *temp;
int pivot = *vector[sup];
You are accessing vector[sup] without having checked whether
sup >= inf.
int i = inf-1;
int j = sup;
int cont = 1;

if(inf>=sup) return;
<snip>
while(cont)
{
while(*vector[++i] < pivot);
while(*vector[--j] > pivot);
while (i < j && *vector[++i] < pivot);
while (i < j && *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);
quicksort(vector, inf, i > inf ? i-1 : inf);
quicksort(vector, i < sup ? i+1 : sup, sup);
}


<snip>

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 24 '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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.