0

The following code should convert a string into an array of numbers and sort them in descending order. The purpose is to find a substitution for the sort() method.

Something is wrong. If 7 is placed in the first half of the array (like in the example), the code does not work properly. If you swap 7 for a number bigger than the last one (22 in the example), the code will work fine.

I'm looking to get it to work right regardless of the positioning of the numbers.

var row = '92 43 7 119 51 22';
var row = row.split(' ');
var column = row.map(Number);
function arrangeNum(column) {
    for (var i = 0; i <= column.length - 1; i++) {
        for (var j = column.length - i; j >= 0; j--) {
            if (column[j] > column[j - 1]) {
                var temp = column[j];
                column[j] = column[j - 1];
                column[j - 1] = temp;
            }
        }
    }
    return column;
}

console.log(arrangeNum(column));
0

4 Answers 4

1

Something is wrong. If 7 is placed in the first half of the array (like in the example), the code does not work properly.

It is because of your second-for-loop's initialization of j

Replace

for (var j = column.length - i; j >= 0; j--) {

with

for (var j = column.length - 1; j >= i; j--) { 

Notice that j is initialized to column.length - 1, but is only allowed to go as low as i

Demo

function arrangeNum(column) {
  for (var i = 0; i <= column.length - 1; i++) {
    for (var j = column.length - 1; j >= i; j--) {
      if (column[j] > column[j - 1]) {
        [column[j], column[j - 1]] = [column[j - 1], column[j]];
      }
    }
  }
  return column;
}

console.log( arrangeNum( '92 43 7 119 51 22'.split( /\s+/ ).map( Number ) ) );

Sign up to request clarification or add additional context in comments.

1 Comment

This is what I needed to do! Thank you!
1

The problem you are having is that you implemented the bubble sort wrong.

See this line var j = column.length - i; j >= 0; j--, will cause you to ignore the elements of the array starting from the right-side. Since this is a descending order algo, you need to ignore the elements from the left-side, so this: var j = column.length - 1; j >= i; j--.

I added some console logs to show this below: enter image description here

See in yellow your algo ignores the first element on the next loop.

Below I added my full correction (you had some other minor issues):

for (var i = 1; i <= column.length; i++) {
        for (var j = column.length - i; j >= 0; j--) {
            console.log({i,j}, column.map((v,idx) => (idx===j) || (idx===j-1) ? `[${v}]` : v ).join(' '))
            if (column[j] > column[j - 1]) {
                var temp = column[j];
                column[j] = column[j - 1];
                column[j - 1] = temp;
                console.log({i,j}, '  ',column.map((v,idx) => (idx===j) || (idx===j-1) ? `[[${v}]]` : v ).join(' '))
            }
        }
    }

1 Comment

I just got the answer from another poster, it's the same issue as you mentioned. However, I will check the full edit for any other issues. Thank you!
0

Here you go, you just needed to reverse the order of execution of the j loop, starting from 0 till column.length - 1

var row = '92 43 7 119 51 22';
var row  = row.split(' ')
 var column = row.map(Number);
function arrangeNum(column) {
  for (var i = 0; i <= column.length - 1; i++) {
    for (var j = 0; j <= column.length - i; j++) { // SEE THIS
      console.log(column)
       if (column[j] > column[j - 1]) {
       var temp = column[j];
       column[j] = column[j - 1];
       column[j - 1] = temp;
    }
  }
}

return column;
}

console.log(arrangeNum(column))

Comments

0

Pls make change in a code as below. It will work

 var row = '92 43 7 119 51 22';
 var row = row.split(' ');
 var column = row.map(Number);
 function arrangeNum(column) {
  for (var i = 0; i <= column.length - 1; i++) {
    for (var j = column.length - 1; j >= i; j--) {// see this change
        if (column[j] > column[j - 1]) {
          var temp = column[j];
          column[j] = column[j - 1];
          column[j - 1] = temp;
        }
     }
  }
   return column;
}

console.log(arrangeNum(column));

1 Comment

Yes, I got this answer by another user a few minutes ago. You can see it listed with the green check mark. Nevertheless, thank you for taking the time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.