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

? Inbox (852)-kuznera@mail - Guided Practice Assignmen zy Home-zyBooks 7.18. Lab

ID: 3706908 • Letter: #

Question

? Inbox (852)-kuznera@mail - Guided Practice Assignmen zy Home-zyBooks 7.18. Lab: Insertion Sort C Chegg Study | Guided Solut ? ????https://learn R106Winter2018/chapter/T/section/18 zyBooks My library> EGR 106 home> 7.18: Lab: Insertion Sort Algonthm zyBooks catalog Help/FAQ Alexander Kuzner 7.18 Lab: Insertion Sort Algorithm The insertion sort algorithm updates its sorted array as each new data value is entered. it is an in-place algorithm, in that the sorted array at each step writes over the array from the previous step. Let us start with sorting in descending order (largest to smallest). The ascending order case follows as a simple modification to the descending case. Assume that we have thus far read N values and these are scrted in correct order (largest to smallest) in our array. We then read the next number, and we desire to place it in our array in the proper position In order to do so, we compare the new Input with the existing array values, starting with the last array element. If the new input has a value greater than the last array element, shift the array element to the next higher index i.e., copy the N-th element to the N+1-th element). Continue to compare with lower indices, as long as the new input has a greater value than the current element, copy that element to the next higher index. At some point, the input value may not be greater than the array element in this case, break out of the element-by-element testing and replace the previously compared element by the new input For example: Assume that we have already read these five numbers: (1,5,3,2, 6). The numbers are sorted as they are read, thus our array will look like the following (where beyond the fifth element the array values are undefined and hold some random value) Let the next input have a value of 4 We start by comparing the new input with the last array element (that is, 4 is compared to1) the new value is larger so we copy the array element to the right, resulting in a new array of. Our next step is to compare the element at the next lower index with the inpute, 4 compared to 2); again, the new value is larger, so we do the copy resulting in We compare the new value of 4 versus the array value of 3. Again, the new input has a larger value, so we again copy the array element to the right resulting in We proceed to the next element where it turns out that our input value is no longer greater than cur array value e, 4 ccmpared to 5). At this point, we stop comparing by breaking out of our comparison loop. We then replace the value at the previously compared index with our new input resulting in the newly sorted array of

Explanation / Answer

#include <stdio.h>

#define MAX 50

int

main ()

{

int arr[MAX], limit, k, m;

char end;

int i, j, temp;

for (k = 0; k < MAX; k++)

{

printf (" press a to add next element and press q to exit ");

scanf ("%c", &end);

if (end == 'q')

{

break;

}

else if (end == 'a')

{

printf ("enter next element:-");

scanf ("%d", &arr[i]);

for (i = 1; i <= k; i++)

{

j = i;

while (j > 0 && arr[j] > arr[j - 1])

{

temp = arr[j];

arr[j] = arr[j - 1];

arr[j - 1] = temp;

j--;

limit = k;

}

}

for (m = 0; m < k; m++)

printf ("%d ", arr[m]);

}

}

return 0;

}

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