Results 1 to 5 of 5
Say I have an array (num[3]) that holds 3 numbers. How could I sort those numbers in order from highest to lowest and store them in individual integer variables (highest, ...
- 03-14-2008 #1Just Joined!
- Join Date
- Feb 2008
- Posts
- 72
sorting an array of numbers in C
Say I have an array (num[3]) that holds 3 numbers. How could I sort those numbers in order from highest to lowest and store them in individual integer variables (highest, middle, lowest) using C? For example:
And sort it so that highest = 9, middle = 5, and lowest = 4.Code:int num1 = 5; int num2 = 4; int num3 = 9; int num[3]; int highest, middle, lowest; num[1] = num1; num[2] = num2; num[3] = num3;
- 03-14-2008 #2
So a few questions:
1) Do you mean to have int pointers, or ints? That is, do you want highest, middle, and lowest to point at the correct spots in the array, or actually hold copies of the numbers?
2) Do you definitely need both the array sorted and the variables holding the numbers?
3) Are there always 3 numbers, or are there more?
The simplest solution would be to run a sorting algorithm on the array. Quicksort is the fastest of these, and though with 3 numbers it doesn't really matter which you choose, it's a good algorithm to know. Once you've run quicksort, you will have a sorted array, so num[0] is the lowest, num[1] is the middle, and num[2] the highest.DISTRO=Arch
Registered Linux User #388732
- 03-14-2008 #3Just Joined!
- Join Date
- Feb 2008
- Posts
- 72
I guess it doesn't really matter whether I use pointers or actual ints. There are two arrays I need sorting, one will always have 3 numbers, the other will always have 2. I just need a way to refer to the highest, middle, and lowest numbers, whether it be resorting the array or putting them into variables it doesn't matter.
Could you give me an example on how to do that?Quicksort is the fastest of these, and though with 3 numbers it doesn't really matter which you choose, it's a good algorithm to know.
- 03-15-2008 #4Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
There is a good comparison (pun intended) article at Sorting algorithm - Wikipedia, the free encyclopedia including references.
Given the small n, I would do a simple bubble sort for the n=3 case and a simple comparison and possible swap for the n=2 case. The bubble sort is the least efficient sort, but the code is very short and avoids the startup cost of more efficient (but more complex) algorithms like quicksort. My next choice would be insertion sort -- Jon Bentley describes it in 5 lines of code. He also has looked at difficulty of coding. In a small study he did only 10% of professional programmers got a binary search code correct when writing from scratch (Programming Pearls, 2nd edition, page 33 ff), so I lean towards simplicity.
Best wishes ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 03-18-2008 #5Just Joined!
- Join Date
- Feb 2008
- Posts
- 72
Ahh thanks guys! The bubble sort does just what I need. I'll learn Quicksort someday when I need it. Thanks again!


Reply With Quote