   Chapter 11.3, Problem 24ES ### Discrete Mathematics With Applicat...

5th Edition
EPP + 1 other
ISBN: 9781337694193

#### Solutions

Chapter
Section ### Discrete Mathematics With Applicat...

5th Edition
EPP + 1 other
ISBN: 9781337694193
Textbook Problem
1 views

# How many comparisons between values of a [ j ] and x actually occur when insertion sort is applied to the array of exercise 20?

To determine

How many comparisons between values of a[j] and x actually occur when insertion sort is

applied to the array of exercise 20 ?

Explanation

Given information:

It is required to find the number of comparisons between values of a[j] and x when insertion

sort is applied to the array given below

 a[n] Stored Value a 6 a 2 a 1 a 8 a 4

Concept used:

It is required to find the number of comparisons between values of a[j] and x when insertion

sort is applied to the array.

Calculation:

for k:=2to nx:=a[k]j:=k1

while (j0)if x<a[j]thena[j+1]:=a[j]j:=j1end ifend whilea[j+1]:=xnext k

In the insertion sort algorithm, when the for-next loop is first iterated, k is assigned the value 2 ;

x is assigned the value of a, which is 2 ; and j the value of k1 which is 1.Since j1 ,

the while loop is entered and the condition for the if-then-else statement is tested. Inside the if-then-else statement, the value of x is compared with a, since x<a, therefore, a is assigned the value of a, which is 6, j is assigned the value of j1 ,which is 0 and a is assigned the value of x, which is 2 .The condition for while loop is tested again and since j=0 ,which is not satisfied, so the while loop is not entered. Again, the value of k is

incremented by 1 and the for-next loop is entered for the second time

### Still sussing out bartleby?

Check out a sample textbook solution.

See a sample solution

#### The Solution to Your Study Problems

Bartleby provides explanations to thousands of textbook problems written by our experts, many with advanced degrees!

Get Started 