Knowee
Questions
Features
Study Tools

SortingOne common task for computers is to sort data. For example, people might want to see all their files on a computer sorted by size. Since sorting is a simple problem with many different possible solutions, it is often used to introduce the study of algorithms.Insertion SortThese challenges will cover Insertion Sort, a simple and intuitive sorting algorithm. We will first start with a nearly sorted list.Insert element into sorted listGiven a sorted list with an unsorted number in the rightmost cell, can you write some simple code to insert into the array so that it remains sorted?Since this is a learning exercise, it won't be the most efficient way of performing the insertion. It will instead demonstrate the brute-force method in detail.Assume you are given the array indexed . Store the value of . Now test lower index values successively from to until you reach a value that is lower than , at in this case. Each time your test fails, copy the value at the lower index to the current index and print your array. When the next lower indexed value is smaller than , insert the stored value at the current index and print the entire array.ExampleStart at the rightmost index. Store the value of . Compare this to each element to the left until a smaller value is reached. Here are the results as described:1 2 4 5 51 2 4 4 51 2 3 4 5Function DescriptionComplete the insertionSort1 function in the editor below.insertionSort1 has the following parameter(s):n: an integer, the size of arr: an array of integers to sortReturnsNone: Print the interim and final arrays, each on a new line. No return value is expected.Input FormatThe first line contains the integer , the size of the array .The next line contains space-separated integers .ConstraintsOutput FormatPrint the array as a row of space-separated integers each time there is a shift or insertion.Sample Input52 4 6 8 3Sample Output2 4 6 8 8 2 4 6 6 8 2 4 4 6 8 2 3 4 6 8 Explanation is removed from the end of the array.In the st line , so is shifted one cell to the right.In the nd line , so is shifted one cell to the right.In the rd line , so is shifted one cell to the right.In the th line , so is placed at position .Next ChallengeIn the next Challenge, we will complete the insertion sort.

Question

SortingOne common task for computers is to sort data. For example, people might want to see all their files on a computer sorted by size. Since sorting is a simple problem with many different possible solutions, it is often used to introduce the study of algorithms.Insertion SortThese challenges will cover Insertion Sort, a simple and intuitive sorting algorithm. We will first start with a nearly sorted list.Insert element into sorted listGiven a sorted list with an unsorted number in the rightmost cell, can you write some simple code to insert into the array so that it remains sorted?Since this is a learning exercise, it won't be the most efficient way of performing the insertion. It will instead demonstrate the brute-force method in detail.Assume you are given the array indexed . Store the value of . Now test lower index values successively from to until you reach a value that is lower than , at in this case. Each time your test fails, copy the value at the lower index to the current index and print your array. When the next lower indexed value is smaller than , insert the stored value at the current index and print the entire array.ExampleStart at the rightmost index. Store the value of . Compare this to each element to the left until a smaller value is reached. Here are the results as described:1 2 4 5 51 2 4 4 51 2 3 4 5Function DescriptionComplete the insertionSort1 function in the editor below.insertionSort1 has the following parameter(s):n: an integer, the size of arr: an array of integers to sortReturnsNone: Print the interim and final arrays, each on a new line. No return value is expected.Input FormatThe first line contains the integer , the size of the array .The next line contains space-separated integers .ConstraintsOutput FormatPrint the array as a row of space-separated integers each time there is a shift or insertion.Sample Input52 4 6 8 3Sample Output2 4 6 8 8 2 4 6 6 8 2 4 4 6 8 2 3 4 6 8 Explanation is removed from the end of the array.In the st line , so is shifted one cell to the right.In the nd line , so is shifted one cell to the right.In the rd line , so is shifted one cell to the right.In the th line , so is placed at position .Next ChallengeIn the next Challenge, we will complete the insertion sort.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The text you provided is a detailed explanation of the Insertion Sort algorithm, a simple and intuitive sorting algorithm often used in computer science to introduce the study of algorithms.

Here's a step-by-step breakdown of the Insertion Sort algorithm:

  1. Start at the rightmost index of the array. Store the value of the element at this index. This is the element that you want to insert into the sorted part of the array.

  2. Compare this element with each element to its left until you find a smaller value.

  3. Each time you find a value that is not smaller (i.e., a value that is greater or equal), shift that larger value one cell to the right.

  4. When you find a value that is smaller, insert the stored value at the current position.

  5. Print the array each time there is a shift or insertion.

  6. Repeat this process for all elements in the array.

The function insertionSort1 described in the text takes an array of integers to sort as its parameter. It doesn't return anything; instead, it prints the array each time there is a shift or insertion.

The input format is as follows: The first line contains the integer n, the size of the array. The next line contains n space-separated integers.

The output format is as follows: Print the array as a row of space-separated integers each time there is a shift or insertion.

The text also includes a sample input and output to demonstrate how the algorithm works.

This problem has been solved

Similar Questions

Write an algorithm for insertion sort. Analyze insertion sort algorithmfor best case and worst case.

The __________________ algorithm sorts a list of values by repetitively inserting a particular value into a subset of the list that has already been sorted. A. insertion sort B. merge sort C. bubble sort D. quick sort E. selection sort

Illustrate the operations of the New Insertion sort algorithm for the array with thegiven set of elements. (For the illustration process assign the values only once to thegiven algorithm codes and then use diagrammatic way to reach the answer.)NEW-INSERTION-SORT (A)1 for j = 2 to A.length2. i =13. while A[j] > A[i]4. i = i + 15 key = A[j]6 for k = 0 to j - i - 17 A[j-k] = A[j-k-1]8 A[i] = key

Program to implement Insertion Sort, Bubble sort, Selection sort, Quick sort

Which of the following sorting methods would be most suitable for sorting a list which is almost sorted?ans.insertion sortmerge sortbubble sortselection sort Previous Marked for Review Next

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.