-2

I am trying to do a bubblesort. I am using this algorithm to sort a 2d array and I keep getting an error. Here is the function:

var array = [
    [ "Rober Hill"         , 123.54  ],
    [ "Chrsitopher Reddkin", 54.67   ],
    [ "Maggie Woods"       , 1000.87 ],
    [ "Jennifer Jones"     , 3.34    ],
    [ "Marcus Parker"      , 64.98   ]
];
table = document.getElementById("table");

function bubbleSort(array, length, element) 
{
    var swapped = false;
    do 
    {
        for (var a = 0; a < 5; a++) // Line 59
        {
            if (array[a][1] > array[a+1][1])
            {
                var temp = array[a][1];
                array[a][1] = array[a+1][1];
                array[a+1][1] = temp; 
                swapped = true; 
            }
        }
    } while(swapped);

    return array;

}

The error says: Sorting.html:59 Uncaught TypeError: Cannot read property '0' of undefined. I have this function on a button. Any kind of help would be nice! Thank you

Thum Choon Tat
  • 2,866
  • 1
  • 20
  • 22
  • Which is line 59? – Thum Choon Tat Nov 26 '18 at 03:31
  • line 59: for (var a = 0; a < 5; a++) – Edrian Yumang Nov 26 '18 at 03:37
  • 1
    Possible for you to create a working demo for myself and others to work with? – NewToJS Nov 26 '18 at 03:41
  • 1
    Maybe this [**JsFiddle Demo**](https://jsfiddle.net/New_To_JS/Lx1fun0b/) might be of some help. I don't know what you want to order them by but credit to [**This Answer**](https://stackoverflow.com/questions/7502489/bubble-sort-algorithm-javascript/#answer-37901176) for the function source code used in my example. – NewToJS Nov 26 '18 at 03:59
  • I doubt very much that is line 59. The error messages in the console are typically clickable and will take you directly to the source code. – Phil Nov 26 '18 at 04:58
  • Also, how **exactly** are you calling `bubbleSort()`? What arguments are you providing for the `array`, `length` and `element` parameters? – Phil Nov 26 '18 at 05:00

2 Answers2

1

Running your code exactly as it is, I get:

Cannot read property '1' of undefined

This is because in your comparison, you're attempting to compare array[a][1] > array[a+1][1], and this works except for the last loop, where array[a+1] doesn't exist, and because it doesn't exist, 1 is undefined.

Here's a working solution, with a few notable differences.

  1. I don't know why you had length, and element as parameters for your bubbleSort, but they're gone now
  2. You can just use the array length in your for loop, that way if you add more items to your array, you don't have to update the 5 you had hardcoded.
  3. In the for loop, I minus 1 from the array length so we're never trying to compare the last item against an item after it that doesn't exist.
  4. Also, I used i instead of a, since that's a common variable name for an incrementer.
  5. I defined swapped inside the do...while, otherwise you'll have created an infinite loop because swapped will get set to true on the first pass and stay true forever.
  6. You don't have to return the array, as it is modifying the original in place when you call bubbleSort

var array = [
  ["Rober Hill", 123.54],
  ["Chrsitopher Reddkin", 54.67],
  ["Maggie Woods", 1000.87],
  ["Jennifer Jones", 3.34],
  ["Marcus Parker", 64.98]
];

function bubbleSort(array) {
  do {
    var swapped = false;

    for (var i = 0; i < array.length - 1; i++) {
      if (array[i][1] > array[i + 1][1]) {
        var temp = array[i];

        array[i] = array[i + 1];
        array[i + 1] = temp;

        swapped = true;
      }
    }
  } while (swapped);
}

bubbleSort(array)
console.log(array);
AnonymousSB
  • 3,270
  • 9
  • 27
0

Move var swapped = false; inside do {....

Also update condition in for it should be like a < 4; or it's better if you use generalized condition as a < array.length - 1;

var array = [
  ["Rober Hill", 123.54],
  ["Chrsitopher Reddkin", 54.67],
  ["Maggie Woods", 1000.87],
  ["Jennifer Jones", 3.34],
  ["Marcus Parker", 64.98]
];

function bubbleSort(array, length, element) {
  do {
    var swapped = false;
    for (var a = 0; a < array.length - 1; a++) // Line 59
    {
      if (array[a][1] > array[a + 1][1]) {
        var temp = array[a][1];
        array[a][1] = array[a + 1][1];
        array[a + 1][1] = temp;
        swapped = true;
      }
    }
  } while (swapped);

  return array;

}

console.log(bubbleSort(array));
Karan
  • 11,176
  • 3
  • 24
  • 37