0

I am writing javascript code to do a naive sort for an array of values, however, I am getting an issue in my while loop where it crashes. It happens at

while(!sorted(dataStructure)){
naiveSort(dataStructure);
}

I want it so that if each element from i to end is less than the following one, sorted returns true, therefore the naive sort algorithm ends. There may be one or two things like createCanvas(800, 800); These are just methods from the p5js library but none of it is used in the algorithm.

naiveSort.js


function setup(){
    createCanvas(800, 800);
    for( var i = 0; i < dataStructure.length; i ++){
        dataStructure[i] = random(800);
        colorCode[i] = "blank";
    }

    while(!sorted(dataStructure)){
        naiveSort(dataStructure);
    }
}


function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

async function swap(arr, a, b){
    var temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
}

async function naiveSort(arr){
    for(var i = 0; i < arr.length; i ++){
        temp = Math.random() * ((arr.length - 1) - 0) + 0;
        swap(arr, i, temp);
        await sleep(10);
    }
}

function sorted(arr){
    for(var i = 1; i < arr.length-1; i ++){
        if(arr[i] > arr[i+1]){
            return false;
        }
    }
    return true;
}


function paint(col, rgb){
    for(var i = 0; i < dataStructure.length; i ++){
        if(colorCode[i] == col){
            stroke(0);
            fill(rgb);
            rect(i * dataWidth, 800 - dataStructure[i], dataWidth, dataStructure[i]);
        } 
    }
}


function draw(){

    background(51);
    paint("red", color(255, 0, 0));
    paint("green", color(0, 255, 0));
    paint("blank", color(255));
}

index.html


<!DOCTYPE html>
<html lang="">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>p5.js example</title>
  <style>
    body {
      padding: 0;
      margin: 0;
    }
  </style>
  <script src="../p5.js"></script>
  <script src="naiveSort.js"></script>
</head>

<body>
</body>

</html>

5
  • "I am getting an issue in my while loop where it crashes" - what exactly is the issue? What do you mean by "crashes"? What error message do you get? Commented Mar 31, 2020 at 20:13
  • Your naiveSort function is asynchronous and returns a promise, but you do never await that. Commented Mar 31, 2020 at 20:13
  • i got rid of it because i want to get it working before i animate a visualization, got rid of async, still no joy. Commented Mar 31, 2020 at 20:56
  • I visualize the data using a paint method, if i do not have the while loop the data is painted on a canvas, if its in the while loop it isnt. I will include the draw and paint method too if it helps Commented Mar 31, 2020 at 21:00
  • 1
    The problem is that your loop is not terminating, or at least taking a very very long time. It does paint to the canvas object at each iteration, but the browser will not render the updated canvas to your screen until the synchronous loop has finished. You must get the await with the delay to work properly before you will see any results. Commented Apr 1, 2020 at 18:18

1 Answer 1

2

What is the error output?

Looking at the sorted function, you are not verifying that the element at index 1 is indeed greater than that at index 0. You are also never awaiting the asynchronous return.

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

5 Comments

I got rid of async because i want to ensure everything works before I animate it visually, I get no error, my data just disappears whereas if I don't have the while loop (I use p5js, I am painting the data) I can see it but it isn't sorted obviously.
If i move i back to 0, problem is still there. I debugged, its something to do with the while loop for sorted function and naive sort function, just cant hit the nail on the head
Can you edit the post and add the error output? Is it even an error or is your sorting function not returning your desired output?
Okay so I call my method in setup, naiveSort. It iterates through array once, i print the array, it comes up in console. Its not sorted but i dont expect it to be after just one iteration. So i put it in a while loop, while(!sorted). When i try to load the webpage, it wont load, nothing in console, my print statement to print the array doesnt print, it seems like the page is forever loading. If you want me to send everything on to you to see for yourself I can, its pretty much all there the p5js library isnt though but it doesnt play a part in the algorithm so it doesnt really matter.
Message me the file. I'll take a look.

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.