I have this following algorithm to alphabetically list the items inside a "Select" object. The select has 50 items. This algorithm takes like 5-10 second to complete on a 300 Mhz computer and around 2-3 seconds on a 900 Mhz computer. It's still a very inefficient code when taking into account that i'm only sorting 50 items alphabetically.
So, my question is, can you guys propose me a more efficient sorting algorithm than the one i'm using below?
(I believe i'm using bubblesort ) There's got to be an algorithm that will sort faster and waste less CPU.
Thanks in advance
So, my question is, can you guys propose me a more efficient sorting algorithm than the one i'm using below?
(I believe i'm using bubblesort ) There's got to be an algorithm that will sort faster and waste less CPU.
Thanks in advance
Code:
function sortItems()
{
document.status.info2.value = "Sorting... please wait.";
document.status.inventory.selectedIndex = 0;
var tempitem = "";
for (var i = 49; i >= 0; i--) {
for ( var j = 49; j >= i; j-- ) {
if ( status.inventory.options[i].text > status.inventory.options[j].text && status.inventory.options[j].text != "" )
{
tempitem = status.inventory.options[i].text;
status.inventory.options[i].text = status.inventory.options[j].text;
status.inventory.options[j].text = tempitem;
}
if ( status.inventory.options[i].text == "" )
{
status.inventory.options[i].text = status.inventory.options[j].text;
status.inventory.options[j].text = "";
}
}
}
document.status.info2.value = "Inventory sorted";
}