Numerical Sorting Algorithim Gone Very Wrong

I was trying to make a sorting Algorithim, the bubble sort one, from scratch. All was going well until the end of the first iteration, where it just stops.

//initial functions
minInt = 0
maxInt = 10
a = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
b = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
c = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
d = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
e = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);

//define array
x = [5, 7, 1, 3, 10, a, b, c, d, e]

//this SHOULD run and compare every number in the array, and act accordingly
function SortingAlgorithm(array)
{ console.log(array);
  for(let i = 0; i <= array.length; i++)
{
  if(array[i] > array[i+1])
  {
    [array[i], array[i+1]] = [array[i+1], array[i]]
  }
  console.log(array)

if(i == array.length && array !== array.sort((a,b) => a - b))
{
  i = 0
  }
}
  
}

//why does this language hate me
SortingAlgorithm(x)

I’ve tried changing the last conditional to another loop that checks the entire array to see if it’s in numerical order, setting i back to 0, but it just spits out the end of the first iteration until the complier refuses to comply.

p.s. sorry if it’s hard to read, it’s 5:24 in the morning and i’m but 16, please don’t burn me at metaphorical stake >u<

Hi @divaroni and welcome to our community!

Here’s a key point of understanding:

console.log([1, 2, 3]==[1, 2, 3]) // false

Do you know why this comparison returns false?

Also, have you looked at this FCC project? It’s how I learned how to implement a bubble sort algorithm in JavaScript.

So the issue seem to be the condition resetting iteration? Try adding couple console.log print outs to make sure the actual check result is always as you expect it to be.

Resetting i like that suggest there might be another loop needed. This is exactly what such condition is doing after all - it imitates a while loop wrapped around the current for loop and adds unnecessary complexity while doing that.

Thats odd. Why does it return false? Are they not the exact same array? What’s off here, what am I missing?

that’s how JavaScript work, you can’t compare arrays like that

I did that originally. I had used the following instead of checking if it’s the same as a presorted array. The problem with that is that it will do the first iteration, and just print that out repeatedly until it hits an error due to idk, too much was typed and it hit a limit.

What else can I do? Or am I doing the second loop wrong?

(let u = 0; u <= array.length; u++)
{
if(array[u] > array[u+1]
{
i = 0
}
}

Ah. What else can I do then. Also, I checked the FCC project…I’m not that proficient in html I fear….

Try adding some console.log, to take a look at the values that are compared.

So it prints out every time the for loop goes to the next iteration, this is what’s given.

[
   5, 7,  1,  3, 10,
  10, 3, 10, 10,  4
]
[
   5, 7,  1,  3, 10,
  10, 3, 10, 10,  4
]
[
   5, 1,  7,  3, 10,
  10, 3, 10, 10,  4
]
[
   5, 1,  3,  7, 10,
  10, 3, 10, 10,  4
]
[
   5, 1,  3,  7, 10,
  10, 3, 10, 10,  4
]
[
   5, 1,  3,  7, 10,
  10, 3, 10, 10,  4
]
[
  5,  1,  3,  7, 10,
  3, 10, 10, 10,  4
]
[
  5,  1,  3,  7, 10,
  3, 10, 10, 10,  4
]
[
  5,  1,  3,  7, 10,
  3, 10, 10, 10,  4
]
[
  5,  1,  3, 7, 10,
  3, 10, 10, 4, 10
]
[
  5,  1,  3, 7, 10,
  3, 10, 10, 4, 10
]
[
  5,  1,  3, 7, 10,
  3, 10, 10, 4, 10
]
[
  1,  3,  3,  4,  5,
  7, 10, 10, 10, 10
]
 
Repl Terminal

I’ve been told(literally just now) that javascript doesn’t allow you to compare arrays in such a way. I think? but everything works up until the first 10 reaches the end of the array. After that, the program just decides to stop.

there is an other problem with this, you are creating a sorting algorithm, using sort you just doing the job of the algorithm

you should not have this at all

Your for loop will run until the condition is met (i.e. i <= array.length).

However, you’re then trying to reset the loop by setting i to 0 again, using a faulty condition, which doesn’t work, so the loop terminates at this point.

Here’s a useful explanation of why direct array comparison doesn’t work:

I needed a sort of checker that would compare the two to make sure the algorithm did it’s job. I’ll have to change that somehow.

So what I got from that was I need another way to compare the arrays/check the array. What is a non-primative value? Is there an easy way to do that or do I have to go down a JavaScript rabbit hole.

So I’ve done some research and found something odd. When I try it in a different way, what ends up happening is it still only does a single iteration, and repeats it for a while, but eventually it just changes to what’s being compared, the sorted array. Like it wants to stop iterating so it just copy and pastes it.

//initial functions
i = 0
minInt = 0
maxInt = 10
a = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
b = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
c = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
d = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
e = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);

//define array
x = [5, 7, 1, 3, 10, a, b, c, d, e]

//this SHOULD run and compare every number in the array, and act accordingly
function SortingAlgorithm(array)
{ console.log(array);
  for(i ; i <= array.length; i++)
{
  if(array[i] > array[i+1])
  {
    [array[i], array[i+1]] = [array[i+1], array[i]]
  }
  console.log(array)

  if(i == array.length && JSON.stringify(array) !== JSON.stringify(array.sort((a,b) => a - b)))
{
  i = 0
  }
  
}


}

//why does this language hate me
SortingAlgorithm(x)
console.log(x.sort((a,b) => a - b))




Could you explain how this is supposed to work?

if i is the same as the length of array in the function, the loop stops. Only problem is that it only takes it as far as the first iteration before it stops again. I used a .sort function kind of like a teacher checking a students work for answers. If they aren’t same then something isn’t right. If the correct array isn’t the same as the array that is being put out by the algorithm, and i is equal to the length of the aforementioned array, the i will be set to zero and the loop should restart.

Leaving aside fact of using different sorting to determine, whether this sort did the job.

Consider the following code:

const array = [5, 2, 1];
console.log(array.sort((a,b) => a - b));  // [1, 2, 5]
console.log(array);  // ?

What will be printed out? How does that affect your algorithm?

does it end up changing the array itself? If it changes the array itself, that could mess up the algorithim. I did end up changing the code a bit and it nearly worked, i didn’t use the .sort.

//initial functions
i = 0
minInt = 0
maxInt = 10
a = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
b = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
c = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
d = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
e = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);

//define array
x = [5, 7, 1, 3, 10, a, b, c, d, e]

//this SHOULD run and compare every number in the array, and act accordingly
function SortingAlgorithm(array)
{ console.log(array);
  for(i ; i <= array.length; i++)
{
   
    
  if(array[i] > array[i+1])
  {
    [array[i], array[i+1]] = [array[i+1], array[i]]
    console.log("Number Moved: " +array[i+1])
  }
  console.log(array)
 
if(i === array.length)
{
 for(let u = 0; u <= array.length; u++)
 {
  if(array[u] > array[u+1])
  {
    i = 0
    console.log("start of a new iteration \n ________________")
    break;
  }else
  {
    break;
  }

 }
}
}
}

//why does this language hate me

SortingAlgorithm(x)

Yes, sort method modifies the input array.

Regarding your new version. Try going though the part checking if array is sorted, by hand for some example array (ie. [1, 5, 0, 3]) and consider whether every step happens as expected.

i’ve checked it and everything works as planned up until the first two numbers. It always ends up sorting everything but never moving the first '5’. It repeats the almost sorted array until the complier stops.