Question: Please help me with this sorting Algorithm code! I cant figure it out Here's my code int n = len-1; for (int i = 1;

Please help me with this sorting Algorithm code! I cant figure it out

Please help me with this sorting Algorithm code! I cant figure itout Here's my code int n = len-1; for (int i =1; i int j = i-2; while (j > 0 && (number[j]> number[j-1])) { swap(number, number[j], number[j-1]); j = j - 1; }

Here's my code

int n = len-1;

for (int i = 1; i

int j = i-2;

while (j > 0 && (number[j] > number[j-1])) {

swap(number, number[j], number[j-1]);

j = j - 1;

}

number[j] = number[j-1];

}

for (int j = 0; j

std::cout

It wouldn't output the correct answer

Sample: 5 3 1 5 4 2

Sample output:

Before: 3 1 5 4 2 After: 1 2 3 4 5

The program defined in insertionSort.cpp gets a list of numbers as input from the user and calls the insertion sort algorithm to sort them. It also displays the list before and after the sorting. The insertion sort algorithm should be defined in the InsertionSort.h file, but this definition has been omitted and it is your task to provide it. The appropriate insertion sort function is to be defined in the InsertionSort.h file The InsertionSorth file performs an indlude of Swap h, where a swap function is defined It accepts an array of integers, a starting index and a destination index, and it swaps the values found in the two specified indices in the list. You must use this swap function to perform swap operations in your implementation of insertion sort. Do not define a different swap function in InsertionSort.h, and do not perform the swapping manually in place. You have to call the function we are providing for you To test your code, run it with inputs of the following structure The first line contains an integer N, which specifies the size of the list to be sorted The next N lines contain a single integer on each line, representing the list we are sorting

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!