Question: 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);
}

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
Get step-by-step solutions from verified subject matter experts
