Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This has to be in HTML with a javascript file in it 1. Write a sorting algorithm

ID: 3809172 • Letter: T

Question

This has to be in HTML with a javascript file in it

1. Write a sorting algorithm. The algorithm is as follows:Use the merge function from Homework 9 to write a sorting algorithm. The algorithm is as follows:

function newsort(array):

   if array length <= 1:

      // an array of length 1 is already sorted

      return array

   else:

      // divide the array in half, sort them separately

      // and then merge them back together

      mid = middle index of array

      left = a new array of size mid

      right = a new array of size array length – mid

      copy leftmost elements of array into left

      copy rightmost elements of array into right

      left = newsort(left)

      right = newsort(right)

      result = merge(left, right);

      return result

2. Verify that the sorting algorithm above actually sorts some data. Then profile the new sorting algorithm and compare it against the other sorting algorithms you have studied. Produce a plot similar to that given in the key points that shows the relationship between the time it takes to sort and the number of elements in the array. The following functions should help you with this problem:

// build an array of random data to sort

function makeRandomArray(size)

{

    var result = Array();

    while (size > 0)

    {

        result.push(Math.floor(Math.random()*100000));

        --size;

    }

    return result;

}

// time the sorting algorithm on the given array, in milliseconds

function timeSortingAlgorithm(arr, sortingAlgorithm)

{

    var startTime = new Date()

    sortingAlgorithm(arr);

    var endTime = new Date();

    return endTime.getTime() - startTime.getTime();

}

Explanation / Answer

1)Merg sort using java script

<!DOCTYPE html>

<html>
<body>
<p>sorting using merge sort</p>
<p id="demo"<</p>
<script>

function mergesort(left,right)
{
   var i = 0;
   var j = 0;
   var results = [];

   while (i < left.length || j < right.length) {
       if (i === left.length) {
           results.push(right_part[j]);
           j++;
       }
else if (j === right.length || left[i] <= right[j]) {
           results.push(left[i]);
           i++;
       } else {
           results.push(right[j]);
           j++;
       }
   }
   return results;
}

function newsort(array)
{
   var result=[];var left[]; var right[];

   if(array.length<=1)

   document.write("an array of length 1 is already sorted");
   return array;

   else {
   var mid=(array.length)/2;
   for(var i=0;i<mid; i++)
   {
        left[i]==array[i];
   }
  
  

for(var j=(mid+1);j<(array.length)+1;j++){
       right[j]==array[j];
   }
       left=newsort(left);
       right=newsort(right);
       result=merge(left,right);
       return result;
      
   }

}


var array=[8,10,4,32,5];
console.log(newsort(array));

</script>
</body>
</html>

2)

<!DOCTYPE html>
<html>
<body>
<p>sorting using merge sort</p>
<p id="demo"<</p>
<script>

function mergesort(left,right)
{
   var i = 0;
   var j = 0;
   var results = [];

   while (i < left.length || j < right.length) {
       if (i === left.length) {
           results.push(right_part[j]);
           j++;
       }
else if (j === right.length || left[i] <= right[j]) {
           results.push(left[i]);
           i++;
       } else {
           results.push(right[j]);
           j++;
       }
   }
   return results;
}

function newsort(array)
{
   var result=[];var left[]; var right[];

   if(array.length<=1)

   document.write("an array of length 1 is already sorted");
   return array;

   else {
   var mid=(array.length)/2;
   for(var i=0;i<mid; i++)
   {
        left[i]==array[i];
   }
  
  

for(var j=(mid+1);j<(array.length)+1;j++){
       right[j]==array[j];
   }
       left=newsort(left);
       right=newsort(right);
       result=merge(left,right);
       return result;
      
   }

}


function makeRandomArray(size)
{
var res = Array();
while (size > 0)
{
res.push(Math.floor(Math.random()*100000));
--size;
}
return res;
}

var arr=makeRandomArray(10);

function timeSortingAlgorithm(arr, sortingAlgorithm)
{
var startTime = new Date()
sortingAlgorithm(arr);
var endTime = new Date();
return endTime.getTime() - startTime.getTime();
}


function sortingAlgorithm(arr)
{
   var temp;
for(var m=0;m<arr.length;m++)
{
for(var n=1;n<arr.length;n++)
   {
   if(arr[m]>arr[n])
       {
       temp=arr[n];arr[n]=arr[m];arr[m]=temp;
       }
   }
   }
   return arr;
}
      

console.log("The time to sort"+ timesortingAlgorithm());

</script>
</body>
</html>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote