qsort example please!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Transmit
    New Member
    • Dec 2006
    • 11

    #1

    qsort example please!

    Hello!

    Can someone give me a really simple example of qsort?
    I just need it to sort an array that is 10 integers big.

    I tried this:
    http://www.phim.unibe. ch/comp_doc/c_manual/C/EXAMPLES/qsort1.c

    but it gives me an error here at the qsort part.


    Thanks in advance!!!
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by Transmit
    Hello!

    Can someone give me a really simple example of qsort?
    I just need it to sort an array that is 10 integers big.

    I tried this:
    http://www.phim.unibe. ch/comp_doc/c_manual/C/EXAMPLES/qsort1.c

    but it gives me an error here at the qsort part.


    Thanks in advance!!!
    Hi. Asking for a really simple quicksort is unlikely to get the response you are seeking. Quick sort is a recursive function so it is not simple. This is the version that I use:

    Code:
    void SortArray(int HiVal, int LowVal, int *arSort)
    {
    	int TempLow = 0;
    	int TempHi = 0;
    	int pivot = 0;
    	int tmp = 0;
    	
    	TempLow = LowVal;
    	TempHi = HiVal;
    
    	pivot = arSort[(HiVal - LowVal) / 2];
    
    	while (TempLow <= TempHi) {
    		while (arSort[TempLow] > pivot && TempLow < HiVal) {
    			TempLow++;
    		}
    		while (arSort[TempHi] < pivot && TempHi > LowVal) {
    			TempHi--;
    		}
    		if (TempLow <= TempHi) {
    			//swap
    			tmp = arSort[TempLow];
    			arSort[TempLow] = arSort[TempHi];
    			arSort[TempHi] = tmpweight;
    			TempLow++;
    			TempHi--;
    		}
    	}
    
    	if (LowVal < TempHi)
    		SortRatings(TempHi, LowVal, arSort);
    
    	if (TempLow < HiVal)
    		SortRatings(HiVal, TempLow, arSort);
    }
    It will sort in descending order.
    call it like this:

    Code:
    SortArray(arraysize - 1, 0, myintarray);
    Good luck

    Comment

    • willakawill
      Top Contributor
      • Oct 2006
      • 1646

      #3
      Oops. I converted this code for you from an app I am working on and it was used to sort an array of struct. I left some of the code in there above. This is the same thing adjusted for an array of int.
      Code:
      void SortArray(int HiVal, int LowVal, int *arSort)
      {
      	int TempLow = 0;
      	int TempHi = 0;
      	int pivot = 0;
      	int tmp = 0;
      	
      	TempLow = LowVal;
      	TempHi = HiVal;
      
      	pivot = arSort[(HiVal - LowVal) / 2];
      
      	while (TempLow <= TempHi) {
      		while (arSort[TempLow] > pivot && TempLow < HiVal) {
      			TempLow++;
      		}
      		while (arSort[TempHi] < pivot && TempHi > LowVal) {
      			TempHi--;
      		}
      		if (TempLow <= TempHi) {
      			//swap
      			tmp = arSort[TempLow];
      			arSort[TempLow] = arSort[TempHi];
      			arSort[TempHi] = tmpweight;
      			TempLow++;
      			TempHi--;
      		}
      	}
      
      	if (LowVal < TempHi)
      		SortArray(TempHi, LowVal, arSort);
      
      	if (TempLow < HiVal)
      		SortArray(HiVal, TempLow, arSort);
      }

      Comment

      Working...