473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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!!!
Dec 31 '06 #1
2 2896
willakawill
1,646 1GB
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
willakawill
1,646 1GB
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

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

Similar topics

11
by: William Buch | last post by:
I have a strange problem. The code isn't written by me, but uses the qsort function in stdlib. ALWAYS, the fourth time through, the memory location of variable list (i.e. mem location = 41813698)...
5
by: Steve | last post by:
can someone tell me how qsort function in <stdlib.h> is used (qsort(..........))? the function has a buffer, two void * parameters and the a pointer to a compare function. Thanks.
7
by: Excluded_Middle | last post by:
Suppose I have a struct typdef struct foo { int age; char *name; }foo; now I made a list of foo using
32
by: John Smith | last post by:
I'm trying to figure out qsort(). I haven't seen any practical examples, only synopsis. In the code below, the array is not sorted. Can someone give me some help? #include <stdio.h> #include...
16
by: t_pantel | last post by:
I 've got the following structure: typedef struct GROUPED { short val ; short code; short group; short forecast_cd; short double_ind; short min;
3
by: No Such Luck | last post by:
Hi All: The code below (using the qsort function) produces the following incorrect result. The last two numbers are not sorted. It this innaccurate result specific to my compiler's qsort, or is...
18
by: bsder | last post by:
Hi, Can anyone please tell me how to calculate the size of the following 4-dimensional array, and now to use qsort for sorting on this array? double sp = { 4.0, 5.0, 6.0 }; double spa = { {...
17
by: arne.muller | last post by:
Hello, I just had to find out that a program that runs fine under linux and True64 alpha crashed with a segmentation fault under SunOS 5.8. I drilled down and it appears that if the ...
10
by: gauss010 | last post by:
Suppose I have an object A of type char. Each A is a buffer containing a string, and I want to sort the M strings of A using the strcmp function. The description of the qsort function says that I...
61
by: Ron Ford | last post by:
K&R has three different versions of qsort, and the ultimate one is supposed to be like the one in the std library. I'm trying to implement the first, which is in §4.10. I think I'm pretty close...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.