Thread: Quick Sort

  1. #1
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32

    RESOLVED - Quick Sort

    Hi all,

    I have a quick sort function which i'm working on however, I am having problems with it. It seems to get stuck in a loop...when ever i break the program its on my comparison function. I know this function works as i use it in a bubble sort too.

    Even with only 10 records to sort it never seem to finnish sorting and out any results.

    thanks for any help in the matter, my code is as follows.

    Code:
    void quickSort( float * dataArray, size_t first, size_t last, int ( *ptrComp ) ( const void * a, const void * b ), void ( *ptrSwap ) ( const void * a, const void * b ) ) {
    	size_t i, j;
    	int x, temp;
    	
    	i = first;
    	j = last;
    	x = dataArray[(first + last) / 2];
    	
    	// Check That There Are At Least Two Elements To Sort
    	if ( first < last ) {
    		// While the to partition indexes are apart
    		do {
    			// From the left, look for the first
    			// element grater than the pivot
    			while (dataArray[i] < x && i < last) {
    				++i;
    			}
    			
    			// from the right, look for the first
    			// element less than the pivot
    			while (dataArray[j] > x && j > first) {
    				--j;
    			}
    			
    			// Compare data and swap if needed
    			if ( ( *ptrComp ) ( &dataArray[i], &dataArray[j] ) == -1 )	{
    				( *ptrSwap ) ( &dataArray[i], &dataArray[j] );
    				++i;
    				--j;
    			}
    		} while (i <= j);
    		
    		// Quicksort the left partition
    		if (first < j) {
    			quickSort( dataArray, first, j, ptrComp, ptrSwap );
    		}
    		
    		// Quicksort the right partition
    		if (i < last) {
    			quickSort( dataArray, i, last, ptrComp, ptrSwap );
    		}
    	}
    }
    Last edited by chriscolden; 06-06-2006 at 06:06 PM.
    I'm a noob when it comes to C and I have to use Borland C++ 3.1 because my university won't move on.

    However I do use Turbo C++ 3.1 for windows. Makes life abit less stressful.

  2. #2
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    hey this has now been resolved. thanks
    I'm a noob when it comes to C and I have to use Borland C++ 3.1 because my university won't move on.

    However I do use Turbo C++ 3.1 for windows. Makes life abit less stressful.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    we did something?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >we did something?
    Actively waiting for problems to go away is usually a viable solution. This is especially true when other people are more interested in getting the problem fixed than you are.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  2. Help On A Quick Sort Program
    By nick4 in forum C++ Programming
    Replies: 11
    Last Post: 12-06-2004, 10:51 AM
  3. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  4. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM