C Programming: Creating recursive quickSort function

In summary: And then realizing that approach you took could never, ever yield what you wanted and you have to start again. Very satisfying indeed :tongue2:
  • #1
cubanmissile
5
0

Homework Statement


Function:
Code:
void quickSort(struct student *array, int start, int end) {

	int i = start;
	int j = end;

	while(i < j) {

		// Move left counter, then right counter.
		while(i <= end && array[i].numDays <= array[start].numDays)
			i++;

		while(array[j].numDays > array[start].numDays)
			j--;

		if(i < j)
			swap(array, i, j);

	}

	swap(array, start, j);

	if(array[j].numDays < array[start].numDays)
		quickSort(array, 0, j);
	if(array[j].numDays > array[end].numDays)
		quickSort(array, j, end);

}


Homework Equations


None


The Attempt at a Solution


I have tried many different versions. Changing the variables from array[j] to array > array[start], as well as changing the bottom statement. This doesn't correctly sort the array however... Is there something I am missing? Look at this, I would *assume* it would work.

Here's why:

First, it increases the left index...then the right. Once all that's done and the elements are swapped, it checks to see if the element on the right index is larger than the element at the left index. If so, run quickSort again. Then after that recursive call, it then checks to the right of the partition element. If it's larger than the end element, recursively call until it is not.

So, why would it not be working?
 
Physics news on Phys.org
  • #2
Use debugger or add a call to a function that will display the content of the sorted table, this way you will see for yourself what is happening and where the function does something different than you think it does.
 
  • #3
A couple of things that come to mind:
1) You don't show the code for your swap function. Without seeing it, I can't tell if it's working correctly.

2) In this code --
Code:
if(array[j].numDays < array[start].numDays)
   quickSort(array, 0, j);
if(array[j].numDays > array[end].numDays)
   quickSort(array, j, end);
what happens if array[j].numDays == array[start].numDays?
 
  • #4
Mark44 said:
A couple of things that come to mind:
1) You don't show the code for your swap function. Without seeing it, I can't tell if it's working correctly.

2) In this code --
Code:
if(array[j].numDays < array[start].numDays)
   quickSort(array, 0, j);
if(array[j].numDays > array[end].numDays)
   quickSort(array, j, end);
what happens if array[j].numDays == array[start].numDays?

Actually, it was my sort function that was causing the quickSort to not work. :) Thank you guys very much for the help! Programming is fun (not sarcasm, I truly love programming)!
 
  • #5
cubanmissile said:
Actually, it was my sort function that was causing the quickSort to not work. :) Thank you guys very much for the help! Programming is fun (not sarcasm, I truly love programming)!

I think so, too. It's very satisfying to write some code (that typically has a few bugs in it), and then figure out why it's not doing what you want it to do.
 
  • #6
Mark44 said:
I think so, too. It's very satisfying to write some code (that typically has a few bugs in it), and then figure out why it's not doing what you want it to do.

And then realizing that approach you took could never, ever yield what you wanted and you have to start again. Very satisfying indeed :tongue2:
 

What is recursive quicksort and how does it work?

Recursive quicksort is a sorting algorithm used to sort elements in an array. It works by selecting a pivot element and partitioning the array into two subarrays - one containing elements smaller than the pivot and the other containing elements larger than the pivot. This process is repeated recursively until the entire array is sorted.

What is the time complexity of recursive quicksort?

The time complexity of recursive quicksort is O(nlogn) in the average case and O(n^2) in the worst case. This means that it is an efficient algorithm for sorting large arrays, but may perform poorly on already sorted or nearly sorted arrays.

What are the advantages of using recursive quicksort?

Recursive quicksort has several advantages, including its relatively simple implementation, efficient average case time complexity, and ability to handle large arrays. It also has a small memory footprint as it sorts the elements in-place without requiring additional memory.

What are the limitations of recursive quicksort?

One limitation of recursive quicksort is its worst-case time complexity of O(n^2), which can occur when the pivot is chosen poorly. This can be mitigated by using randomized pivot selection. Additionally, recursive quicksort is not a stable sorting algorithm, meaning that the relative order of equal elements may change after sorting.

How can I implement a recursive quicksort function in C?

To implement a recursive quicksort function in C, you will need to define a function that takes in an array, the starting and ending indices of the subarray to be sorted, and a pivot index. The function should partition the array using the pivot index, and then recursively call itself on the two resulting subarrays until the entire array is sorted. It is important to handle base cases, such as when the subarray has only one element. A sample implementation can be found in many online resources.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
925
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top