Good for selection sorting?

  • 0
    function selectSort(arr) {
      for (let i = 0; i < arr.length; i++) {
        let min = i;
    
        for (let k = i + 1; k < arr.length; k++) {
          if (arr[min] > arr[k]) {
            console.log([arr[min], arr[k]] = [arr[k], arr[min]]);
            
          }
        }
      }
      return arr;
    }
    JavaScript Collin Hull, May 17, 2020

  • 1 Answers
  • 0
    It won't do. If implemented correctly, the search for the minimum goes without rearranging the elements. The permutation, if needed, is done once after the search.

    Your worst-case implementation (the array is already sorted in reverse order) will do (N-1) * N / 2 permutations, while the classic implementation will only do N-1.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!