By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,432 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,432 IT Pros & Developers. It's quick & easy.

qsort example please!

P: 11
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!!!
Dec 31 '06 #1
Share this Question
Share on Google+
2 Replies


100+
P: 1,646
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:

Expand|Select|Wrap|Line Numbers
  1. void SortArray(int HiVal, int LowVal, int *arSort)
  2. {
  3.     int TempLow = 0;
  4.     int TempHi = 0;
  5.     int pivot = 0;
  6.     int tmp = 0;
  7.  
  8.     TempLow = LowVal;
  9.     TempHi = HiVal;
  10.  
  11.     pivot = arSort[(HiVal - LowVal) / 2];
  12.  
  13.     while (TempLow <= TempHi) {
  14.         while (arSort[TempLow] > pivot && TempLow < HiVal) {
  15.             TempLow++;
  16.         }
  17.         while (arSort[TempHi] < pivot && TempHi > LowVal) {
  18.             TempHi--;
  19.         }
  20.         if (TempLow <= TempHi) {
  21.             //swap
  22.             tmp = arSort[TempLow];
  23.             arSort[TempLow] = arSort[TempHi];
  24.             arSort[TempHi] = tmpweight;
  25.             TempLow++;
  26.             TempHi--;
  27.         }
  28.     }
  29.  
  30.     if (LowVal < TempHi)
  31.         SortRatings(TempHi, LowVal, arSort);
  32.  
  33.     if (TempLow < HiVal)
  34.         SortRatings(HiVal, TempLow, arSort);
  35. }
It will sort in descending order.
call it like this:

Expand|Select|Wrap|Line Numbers
  1. SortArray(arraysize - 1, 0, myintarray);
Good luck
Dec 31 '06 #2

100+
P: 1,646
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.
Expand|Select|Wrap|Line Numbers
  1. void SortArray(int HiVal, int LowVal, int *arSort)
  2. {
  3.     int TempLow = 0;
  4.     int TempHi = 0;
  5.     int pivot = 0;
  6.     int tmp = 0;
  7.  
  8.     TempLow = LowVal;
  9.     TempHi = HiVal;
  10.  
  11.     pivot = arSort[(HiVal - LowVal) / 2];
  12.  
  13.     while (TempLow <= TempHi) {
  14.         while (arSort[TempLow] > pivot && TempLow < HiVal) {
  15.             TempLow++;
  16.         }
  17.         while (arSort[TempHi] < pivot && TempHi > LowVal) {
  18.             TempHi--;
  19.         }
  20.         if (TempLow <= TempHi) {
  21.             //swap
  22.             tmp = arSort[TempLow];
  23.             arSort[TempLow] = arSort[TempHi];
  24.             arSort[TempHi] = tmpweight;
  25.             TempLow++;
  26.             TempHi--;
  27.         }
  28.     }
  29.  
  30.     if (LowVal < TempHi)
  31.         SortArray(TempHi, LowVal, arSort);
  32.  
  33.     if (TempLow < HiVal)
  34.         SortArray(HiVal, TempLow, arSort);
  35. }
Jan 1 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.