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 that uses a two-dimensional array to store the highest and lowest temperatures for...
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. These methods MUST be your original code.   Your program should output all the values in the array and then output the average high and the...
Write a program that uses a two dimensional array to store the highest and lowest temperatures...
Write a program that uses a two dimensional array to store the highest and lowest temperatures for each month of the calendar year. The temperatures will be entered at the keyboard. This program must output the average high, average low, and highest and lowest temperatures of the year. The results will be printed on the console. The program must include the following methods: A method named inputTempForMonth whose purpose is to input a high and a low temperature for a...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as the completion of one merge operation. Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from...
USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that...
USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that during the final merge, it is not necessary to merge all n elements, but only the elements in positions 1 to k.
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.
Sorting (quick) Sort the array using quick sort. Write the array content after each pass (i.e.,...
Sorting (quick) Sort the array using quick sort. Write the array content after each pass (i.e., from pass 1 to pass 7). Each pass is defined as the completion of one partition. We always pick the first array element as the pivot for each partition.
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT