Javascript: selection sort implementation

Selection sort has an outer loop and an inner loop. The outer loop starts from the first element to the second element to the end, because when it gets to the element right before the last element, the last element should be in its correct position. The inner loop starts from the current index+1 of the outer loop to the end of the list. When the inner loop finishes, it will swap the current index of the outer loop with the next minimum(for ascending) or maximum(for descending) value.
Here is an example of selection sort in Javascript

function selectionSort(data)
{
	var n=data.length;
	var nextSwap;		//the index of next min value or max value
	var temp;

	for(var i=0; i<n-1; i++)
	{

		nextSwap=i;
		for(var j=i+1; j<n; j++)
		{
			if( data[j]<data[nextSwap] ) //change the < to > for descending order
			{
				nextSwap=j; 
			}
		}
		temp=data[i];
		data[i]=data[nextSwap];
		data[nextSwap]=temp;

	}
	return data;
}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search