Question: This is the code and my algorithm I've written for insertion sort. What do I need to add or fix? public class InsertionSort { /**
This is the code and my algorithm I've written for insertion sort. What do I need to add or fix?
public class InsertionSort { /** The method for sorting the numbers */ public static void insertionSort(double[] list) { for (int i = 1; i < list.length; i++) { /** insert list[i] into a sorted sublist list[0..i-1] so that list[0..i] is sorted. */ double currentElement = list[i]; int k; for (k = i - 1; k >= 0 && list[k] > currentElement; k--) { list[k + 1] = list[k]; }
// Insert the current element into list[k+1] list[k + 1] = currentElement; } } }
Step 0: Have an array of integers called list Step 1: FOR i from 1 to list.length DO Step 1.1: SET currentElement to the value of the element at index i Step 1.2: SET int k Step 1.3: FOR k from i 1 to 0 AND from value of the element at index k greater than currentElement DO Step 2: list[k+1] = list[k] Step 3: insert currentElement into list[k+1] Step 4: list is now sorted
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
