skip to Main Content

I am using windows 10, Visual Studio Code program. I have written bubble sort function in JS but my array is not included in it.I think I called the function incorrectly. Below I show my code, I will be glad if you can tell me what I did wrong, thanks in advance.

function sorting(arr) 
{
    for (let i = arr.length - 1; i > 0; i--) 
    {
      for (let i = 0; i < j; i++) 
      {
        if (arr[i] > arr[i + 1]) 
        {
          let temp = arr[i];
          arr[i] = arr[i + 1];
          arr[i + 1] = temp;
        }
      }
    }
  }

  function aftersort(arr)
  {
    let = [2, 5, 7, 65, 56, 34]; 
    sorting(arr);
  }

  console.log(arr);
  

My codes screenshot

I tried to give numbers to Array and then used the name of this Array in the function, I got an error that my Array is undefined, then I tried to call the function but nothing has changed

2

Answers


  1. You didn’t call your aftersort function. You have a syntax error: let = [2, 5, 7, 65, 56, 34];. j is never declared

    function sorting(arr) {
      for (let i = arr.length - 1; i > 0; i--) {
        for (let j = 0; j < i; j++) {
          if (arr[i] > arr[i + 1]) {
            let temp = arr[i];
            arr[i] = arr[i + 1];
            arr[i + 1] = temp;
          }
        }
      }
    }
    
    
    let arr = [2, 5, 7, 65, 56, 34];
    sorting(arr);
    
    console.log(arr);

    And it still doesn’t work

    Login or Signup to reply.
  2. You had a few issues.

    1. The aftersort function is not needed
    2. You did not assign arr a value
    3. You messed up the inner-loop i.e. j

    Also, your swap was not working, so I fixed i and j and updated the condition. You need to swap with respect to j, not i.

    function sorting(arr) {
      for (let i = 0; i < arr.length; i++) {
        for (let j = 0; j < (arr.length - i - 1); j++) { // Assign j, and increment it
          // Corrected swap
          if (arr[j] > arr[j + 1]) {
            let temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
          }
        }
      }
    }
    
    let arr = [2, 5, 7, 65, 56, 34]; // Need to assign `arr`
    sorting(arr); // Sort the array
    console.log(...arr); // Print the modified array :- [2, 5, 7, 34, 56, 65]

    You could rewrite this to make is more readable. You could also make the sorted result immutable, by making a copy.

    const DEFAULT_COMPARATOR = function(a, b) { return a - b; } 
    
    function BubbleSort(source, comparator = DEFAULT_COMPARATOR) {
      return execute();
      
      function execute() {
        const copy = structuredClone(source);
        sort(copy);
        return copy;
      }
      
      function sort(ref) {
        let i, j, swapped;
        for (i = 0; i < ref.length; i++) {
          swapped = false;
          for (j = 0; j < (ref.length - i - 1); j++) {
            if (comparator(ref[j], ref[j + 1]) > 0) {
              swapped = swap(ref, j, j + 1);
            }
          }
          if (!swapped) break;
        }
      }
      
      function swap(ref, leftIndex, rightIndex) {
        const temp = ref[leftIndex];
        ref[leftIndex] = ref[rightIndex];
        ref[rightIndex] = temp;
        return true;
      }
    }
    
    const original = [7, 65, 5, 56, 2, 34];
    const sorted   = BubbleSort(original);
    
    console.log(...original); // [7, 65, 5, 56, 2, 34]
    console.log(...sorted);   // [2, 5, 7, 34, 56, 65]
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search