C Programming: Creating recursive quickSort function

Click For Summary

Discussion Overview

The discussion revolves around the implementation of a recursive quickSort function in C programming, specifically focusing on sorting an array of student structures based on the numDays attribute. Participants explore issues related to the function's correctness and potential bugs in the implementation.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The initial implementation of quickSort is presented, but the author expresses uncertainty about its correctness and functionality.
  • One participant suggests using a debugger or a display function to visualize the array's state during sorting to identify issues.
  • Another participant raises concerns about the absence of the swap function's code, questioning its correctness and how it might affect the quickSort function.
  • There is a question regarding the handling of cases where array[j].numDays equals array[start].numDays, indicating a potential oversight in the logic of recursive calls.
  • A later reply acknowledges that the author's sort function was the source of the issues with quickSort, expressing gratitude for the assistance received.
  • Participants share sentiments about the satisfaction of debugging and programming, highlighting the challenges and rewards of coding.

Areas of Agreement / Disagreement

While there is some agreement on the challenges of debugging and the satisfaction derived from programming, the technical aspects of the quickSort implementation remain contested, with no consensus on the correctness of the logic presented.

Contextual Notes

Participants note the importance of the swap function's implementation and the handling of specific conditions in the quickSort logic, which remain unresolved in the discussion.

cubanmissile
Messages
5
Reaction score
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
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.
 
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?
 
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)!
 
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.
 
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 :-p
 

Similar threads

  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K