Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 . 1 1 Lab: Insertion Sort ( shift data ) The program in Figure 1 . 5 . 1 demonstrates a variation of the

1.11 Lab: Insertion Sort (shift data)
The program in Figure 1.5.1 demonstrates a variation of the Insertion Sort algorithm that is based on swapping data. Simple sorting algorithms such as Selection Sort and Bubble Sort are based on swapping data. The Insertion Sort algorithm can be implemented without swapping data: it makes room for the current element by shifting values in the array.
Read more about this implementation of the Insertion Sort algorithm: Insertion Sort: KhanAcademy
Ex. If an array is being sorted using the insertion sort (swap):
1030405060|20
The current element to be inserted is 20 Each swap consists of 3 assignments (data moves)
Since 20<60, swap 20 and 60: 3 data moves
103040502060
Since 20<50, swap 20 and 50: 3 data moves
103040205060
Since 20<40, swap 20 and 40: 3 data moves
103020405060
Since 20<30, swap 20 and 30: 3 data moves
102030405060
Since 20 is not <10, the swapping process stops, with a total of 12 data moves.
Ex. If an array is being sorted using the insertion sort (shift):
1030405060|20
The current element to be inserted is 20
Each shift consists of 1 assignment (data move)
The first step is to copy 20 to a temp location
temp =20; //1 data move
Since 20<60, shift 60 to the right
103040506060//1 data move
Since 20<50, shift 50 to the right
103040505060//1 data move
Since 20<40, shift 40 to the right
103040405060//1 data move
Since 20<30, shift 30 to the right
103030405060//1 data move
Since 20 is not <10, the shifting process stops, and 20 is placed back into the array, after 10:
102030405060//1 data move
A total of 6 data moves.
Your task is to change the insertion sort by replacing swapping data with shifting data.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inference Control In Statistical Databases From Theory To Practice Lncs 2316

Authors: Josep Domingo-Ferrer

2002nd Edition

3540436146, 978-3540436140

More Books

Students also viewed these Databases questions

Question

Differentiate the function. r(z) = 2-8 - 21/2 r'(z) =

Answered: 1 week ago

Question

3. What should a contract of employment contain?

Answered: 1 week ago

Question

1. What does the term employment relationship mean?

Answered: 1 week ago