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

quick sort

Hi folks,

this is the program implementing quicksort

#include<stdio.h>
#include<stdlib.h>
#define swap(a,b){int t; t=a;a=b;b=t;}

int partition(int[],int,int);
void quicksort(int[],int,int);

int main(void)
{
int a[5],i;

for(i=0;i<5;a[i] = (rand()%100)+1,i++);

puts("unsorted array:: ");
for(i=0;i<5;printf("\t%d",a[i]),i++);

quicksort(a,0,5);
puts("sorted array:: ");
for(i=0;i<5;printf("\t%d",a[i]),i++);

return(EXIT_SUCCESS);
}

void quicksort(int a[5],int p,int q)
{
int j;
if(p < q)
{
j = partition(a,p,q+1);
quicksort(a,p,j-1);
//quicksort(a,j+1,q);

}
return;
}

int partition(int a[5],int m,int p)
{
int v = a[m], i = m, j = p;
do
{
do{ i += 1;}while(a[i] <= v && i <= p );

do{j -= 1;}while(a[j] >= v && j >= m);

if(i < j) swap(a[i],a[j]);

}while(i <= j && i <= p);
a[m] = a[j]; a[j] = v;
return j;
}

my questions are as follows

1)
this program works well on some compilers(djgpp,bloodshed devc++ 4.0)
it is not working on turboc++ version 3.0, i am getting junk values
how to rectify this???

2) is the commented statement in the quicksort function(
quicksort(a,j+1,q); ) redundant
the partitioning is based on the following algorithm

algorithm partition(a,m,p)
/* within a[m],a[m+1].....a[p-1] the elements are re arranged in such
a manner that if initially t = a[m],then after completion a[q]=t for

some q between m and p-1,a[k] <= t for m <= k < q,and a[k] >= t
for q < k < p,q is returned,set a[p] = infinity
*/
{
v = a[m];i=m,j=p;
repeat
{
repeat
i = i+1;
until(a[i] >= v);

repeat
j = j-1;
until(a[j] <= v);

if(i < j) swap(a[i],a[j])

}until(i >= j);

a[m] = a[j];a[j]=v;
return j;
}

Apr 11 '06 #1
1 4089
aa*****@gmail.com schrieb:
Hi folks,

this is the program implementing quicksort

#include<stdio.h>
#include<stdlib.h> #define swap(a,b){int t; t=a;a=b;b=t;}
If you do not need this very often, consider not using this
macro. It is not exactly generic and does not even give much
clarity.
int partition(int[],int,int);
void quicksort(int[],int,int);
Note that the compiler may be able to help you better if you
give the parameter names in the prototypes.
The above is the same as
int partition(int *,int,int);
void quicksort(int *,int,int);
which has the advantage of making it clear that you work with
pointers.
int main(void)
{
int a[5],i;

for(i=0;i<5;a[i] = (rand()%100)+1,i++);
5 is a magic number here, consider using a symbolic constant
(via #define ARRAY_SIZE 5 or similar) instead.
The above is the same as
for(i=0;i<5;i++)
a[i] = (rand()%100)+1;
which certainly is easier to grasp at one glance.

puts("unsorted array:: ");
for(i=0;i<5;printf("\t%d",a[i]),i++);

quicksort(a,0,5);
This seems to imply that quicksort sorts a from 0 to 5-1.
puts("sorted array:: ");
for(i=0;i<5;printf("\t%d",a[i]),i++);

return(EXIT_SUCCESS);
I would have written a printIntArray(int *array, size_t size)
and a populateIntArray(int *array, size_t size) function such
that main() only obtains the input, calls populateIntArray(),
printIntArray(), quicksort() (yes, I'd have called it something
with Int), and printIntArray() and returns finally.
}

void quicksort(int a[5],int p,int q)
The 5 has no meaning whatsoever, so just leave it out.
{
int j;
if(p < q)
{
j = partition(a,p,q+1);
The above call seems to have implied that "q" is an exclusive
upper array index -- seeing "q+1" passed to partition() does not
exactly fill me with confidence.
quicksort(a,p,j-1);
//quicksort(a,j+1,q);
Why do you not want to sort the upper half of the respective
array? Read up on quicksort, especially the "hard split, easy
join" part.

}
return;
}

int partition(int a[5],int m,int p)
Same as above.
{
int v = a[m], i = m, j = p;
do
{
do{ i += 1;}while(a[i] <= v && i <= p );

do{j -= 1;}while(a[j] >= v && j >= m);

if(i < j) swap(a[i],a[j]);

}while(i <= j && i <= p);
Oh, it gets just better: In this case, p is potentially
an inclusive upper bound which in principle could make
access to a[6] possible while even a[5] is beyond the
array bounds.

What happens if you pass in m==0, p==0 or m==upper, p==upper?
You access a[-1] or a[upper+1] respectively -- this must not
happen.
a[m] = a[j]; a[j] = v;
return j;
}

my questions are as follows

1)
this program works well on some compilers(djgpp,bloodshed devc++ 4.0)
it is not working on turboc++ version 3.0, i am getting junk values
how to rectify this???
Write a working algorithm.

You do not seed rand() with a random value via srand(), so you get
the same values all the time. Usually one seeds with "time(NULL)".

Take pen and paper and work through your algorithm for array sizes
1, 2, ..., 7, 8 and 16 for values in ascending order, values in
descending order, all the same values, one mixed example with an
ascending series on the even and a descending series on the odd
elements with two elements of equal value.

2) is the commented statement in the quicksort function(
quicksort(a,j+1,q); ) redundant


No. You effectively sort only one or two numbers correctly, the
rest is only grouped accordingly.

Example: 4,5,6,7,8,3,2,1 will be partitioned how if your
partitioning is implemented in one correct manner?
{1,2,3}{4}{8,7,6,5} and {}{1}{3,2}{4}{8,7,6,5} by a second
step -- this will be the sorted order.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 12 '06 #2

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

Similar topics

12
by: Eva | last post by:
Hi, I try to implement quick sort. I sort vectors by their first value. 10 2 3 4 9 3 5 6 10 4 5 6 must be 9 3 5 6 10 2 3 4 10 4 5 6 The prog works great on maybe 500 vectors, but I have an...
0
by: Frank King | last post by:
Hi, I am using CArray and quick sort funciton to sort an array of double type of data points. I found an article in MSDN HOWTO: Quick Sorting Using MFC CArray-Derived Classes ID: Q216858 ...
5
by: Am | last post by:
hi i came to know that microsoft improved the efficiency of quick sort by using a cutoff of 8 elements and continuing with insertion sort then, do anybody have the details about it please...
11
by: Am | last post by:
hi i came to know that microsoft improved the efficiency of quick sort by using a cutoff of 8 elements and continuing with insertion sort then, do anybody have the details about it please...
1
by: AngelLopez1989 | last post by:
I need the complete C++ program/ algorithm of Quick Sort. Can you please help me out? No pseudocode please. Can you please also explain how to do the quick sort? Thank you!
5
by: neehakale | last post by:
I know that heap sort,quick sort and merg sort are the faster sorting algoritms than the bubble sort,selection sort,shell sort and selection sort. I got an idea,in which situation we can use...
2
by: Rerun05 | last post by:
I have a quick sort that reads in data from a file. I know there is something wrong with the partition part of my quick sort and i know exactly what line of code it is. I keep getting the Error...
10
by: sophia.agnes | last post by:
Dear all, The following is the quick sort function which i have seen in my note book how good is this function ? void sort(int a, int begin, int end) { int pivot,l,r;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.