![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
Sorting arrays
Howdy everyone.
I need some help in sorting an array, the example in my textbook is just not up to par. Could someone write a simple short program in C (not C++) that sorts the elements of an array? Just put integers in the array, it doesn't matter how many. And either from highest to lowest or lowest to highest. I don't care. I would appreciate this a whole lot. Thanks to those who help. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
Does "not up to par" mean, that your instructur/teacher/professor has requested that you write your own array sort function and that they mentioned that the students who use the one in the book will get a 0?
What have you tried so far?
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Feb 2005
Posts: 89
Rep Power: 4
![]() |
i started writing it, but you'll have to do your own homework. it's really easy, anyway. here's pseudocode
1) fill your array 2) make a temporary holder for an element 3) compare the first and second elements 4) if a is less than b, do nothing. 5) if b is less than a, copy b to temp, copy a to b, copy temp to a, in that order. basically, you're swapping the two values. 6) compare second and third elements, test as above, then 3rd and 4th, etc. 7) start again. do once for each element in the list. ie, a 100-element array needs 100 iterations. this test only assures that the last element is the lowest. technically, it's a bubble down sort, i guess. there are LOTS of ways to optimize this, but this is a basic sort routine. good luck. |
|
|
|
|
|
#4 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
http://programmingforums.org/forum/s...ead.php?t=1644
Check the last short program I wrote in the first post.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|