In: Computer Science
using javascript and without using javascript sort. Sort an array from lowest to highest const x = [201,28,30,-5]
function sort(array){
// put your code here
return //the answer
}
sort(x)
// output: [-5,28,30,201]
// STAY HOME STAY SAFE
<!DOCTYPE html>
<html>
<title>Sort Using Bubble Sort</title>
<script>
// declare the array
const x = [201,28,30,-5]
function sort(array)
{
var end;
// perform bubble sort
for (var i=0; i < array.length; i++)
{
for (var j=0, end=array.length-i; j < end; j++)
{
// swap array[j] with array[j+1] if array[j] > array[j+1]
if (array[j] > array[j+1])
{
var c = array[j];
array[j] = array[j+1];
array[j+1] = c;
}
}
}
return array;
}
// call the sort function
document.write(sort(x));
</script>
<body>
</body>
</html>