Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 16.1, Problem 16.2CP

Explanation of Solution

Insertion sort:

  • This is sorting algorithm in which the first two elements of the array are taken and these elements are compared. If the swap is required, then it swaps the elements and places the elements in proper order.
  • Now, this becomes a sorted portion of an array. Then it starts with the third element of the array and it is compared with the already sorted portion.
  • This is done to insert the elements relatively in correct position according to first two elements.
  • If the swap is required on comparison, then it is need to shift the elements until the third elements is placed in it correct position.
  • Now, the first three elements will be in sorted manner.
  • This process is continued for the fourth and subsequent elements until all of the elements are inserted into proper positions.

Example:

Consider the elements to sort (3, 6, 5, 2, 1, 4).

  • The number 3 is less than 6; so no changes are made; next number 6 is greater than 5. So, number 5 is inserted in between 3 and 6.

Blurred answer
Students have asked these similar questions
Answer the following question for basic sorting algorithms. Here is an array of 7 integers: (1)                     8   3   2   0   1   9   7   Draw this array after the FIRST iteration of the large loop in a selection sort (sorting from smallest to largest, and always pick the smallest element in each iteration).           Here is an array of 7 integers: (1)                     8   3   2   0   1   9   7   Draw this array after the FIRST iteration of the large loop in a bubble sort (sorting from smallest to largest).
Double Insertion Sort is a variation on Insertion Sort that works from the middle of the array out. At each iteration, some middle portion of the array is sorted. On the next iteration, take the two adjacent elements to the sorted portion of the array. If they are out of order with respect to each other, then swap them. Now, push the left element toward the right in the array so long as it is greater than the element to its right. And push the right element toward the left in the array so long as it is less than the element to its left. The algorithm begins by processing the middle two elements of the array if the array is even. If the array is odd, then skip processing the middle item and begin with processing the elements to its immediate left and right. Implement Double Insertion Sort, being careful to properly handle both when the array is odd and when it is even. by using java. 1) Implement the Double Insertion sort algorithm on a randomly generated list of N integer numbers. Your…
Double Insertion Sort is a variation on Insertion Sort that works from the middle of the array out. At each iteration, some middle portion of the array is sorted. On the next iteration, take the two adjacent elements to the sorted portion of the array. If they are out of order with respect to each other, then swap them. Now, push the left element toward the right in the array so long as it is greater than the element to its right. And push the right element toward the left in the array so long as it is less than the element to its left. The algorithm begins by processing the middle two elements of the array if the array is even. If the array is odd, then skip processing the middle item and begin with processing the elements to its immediate left and right. Implement Double Insertion Sort, being careful to properly handle both when the array is odd and when it is even. Using java.

Chapter 16 Solutions

Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage