Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with QuickSort 1

Status
Not open for further replies.

KT927

Programmer
Oct 1, 2000
5
0
0
US
I am currently trying to use a quicksort on an array already created in a file. I'm having some difficulty. I'm not sure how I output the QuickSort. I know I need the Function header in my main and in the header file. I know the function needs a cout statement, but I'm not sure how. The code below is what I have so far.

void Swap (float&x, float&y)
{
float temp;
temp = x;
x = y;
y = temp;
}

void Partition(float kArray[], int first, int last, int& PivotIndex)
{
float Pivot = kArray[first];

int left = first;
int right = last;

while(left<right)
{
while (kArray
>Pivot)
{
right = right -1;

while((kArray
<=Pivot) && (left<right))
{
left=left+1;

Swap(kArray
, kArray
);
}
int PivotIndex = left;
}

}

}

void Quicksort(float kArray[], int first, int last)
{
int PivotIndex;
if (first < last)
{
Partition(kArray, first, last, PivotIndex);
Quicksort(kArray, first, PivotIndex-1);
Quicksort(kArray, PivotIndex+1, last);
}
int i;
for (i = 0; i<PivotIndex; i++)

cout<<kArray<<&quot;\n&quot;;

} [sig][/sig]
 
Tell you what. I'm a highschool student that just covered the quick sort last year, and I could help you out if I could read your code. If you make it readable, I'll do my best to help you out, okay? Alrighty then, good luck.
Mattholomew [sig][/sig]
 
OOps...I didn't realize that happend. Here is the code a little clearer.
Thanks,


void Swap (float&x, float&y)
{
float temp;
temp = x;
x = y;
y = temp;
}

void Partition(float kArray[], int first, int last, int& PivotIndex)
{
float Pivot = kArray[first];

int left = first;
int right = last;

while(left<right)
{
while (kArray>Pivot)
{
right = right -1;

while((kArray
<=Pivot) && (left<right))
{
left=left+1;

Swap(kArray
, kArray);
}
int PivotIndex = left;
}

}

}

void Quicksort(float kArray[], int first, int last)
{
int PivotIndex;
if (first < last)
{
Partition(kArray, first, last, PivotIndex);
Quicksort(kArray, first, PivotIndex-1);
Quicksort(kArray, PivotIndex+1, last);
}
int i;
for (i = 0; i<PivotIndex; i++)

cout<<kArray<<&quot;\n&quot;;

} [sig][/sig]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top