ajeshrkurup
4/4/2017 - 1:30 PM

Selection Sort

Selection Sort

function selectionSort(A) {
	var indexMin = 0;
	var temp = 0;
	console.log(A);
	for(var i=0; i<A.length-1; i++) {
		indexMin = i;
		for(var j=i; j<A.length; j++) {
			
			if(A[j] < A[indexMin]) {
				indexMin = j;
			}
		}
		
		temp = A[i];
		A[i] = A[indexMin];
		A[indexMin] = temp;
		
	}
	
	console.log(A);
}

selectionSort([5,1,2,6,7,3,4]);