| | |
Please help with my quicksort
![]() |
•
•
Join Date: Nov 2004
Posts: 2
Reputation:
Solved Threads: 0
I need help ... I think I know what I'm doing one min. then the next I'm lost :o . Please help ...
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#define SIZE 5
void quickSort(int[], int );
void q_sort(int[], int, int ) ;
// ***** Main program *****
int main(void)
{
int list_sorted, count = SIZE; //no. of machines used
int values[SIZE]={4,6,7,5,1}; //no. of task
// **** initialize contents of array; ****
for(int i=0; i<=SIZE;i++)
printf("Original values are: %d", values[i]);
list_sorted = quickSort(values,count);
printf("NUMBERS SORTED IN DECENDING ORDER: %d", list_sorted);
getch();
return 0;
/*******************/
/*The following function defines the array size*/
void quickSort(int numbers[], int array_size) //An array of numbers & their
//size is passed as a parameter.
{
q_sort(numbers, 0, array_size - 1); //a call to q_sort with the array values,
// 0 and the amt of numbers in array
// minus 1 - passed as the parameter
} //end of quickSort
void q_sort(int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left; //l_hold is assigned 0
r_hold = right; //r_hold is assigned array_size minus 1
pivot = numbers[left]; //pivot is assigned a value in array as pivot
while (left < right)
{
while ((numbers[right] >= pivot) && (left < right))
right--;
if (left != right)
{
numbers[left] = numbers[right];
left++;
}
while ((numbers[left] <= pivot) && (left < right))
left++;
if (left != right)
{
numbers[right] = numbers[left];
right--;
}
}
numbers[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(numbers, left, pivot-1);
if (right > pivot)
q_sort(numbers, pivot+1, right);
}
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#define SIZE 5
void quickSort(int[], int );
void q_sort(int[], int, int ) ;
// ***** Main program *****
int main(void)
{
int list_sorted, count = SIZE; //no. of machines used
int values[SIZE]={4,6,7,5,1}; //no. of task
// **** initialize contents of array; ****
for(int i=0; i<=SIZE;i++)
printf("Original values are: %d", values[i]);
list_sorted = quickSort(values,count);
printf("NUMBERS SORTED IN DECENDING ORDER: %d", list_sorted);
getch();
return 0;
/*******************/
/*The following function defines the array size*/
void quickSort(int numbers[], int array_size) //An array of numbers & their
//size is passed as a parameter.
{
q_sort(numbers, 0, array_size - 1); //a call to q_sort with the array values,
// 0 and the amt of numbers in array
// minus 1 - passed as the parameter
} //end of quickSort
void q_sort(int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left; //l_hold is assigned 0
r_hold = right; //r_hold is assigned array_size minus 1
pivot = numbers[left]; //pivot is assigned a value in array as pivot
while (left < right)
{
while ((numbers[right] >= pivot) && (left < right))
right--;
if (left != right)
{
numbers[left] = numbers[right];
left++;
}
while ((numbers[left] <= pivot) && (left < right))
left++;
if (left != right)
{
numbers[right] = numbers[left];
right--;
}
}
numbers[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(numbers, left, pivot-1);
if (right > pivot)
q_sort(numbers, pivot+1, right);
}
Okay...what is it doing that you don't want? As much as I enjoy working with algorithms and data structures, debugging somebody else's quicksort usually isn't enjoyable. Your recursive calls are correct, so the problem is in the partitioning. I see several potential problems, but the best way for you to figure them out is to step through a small data set by hand to check your logic.
I'm here to prove you wrong.
![]() |
Similar Threads
- Quicksort/Insertion sort Hyrbid? (C)
- QuickSort (Computer Science)
Other Threads in the C Forum
- Previous Thread: Many Errors while doing this assignment
- Next Thread: program
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






