Hiw sort a score list using the bubble method?
- Juan Carlos
- Posts: 932
- Joined: Mon Sep 06, 2010 1:02 pm
Hiw sort a score list using the bubble method?
I'm working in a game and I need sorting the score list using the bubble method or is there other way programming with Hollywood to sort this list?
Re: Hiw sort a score list using the bubble method?
what is bubble method?
- Juan Carlos
- Posts: 932
- Joined: Mon Sep 06, 2010 1:02 pm
Re: Hiw sort a score list using the bubble method?
When I studied the bubble method or Bubble Sort her eis expalined: http://en.wikipedia.org/wiki/Bubble_sort but I don't know how I can meking with Hollywood, with C there are examples and routines but is different in Hollywood.Bugala wrote:what is bubble method?
Re: Hiw sort a score list using the bubble method?
Ah, i see, lets see. I havent coded for a while, so i might make an error, plus you need to figure out the actual commands as I am just making this as example, not as working code:
This code wont work if you copy apste it to hollywood. This is just the principle of the code and i didnt check the actual commands. Just used what i remembered from head.
If you wnt to increase the performance, you could do so that after each complete list round, it makes the listsize variable become 1 smaller, since after each round, sure thing is that the biggest number is at right place already, hence there is no point in going through the last two numbers compare again.
In small lists it doesnt really matter, but say you have 100 to compare, this means that instead of comparing 100, hundred times = 10 000 compares in total, you could just compare 100, 99, 98, 97... making it be only half = 5 000 compares, hence also halving the time it tooks to compare them.
By the way, you are aware that Hollywood has its own sorting command, which you could use instead?
I think it worked about
newlist = SortList(listtobesorted)
and it will make it from smallest to biggest or something like that.
Code: Select all
list={3, 2, 5, 3, 1}
continue = 0
listsize=getlistsize(list)
repeat
n=0
listorderchanged=0 /* since each time list is gone through and change is made, it tells list order have been changed, it is necessary to make it 0 again to know when no sorting is happening anymore */
repeat
n=n+1
if list[n] > list[n+1]
temp = list[n] /* This part swiches placing of numbers placing bigger one further in the list, making list be from smallest to biggest */
list[n] = list[n+1]
list[n+1] = temp
listorderchanged=1 /* This tells that list order was changed, which means that the list is not completely sorted yet */
endif
if n=listsize-1 then endoflistreached=1
until endoflistreached=1
if listorderchanged = 0 then continue = 0 /* If whole list was gone through and order was not changed a single time, then it means list is sorted in which case you can continue forward.
/* Actually that continue variable is unnecessary, since you could simply use listorderchanged instead,
/* but i used two variables to make it easier to understand the code */
until continue = 1
If you wnt to increase the performance, you could do so that after each complete list round, it makes the listsize variable become 1 smaller, since after each round, sure thing is that the biggest number is at right place already, hence there is no point in going through the last two numbers compare again.
In small lists it doesnt really matter, but say you have 100 to compare, this means that instead of comparing 100, hundred times = 10 000 compares in total, you could just compare 100, 99, 98, 97... making it be only half = 5 000 compares, hence also halving the time it tooks to compare them.
By the way, you are aware that Hollywood has its own sorting command, which you could use instead?
I think it worked about
newlist = SortList(listtobesorted)
and it will make it from smallest to biggest or something like that.
- Juan Carlos
- Posts: 932
- Joined: Mon Sep 06, 2010 1:02 pm
Re: Hiw sort a score list using the bubble method?
Thanks Bugala for you time and ideas, yes now I have saw the command: Sort(array[, sortfunc]), hum I work with this in the score list to try that it works.Bugala wrote:Ah, i see, lets see. I havent coded for a while, so i might make an error, plus you need to figure out the actual commands as I am just making this as example, not as working code:
This code wont work if you copy apste it to hollywood. This is just the principle of the code and i didnt check the actual commands. Just used what i remembered from head.Code: Select all
list={3, 2, 5, 3, 1} continue = 0 listsize=getlistsize(list) repeat n=0 listorderchanged=0 /* since each time list is gone through and change is made, it tells list order have been changed, it is necessary to make it 0 again to know when no sorting is happening anymore */ repeat n=n+1 if list[n] > list[n+1] temp = list[n] /* This part swiches placing of numbers placing bigger one further in the list, making list be from smallest to biggest */ list[n] = list[n+1] list[n+1] = temp listorderchanged=1 /* This tells that list order was changed, which means that the list is not completely sorted yet */ endif if n=listsize-1 then endoflistreached=1 until endoflistreached=1 if listorderchanged = 0 then continue = 0 /* If whole list was gone through and order was not changed a single time, then it means list is sorted in which case you can continue forward. /* Actually that continue variable is unnecessary, since you could simply use listorderchanged instead, /* but i used two variables to make it easier to understand the code */ until continue = 1
If you wnt to increase the performance, you could do so that after each complete list round, it makes the listsize variable become 1 smaller, since after each round, sure thing is that the biggest number is at right place already, hence there is no point in going through the last two numbers compare again.
In small lists it doesnt really matter, but say you have 100 to compare, this means that instead of comparing 100, hundred times = 10 000 compares in total, you could just compare 100, 99, 98, 97... making it be only half = 5 000 compares, hence also halving the time it tooks to compare them.
By the way, you are aware that Hollywood has its own sorting command, which you could use instead?
I think it worked about
newlist = SortList(listtobesorted)
and it will make it from smallest to biggest or something like that.
Again thanks.
- Juan Carlos
- Posts: 932
- Joined: Mon Sep 06, 2010 1:02 pm
Re: Hiw sort a score list using the bubble method?
refine the question, I want sort arrays for statistics for example the arrays like JoeDoe=40, Mary=20, JohnSmith=15, where I can sort the arrays for number but I can sort the numbers but with its names associated like in a score table for a game.
Re: Hiw sort a score list using the bubble method?
You could achieve that by changing couple of lines in my example code.
First of all your list could be something like this:
Then you would change comparison line:
Original:
new version:
otherwise it would work the same.
The reason why the switching part would still work is thanks to hollywoods flexibility. In original version you were switching one dimensional lists (ie. a, b, c, d...) placing, but since Hollywood lets you as well swap lists, you are now in practice having two dimensional list, and changing second dimension lists places in first dimension.
First of all your list could be something like this:
Code: Select all
list={
[1] = {name="johndoe", score=100},
[2] = {name="mary", score=50},
...
}Original:
Code: Select all
if list[n] > list[n+1]
new version:
Code: Select all
if list[n].score > list[n+1].score
otherwise it would work the same.
The reason why the switching part would still work is thanks to hollywoods flexibility. In original version you were switching one dimensional lists (ie. a, b, c, d...) placing, but since Hollywood lets you as well swap lists, you are now in practice having two dimensional list, and changing second dimension lists places in first dimension.
- Juan Carlos
- Posts: 932
- Joined: Mon Sep 06, 2010 1:02 pm
Re: Hiw sort a score list using the bubble method?
Thanks Bugala for your help, I'll try to it, to make a score list.
- Juan Carlos
- Posts: 932
- Joined: Mon Sep 06, 2010 1:02 pm
Re: Hiw sort a score list using the bubble method?
I tested the rotuine and for the moment I no got it runs perfect neither the basic method