954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

quick Sort

hay i need some help here i am trying to do a quick sort this is the code i have and i am getting some errors i do not understand.
this is the definition

void quickSort(int numbers[], int first, int last)
{
int i, pivot, left, right, mid;
int passes = last - first;
if (passes > 0)
{
if (numbers[first] > numbers[last])
swap(&numbers[first], &numbers[last]);
if (passes > 1)
{
for (i = first + 1; i < last; i++)
{
if (numbers[i] < numbers[first])
swap(numbers[i], &numbers[first]);
else if (numbers[i] > numbers[last])
swap (&numbers[i], &numbers[last]);
}
if (passes > 2)
{
mid = (numbers[first] + numbers[last]) / 2;
pivot = numbers[first + 1];

for (i = first + 2; i < last; i++)
{
if (abs(numbers[i] - mid) < abs(pivot - mid))
pivot = numbers[i];
}
left = first + 1;
right = last - 1;
while (left < right)
{
while(numbers < pivot)
left++;
while (numbers > pivot)
right--;
if (left < right)
swap(&numbers, &numbers);
if (numbers == pivot && numbers == pivot)
left++;
}
quickSort(numbers, first + 1, right - 1);
quickSort(numbers, right + 1, last - 1);
}
}
}

kendell
Newbie Poster
10 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

what are the errors?

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

can some one please help me? I am trying to find a best case, worst case and average case in a sorting function. I have no idea how to go about doing that, can some one please help me?

kendell
Newbie Poster
10 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

I reformatted your code so that I could read it. Your compiler should have produced errors on lines 35 and 37 because variable numbers is an array, not a single integer. Correct all the compile errors before attempting to go on with the assignment.

#include<stdio.h> 
#include <stdlib.h>

void quickSort(int numbers[], int first, int last)
{
    int i, pivot, left, right, mid;
    int passes = last - first;
    if (passes > 0)
    {
        if (numbers[first] > numbers[last])
            swap(&numbers[first], &numbers[last]);
        if (passes > 1)
        {
            for (i = first + 1; i < last; i++)
            {
                if (numbers[i] < numbers[first])
                    swap(numbers[i], &numbers[first]);
                else if (numbers[i] > numbers[last])
                    swap (&numbers[i], &numbers[last]);
            }
            if (passes > 2)
            { 
                mid = (numbers[first] + numbers[last]) / 2;
                pivot = numbers[first + 1];

                for (i = first + 2; i < last; i++)
                {
                    if (abs(numbers[i] - mid) < abs(pivot - mid))
                        pivot = numbers[i];
                }
                left = first + 1;
                right = last - 1;
                while (left < right)
                {
                    while(numbers < pivot)
                        left++;
                    while (numbers > pivot)
                        right--;
                    if (left < right)
                        swap(&numbers, &numbers);
                    if (numbers == pivot && numbers == pivot)
                        left++;
                }
                quickSort(numbers, first + 1, right - 1);
                quickSort(numbers, right + 1, last - 1);
            }
        }
    }
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
can some one please help me? I am trying to find a best case, worst case and average case in a sorting function. I have no idea how to go about doing that, can some one please help me?


I think you will need several different sort algorithms, such as compare bubble sort with quick sort and several others. You have to code each sort algorithm then run the same set of data through each sort method, placing timers before and after to get the amount of time it takes for the algorithm to sort the data. Because computers do their work so quickly you will have to run each sort algorithm 1,000 or more times and to get measurable amount of time. Here's an example

clock_t startTime, endTime, deltaTime;

// time the bubble sort algorithm
startTime = clock(); // start the timer
for(int i = 0; i < 10000; i++)
     DoBubbleSort( <parameters here> );
endTime = clock(); // end the timer
deltaTime = endTime - startTime;
printf("Bubble sort took %u time\n", deltaTime);


now do each of the other algorithms the same way

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

No, I think he/she wants a big 'O' explanation, running code proves nothing.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Oh, then I'd suggest reading this I really don't know that much about it either, never had the occasion to use it.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

hay thank you. another question, how can i test to see if a sort function is already sorted?

kendell
Newbie Poster
10 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

you mean if the data is already sorted, not the sort function. :)

You have to iterate through the array and check if any element is not in order.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You