Sorting an Array

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
Prophet][
DCEmu Mega Fan
DCEmu Mega Fan
Posts: 2984
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Fri May 31, 2002 6:10 am
Location: Adelaide
Has thanked: 0
Been thanked: 0

Sorting an Array

Post by Prophet][ »

I really have no idea where to begin here, I have a 2d float array and I want to sort the array so that the its in numerical order acording to the last column.

Code: Select all

Football League Ladder
======================

Clubs      Played   Won  Lost Drawn      For  Against          %   Points
==========================================================================
Blues          10     0    10     0      696     1332      52.25        0
Bombers        10     8     2     0     1008      736     136.96       32
Bulldogs       10     2     8     0      829     1251      66.27        8
Cats           10     4     5     1      821      891      92.14       18
Crows          10     6     4     0      888      756     117.46       24
Demons         10     2     8     0      878     1020      86.08        8
Dockers        10     6     4     0      894      941      95.01       24
Eagles         10     4     5     1      917      855     107.25       18
Hawks          10     7     3     0     1004      859     116.88       28
Kangaroos      10     4     6     0      987      987     100.00       16
Lions          10     6     4     0     1065      822     129.56       24
Magpies        10     9     1     0     1068      710     150.42       36
Power          10     9     1     0     1033      817     126.44       36
Saints         10     5     5     0     1008      943     106.89       20
Swans          10     6     4     0      941      867     108.54       24
Tigers         10     1     9     0      781     1031      75.75        4
That is the output, and I want to order the ladder so that its in order of who has the most 'Points'. The team name is not part of the array that holds the values, that is a different array.

I'm just looking for some tips or something to help me get started
User avatar
toastman
Iron Fist of Justice
Iron Fist of Justice
Posts: 4933
Joined: Sat Nov 10, 2001 3:08 am
Location: New Orleans
Has thanked: 0
Been thanked: 0
Contact:

Post by toastman »

Do a search on bubble or quick sorts. That should point you in the right direction.
No signature.
quarn
DC Developer
DC Developer
Posts: 80
Joined: Wed Oct 17, 2001 7:44 pm
Location: Sweden
Has thanked: 0
Been thanked: 1 time

Post by quarn »

BlackAura
DC Developer
DC Developer
Posts: 9951
Joined: Sun Dec 30, 2001 9:02 am
Has thanked: 0
Been thanked: 1 time

Post by BlackAura »

Assuming you're using C and aren't doing this for an assignment (and thus need to write code to do the sorting), you could use the qsort function from the standard C library. If you're using Java or pretty much any other language, there should be some way to sort arrays fairly easily.

Otherwise, a selection sort is pretty simple to implement. For each element in the array, find the item which should be there, and swap it with the item that actually is there.
User avatar
Prophet][
DCEmu Mega Fan
DCEmu Mega Fan
Posts: 2984
Joined: Fri May 31, 2002 6:10 am
Location: Adelaide
Has thanked: 0
Been thanked: 0

Post by Prophet][ »

I'm using C, and it is for an assignment. I know Java and have done this in java before but I just couldn't get my head around doing it in C. The link quarn gave me was quite good though and it mentions selection sort.
Post Reply