So I am using selection sort , the easiest among all, if the item you want to sort is little, let say 10 item, then you can use the below sort, it will loop N x N, so if you have 3 item, it will loop 3x3 = 9, if you have 10 item, it will loop, 10x10 = 100
//ranking say john 80 , peter 70 , you 60,
//so ranking[0] = 80, ranking[1] = 70 , ranking[2] = 60
//rankingname[0] = John, rankingname[1] = Peter, rankingname[2]= you
var ranking = new int [3];
var rankingtemp:int;
var rankingname = new String[3];
var rankingnametemp: String;
//for for loop
var i : int;
var j : int;
var iMin: int;
//number of people in the ranking
var n: int = 3;
function sortranking()
{
//selection sort
for (j = 0; j < n-1; j++)
{
/* find the min element in the unsorted a[j .. n-1] */
/* assume the min is the first element */
iMin = j;
/* test against elements after j to find the smallest */
for ( i = j+1; i < n; i++)
{
/* if this element is less, then it is the new minimum */
if (ranking[i] < ranking[iMin])
{
/* found new minimum; remember its index */
iMin = i;
}
}
if(iMin != j)
{
//swap(ranking[j], ranking[iMin]);
rankingtemp=ranking[j];
ranking[j]=ranking[iMin];
ranking[iMin]=rankingtemp;
rankingnametemp=rankingname[j];
rankingname[j]=rankingname[iMin];
rankingname[iMin]=rankingnametemp;
}
}
}
No comments:
Post a Comment