Bubblesort problem

Bubblesort problem
0

#1

I am trying to make a PEN that takes names and ratings, sorts the 2 arrays based on the ratings, using Bubblesort and displays the sorted arrays. But it doesn’t seem to work for numbers greater or equal than 10 (tried 9.99 and it works but not with 10.001 or 10). Here’s the code:

for (var i = numberOfCountries -1; i>=0; i-- ){
  for (var j = 0; j <= i; j++){
    if (ratings[j-1] < ratings[j]){
      var temp = ratings[j-1];
           ratings[j-1] = ratings[j];
           ratings[j] = temp;
      temp = countries[j-1];
      countries[j-1] = countries[j];
      countries[j] = temp;
    }
  }
}

Any suggestions ?


#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

Can you post the actual arrays that did not work for you?


#4

That was the output:

Place #1 i9 with rating 9
Place #2 i8 with rating 8
Place #3 i7 with rating 7
Place #4 i5 with rating 5
Place #5 i10 with rating 10


#5

What does the ratings array and the counties array look like?


#6

I inputted in this order :
Country Rating
i5 5
i10 10
i9 9
i7 7
i8 8


#7

When I run the following code:

var countries = ['i5', 'i10', 'i9', 'i7', 'i8'];
var ratings = [5, 10, 9, 7, 8];
numberOfCountries = 5;
for (var i = numberOfCountries -1; i>=0; i-- ){
  for (var j = 0; j <= i; j++){
    if (ratings[j-1] < ratings[j]){
      var temp = ratings[j-1];
      ratings[j-1] = ratings[j];
      ratings[j] = temp;
      temp = countries[j-1];
      countries[j-1] = countries[j];
      countries[j] = temp;
    }
  }
}
console.log(countries);
console.log(ratings);

I get the following displayed to the console. The arrays are sorted in descending order based on rating, so your sort appears to work.

[ ‘i10’, ‘i9’, ‘i8’, ‘i7’, ‘i5’ ]
[ 10, 9, 8, 7, 5 ]


#8

Is it a CodePen problem then?


#9

Can share your Codepen link? I can check your code on there. It should run fine in Codepen.


#10

#11

There is nothing wrong with your sort. The problem is in the ratings array. Put a console.log statement for the ratings array after the for loop of prompts and you will see that ratings is an array of string items. It needs to be an array of numeric items.


#12

So what do I do to fix the problem? And why doesn’t it work for numbers >=10? It’s so weird…


#13

As @randelldawson said, you’re comparing STRINGS instead of NUMBERS (int, or float).

If you’re comparing strings, “9” < “10” will be false.
While 9 < 10 will be true

For strings, it will be sorted like this
"10"
"11"
"12"
"13"
"2"
"3"
"4"
"5"
“6”

If your array is an integer or float, then it will be sorted like this
2
3
4
5
6
10
11
12
13

Try doing a parseFloat() of the comparison, so you’re comparing the numeric value of your number, instead of the string value.


#14

When you push the rating into the ratings array, you could use the function @owel mentioned to convert the string to a number before pushing.