Question

In: Computer Science

Language Javascript Implement Insertion Sort 1. Non-increasing order, 2. At least an array of 10 elements.,...

Language Javascript

  1. Implement Insertion Sort

    1. Non-increasing order,

    2. At least an array of 10 elements.,

    3. You can use a static array.

Solutions

Expert Solution

CODE:-

function insertionSort(arr){

var length = arr.length;

for (let i = 1; i < length; i++) {

var key = arr[i];

var j = i - 1;

while (j >= 0 && arr[j] < key) {

arr[j + 1] = arr[j];

j = j - 1;

}

arr[j + 1] = key;

}

return arr;

};

var a = [4,3,2,1,6,5,1,8,6,7]

console.log("Array before sort: "+a);

a = insertionSort(a)

console.log("Array after sort: "+a);

OUTPUT:-

NOTE:- If you have any doubts, please comment below. THANK YOU!!! Please give positive rating.


Related Solutions

All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1...
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1 and 2, please: a. Let the program generate a random array. b. Output both the original random array and the sorted version of it
Using the worst case scenario for a recursive insertion sort on an array of 5 elements...
Using the worst case scenario for a recursive insertion sort on an array of 5 elements {5, 4, 3, 2, 1} Determine a formula that counts the numbers of nodes in that recursion tree. What is the Big O for execution time. Determine a formula that expresses the height of the recursion tree. What is the Big O for memory?
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
Consider an array of 10 elements sorted in ascending order. Assume that you sort the table...
Consider an array of 10 elements sorted in ascending order. Assume that you sort the table in descending order by running a sorting algorithm. a) Count the exact number of changes made to the array (i.e. the number of operations of assignment T [...]=) if the bubble sort is applied (Assuming two assignments per swap). b) Count the exact number of changes made to the array (i.e. the number of operations of assignment T [...]=) if heap-sort is applied (Assuming...
Part 2: Insertion Sort Q3. Arrange the following arrays in ascending order using Insertion sort. Show...
Part 2: Insertion Sort Q3. Arrange the following arrays in ascending order using Insertion sort. Show all steps. 7          11        2          9          5          14 Q4. State the number of comparisons for each pass. Pass # comparisons 1st 2nd 3rd 4th 5th
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the list after each outer loop. Do his manually, i.e. step through the algorithm yourself without a computer. This question is related to data structure and algorithm in javascript (.js). Please give your answer keeping this in your mind.
1a .Write a program that perform insertion sort (ascending order) on an input array and print...
1a .Write a program that perform insertion sort (ascending order) on an input array and print the total number of comparisons that have been made. You can implement by using any programming languages such as C/C++. For example, in the case of array B = [ 30 , 10 , 20 , 40 ], there are 4 needed comparisons to perform insertion sort (30 vs 10, 20 vs 30, 20 vs 10, and 40 vs 30). 1b. Write a program...
Write a function to sort data (in increasing order) in an array using a. pass by...
Write a function to sort data (in increasing order) in an array using a. pass by value b. and pass by reference.
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort,...
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort,...
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT