Selection Sort

Let's sort an array from smallest to largest.

Output ...

       
           // JAVASCRIPT

           function _findSmallestIndex(arr) {
               var smallestElement = arr[0];
               var smallestIndex = 0;
           
               for(var i=1 ; i < arr.length ; i++) {
                   if( arr[i] < smallestElement ) {
                       smallestElement = arr[i];
                       smallestIndex = i;
                   }
               }
               return smallestIndex;
           }
           
           function SelectionSort(arr) {
               // Creating a copy of array, because I will remove the smallest one each time and add it into the new array
               var copyArr = arr.slice();
               var sortedArr = [];
           
               for(var i=0 ; i < arr.length ; i++) {
                   var smallestIndex = _findSmallestIndex(copyArr);
                   //Remove element from copyArr at any index (using splice) and add it into sortedArr
                   var element = copyArr.splice(smallestIndex, 1);
                   sortedArr.push(element);
               }
               
               return sortedArr;
           }

           const sourceArray = [5,3,6,2,10];
           const sortedArray = SelectionSort(sourceArray);

           console.log("Source Array: "+sourceArray); //[5,3,6,2,10];
           console.log("Sorted Array: "+sortedArray); //[2,3,5,6,10];

           // PYTHON

           def _findSmallestIndex(arr):
               smallestElement = arr[0]
               smallestIndex = 0

               for i in range(1, len(arr)):
                   if arr[i] < smallestElement:
                       smallestElement = arr[i]
                       smallestIndex = i
               return smallestIndex

           def SelectionSort(arr):
               sortedArr = []
               for i in range(len(arr)):
                   smallestIndex = _findSmallestIndex(arr)
                   element = arr.pop(smallestIndex)
                   sortedArr.append(element)
               return sortedArr

           print( SelectionSort([5,3,6,2,10]) )