Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 4th, 2005, 1:54 PM   #1
mmmm_strawberries
Newbie
 
Join Date: Feb 2005
Posts: 10
Rep Power: 0 mmmm_strawberries is on a distinguished road
Send a message via AIM to mmmm_strawberries
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.
mmmm_strawberries is offline   Reply With Quote
Old Mar 4th, 2005, 2:18 PM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Mar 4th, 2005, 2:27 PM   #3
brkstf
Programmer
 
brkstf's Avatar
 
Join Date: Feb 2005
Posts: 89
Rep Power: 4 brkstf is on a distinguished road
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.
brkstf is offline   Reply With Quote
Old Mar 4th, 2005, 3:20 PM   #4
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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
Mjordan2nd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:57 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC