Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Insertion sort, we use the compare - and - swap operation to do sorting. In this question, we shall use a more general operation

In Insertion sort, we use the compare-and-swap operation to do sorting. In this question, we shall use
a more general operation flip to do sorting.
Let L:a1,a2,dots,an be a list of elements (drawn from a totally ordered set). The flip operation
flip(L,i,j) converts the list a1,a2,dots,ai-1,ai,ai+1,dots,aj-1,aj,aj+1,dots,an
toa1,a2,dots,ai-1,aj,aj-1,dots,ai+1,ai,aj+1,dots,an
i.e. filp(L,i,j) reverses the order of elements in the sublist ai,ai+1,dots,aj-1,aj.
Assuming flip (L,i,j) takes O(j-i) time.
(a) Given a list of elements a1,a2,dots,an such that aiin{0,1},1in. Present an algorithm that
sorts the list in O(nlgn) time. You are allowed to use only the flip operation to rearrange the
elements.
(b) Using the algorithm of Part (a) as a subroutine, present an O(n2lgn)-time algorithm that
sorts a list of n elements, ai,1in, drawn from a totally ordered set.
(Note: You are to reduce sorting to the problem of sorting a list of 0's and 1's. Hence,
presenting an algorithm without using Part (a) will receive a 0 mark.)
image text in transcribed

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

International Baccalaureate Computer Science HL And SL Option A Databases Part I Basic Concepts

Authors: H Sarah Shakibi PhD

1st Edition

1542457084, 978-1542457088

More Books

Students also viewed these Databases questions