Skip to main content
deleted 5 characters in body
Source Link
rdllopes
  • 868
  • 4
  • 15

Yes, it could be simpler.

  1. Special case for first value is not needed.
  2. Some variables could be removed.
  3. All variables initialization could be removed.

Below, you can see your same code reviewed.

    function selectionSort(inputArray) {
      for (var i = 0; i < (inputArray.length - 1); i++) {
         var minNum = inputArray[i];
         var swapElementIndex = i;
         for (var currentIndex = i + 1; currentIndex < inputArray.length; currentIndex++) {    
            if (inputArray[currentIndex] < minNum) {
                swapElementIndex = currentIndex;
                minNum = inputArray[swapElementIndex];
            }    
        }
        if (i != swapElementIndex) {
            var temp = inputArray[i];
            inputArray[i] = inputArray[swapElementIndex];
            inputArray[swapElementIndex] = temp;
        }
     }
     return inputArray;
    }

Yes, it could be simpler.

  1. Special case for first value is not needed.
  2. Some variables could be removed.
  3. All variables initialization could be removed.

Below, you can see your same code reviewed.

    function selectionSort(inputArray) {
      for (var i = 0; i < (inputArray.length - 1); i++) {
         var minNum = inputArray[i];
         var swapElementIndex = i;
         for (var currentIndex = i + 1; currentIndex < inputArray.length; currentIndex++) {    
            if (inputArray[currentIndex] < minNum) {
                swapElementIndex = currentIndex;
                minNum = inputArray[swapElementIndex];
            }    
        }
        if (i != swapElementIndex) {
            var temp = inputArray[i];
            inputArray[i] = inputArray[swapElementIndex];
            inputArray[swapElementIndex] = temp;
        }
     }
     return inputArray;
    }

Yes, it could be simpler.

  1. Special case for first value is not needed.
  2. Some variables could be removed.
  3. All variables initialization could be removed.

Below, you can see your code reviewed.

    function selectionSort(inputArray) {
      for (var i = 0; i < (inputArray.length - 1); i++) {
         var minNum = inputArray[i];
         var swapElementIndex = i;
         for (var currentIndex = i + 1; currentIndex < inputArray.length; currentIndex++) {    
            if (inputArray[currentIndex] < minNum) {
                swapElementIndex = currentIndex;
                minNum = inputArray[swapElementIndex];
            }    
        }
        if (i != swapElementIndex) {
            var temp = inputArray[i];
            inputArray[i] = inputArray[swapElementIndex];
            inputArray[swapElementIndex] = temp;
        }
     }
     return inputArray;
    }
Source Link
rdllopes
  • 868
  • 4
  • 15

Yes, it could be simpler.

  1. Special case for first value is not needed.
  2. Some variables could be removed.
  3. All variables initialization could be removed.

Below, you can see your same code reviewed.

    function selectionSort(inputArray) {
      for (var i = 0; i < (inputArray.length - 1); i++) {
         var minNum = inputArray[i];
         var swapElementIndex = i;
         for (var currentIndex = i + 1; currentIndex < inputArray.length; currentIndex++) {    
            if (inputArray[currentIndex] < minNum) {
                swapElementIndex = currentIndex;
                minNum = inputArray[swapElementIndex];
            }    
        }
        if (i != swapElementIndex) {
            var temp = inputArray[i];
            inputArray[i] = inputArray[swapElementIndex];
            inputArray[swapElementIndex] = temp;
        }
     }
     return inputArray;
    }