Thursday, October 9, 2014

Unity easiest sort algorithm

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] = 80ranking[1] = 70 , ranking[2] = 60
//rankingname[0] = Johnrankingname[1] = Peterrankingname[2]= you
var ranking = new int [3];
var rankingtemp:int;

var rankingname = new String[3];
var rankingnametempString;

//for for loop
var i : int;
var j : int;
var iMinint;
//number of people in the ranking
var nint = 3;


function sortranking()
{

    //selection sort
    
    for (j = 0j < n-1j++) 
    {
    /* 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+1i < ni++) 
        {
            /* if this element is lessthen it is the new minimum */  
            if (ranking[i] < ranking[iMin]) 
            {
                /* found new minimumremember 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