Bubblesort on two values
Blitz3D Forums/Blitz3D Beginners Area/Bubblesort on two values
| ||
Hello, I'm working on my first project and I have a problem with my highscore tables. The game is a remake of space taxi where players have to pick up and deliver clients. The score is the number of clients delivered. But players can choose the amount of playing time (60, 120, 180 seconds). So I need to incorporate the time in the table, because obviously delivering 5 clients in 60 seconds is better then 5 clients in 180 seconds. Sofar I can only sort on the score but for equal scores I would need second sort on the time, ie: rank name clients time 1. aaa 5 60 2. bbb 5 120 3. ccc 5 180 4. ddd 3 120 5. eee 3 180 etc. I hope I'm clear enough and that someone can help :) Here is the function I'm using: Function SortHighscores(sPlayer$,iScore%,iOption%) For i = 0 To iTableSize% - 1 If iScore% >= HSClients(i) sTempName$ = HSName$(i) iTempScore% = HSClients(i) iTempOption% = HSTime(i) HSName$(i) = sPlayer$ HSClients(i) = iScore% HSTime(i) = iOption% sPlayer$ = sTempName$ iScore% = iTempScore% iOption% = iTempOption% End If Next End Function Thanks! Klaus |
| ||
Write a second routine to sort into 'increasing time' order. Do this sort first, then do your score sort. The result is the order you want. Another approach is to modify the comparison in your current sort. Two elements are 'out of order and need to be swapped' if the scores are in decreasing order OR they are equal and the times are increasing. |
| ||
First, some comments on sort techniques: There are several methods for sorting, and the Bubble is one of the easiest, but not one of the fastest. For game scores, with just a few players, sort soeed on scores is not important, But sorting is the way we organize and priorize information, so it is important to find a good method, As a rule, the QuickSort method is the best compromise between speed and retention of prior sort orders -- that is, if your first sort is on the First Name field, then you sort on the Last name field, the results will retain a Last Name,First Name sequence. Some other sort techniques will disturb the First Name sort order while doing the Last Name sort, so they are generally less useful when considering primary-secondary sort orders. I would recommend looking for examples of the QuickSORT algorythm, of which at least one has appeared on these forums. Also check to see if the language itself includes a Sort capability - several languages now include this, as it is such a universal way for handling structured data. |