Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Use step count method and analyze the time complexity Code: sorted_disks sort_lawnmower(const disk_state& before) { assert(before.is_alternating()); disk_state temp = before; int count = 0;

1. Use step count method and analyze the time complexity

Code:

sorted_disks sort_lawnmower(const disk_state& before) {

assert(before.is_alternating());

disk_state temp = before;

int count = 0;

while(!temp.is_sorted())

{

for(size_t i = 0; i

{

if(temp.get(i) > temp.get(i+1))

{

temp.swap(i);

count++;

}

}

for(size_t i = (temp.total_count() -1); i > 0 ; i--)

{

if(temp.get(i)

{ temp.swap(i-1);

count++;

}

}

}

return sorted_disks(disk_state(temp), count);

}

image text in transcribed

sorted_disks sort_lawnmower(const disk_state& before) { // check that the input is in alternating format assert(before.is_alternating()); disk_state temp = before; int count = 0; while( ! temp.is_sorted()) { for(size_t i = 0; i temp.get(i+1)) { temp. swap(i); count++; for(size_t i = (temp.total_count() -1); i >0; i--) { if(temp.get(i)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions