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
IN C PROGRAMMING LANGUAGE AND COMMENT EVERY LINE PLEASE SO I CAN UNDERSTAND EVERY STEP , The selection sort is one of several techniques for sorting an array. A selection sort compares every element of an array with all the other elements of the array and exchanges their values if they are out of order. After the first pass of a selection sort, the first array element is in the correct position; after the second pass the first two elements of the array are in the correct position, and so on. Thus, after each pass of a selection sort, the unsorted portion of the array contains one less element. Write and test a function that implements this sorting method.
Write a program that reads the numbers and sorts them by using the Counting Sort algorithm and finally search a number from that array using Linear Search Algorithm. Input: 3 6 5 4 789 Search Item: 7 Output: Sorted Array: 3 4 5 6 789 Search item 7 is found.
Implement the following procedure in C language to generate prime numbers from 1 to 100 into a program. This procedure is called sieve of Eratosthenes. Step 1: Fill an array num[100] with numbers from 1 to 100 Step 2: Make the first entry as zero. Step 3: Starting with the second entry in the array, set all its multiples to zero. Step 4: Proceed to the next non-zero element and set all its multiples to zero. Step 5: Repeat step 4 till you have set up the multiples of all the non-zero elements to zero Step 6: At the conclusion of step 4, all the non- zero entries left in the array would be prime numbers, so print out these numbers. of the -End question-

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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education