Hey everyone, I'm facing a challenge with AngularJS. I need to display the steps for selectionSort and here is my code:
var myApp = angular.module('myApp',[]);
var arraymain = [];
myApp.controller('myController',function($scope) {
$scope.array2 = [];
$scope.selectionSort = function(list) {
n = list.length;
temp2 = list;
for(i = 0; i < n-1; i++) { //need to do n-2 passes
i_min = i;
//finding minimum index
for(j = i+1; j < n; j++){//ith position: elements from i till n-1 candidates
if(temp2[j] < temp2[i_min])
i_min = j; //update the index of minimum element
}
temp = temp2[i];
temp2[i] = temp2[i_min];
temp2[i_min] = temp;
alert(temp); //It shows as needed
$scope.array2.push(temp2); //Here I am having trouble as it saves the sorted final array (the last one) every time the loop runs, but I want to save the current array on each outer loop execution
}
return list;
$scope.selectionSort([3,2,3,4,5,1,2]);
console.log($scope.array2[0]);
console.log($scope.array2[1]);
console.log($scope.array2[2]);
});
Apologies for any language errors.