Question

In: Computer Science

using javascript and without using javascript sort. Sort an array from lowest to highest const x...

 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]

Solutions

Expert Solution

// 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>


Related Solutions

Using JAVA Write a program that uses a 2-D array to store the highest and lowest...
Using JAVA Write a program that uses a 2-D array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of the following methods with their appropriate parameters: a.) getData: This method reads and stores the data in the 2-D array. b.) averageHigh: This method calculates and returns the average high temperature of the year. c.)...
Sort the pH of the following mixtures from LOWEST (most acidic) to HIGHEST (most basic): (I)...
Sort the pH of the following mixtures from LOWEST (most acidic) to HIGHEST (most basic): (I) 0.001 mol of HCl added to 1 L water (II) 0.001 mol HCl added to a buffer solution of 0.1 M HCOOH and 0.1 M NaHCOO (pKa=3.75) (III) 0.001 mol HCl added to a buffer solution of 0.1 M NH4Cl and NH3 (pKa=9.25)
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low. im trying to...
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Using Javascript, complete the following /************************************************************************** * * Array callback filtering * **************************************************************************/ /** * Write...
Using Javascript, complete the following /************************************************************************** * * Array callback filtering * **************************************************************************/ /** * Write and export a function named "everyEven" which takes an array and a test * function for checking individual elements of the array. The "everyEven" * function should test the even elements of the array and return true only * if at least one of the even elements passes the test. * * @param arr An array whose even elements should be tested * @param...
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in...
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in place. * * @param students array to be sorted, can be empty, but cannot be null */ public static void sortGPA(Student[] students) { // TODO: implement this } Student class: public class Student extends Person { private double GPA; public Student(String lastName, String firstName, double gpa) { super(lastName, firstName); this.GPA = gpa; } public double getGPA() { return GPA; } @Override public boolean equals(Object...
Which of the following is listed in the correct order, from the highest to the lowest...
Which of the following is listed in the correct order, from the highest to the lowest taxonomic unit?
Suppose you are given the following array X = [7, 9, 1, 6] Sort the array...
Suppose you are given the following array X = [7, 9, 1, 6] Sort the array in ascending order using the selction sort algorithm. Write the state of the array after each pass. Pass1: Pass2: Pass3: Suppose you are given the following array X = [7, 9, 1, 6] Sort the array in ascending order using the selction sort algorithm. Write the state of the array after each pass. Pass1: Pass2: Pass3:
Rank these aqueous solutions from highest boiling point to lowest boiling point. 1 = highest boiling...
Rank these aqueous solutions from highest boiling point to lowest boiling point. 1 = highest boiling point; 4 = lowest boiling point 0.40 m C2H6O2 0.20 m Na3PO4 0.30 m KNO3 0.20 m C6H12O6  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT