Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You have an array called shapes that contain the following: shapes = [square, round, circle, octagon, trapezoid, triangle] This array needs to be sorted in

You have an array called shapes that contain the following:

shapes = ["square", "round", "circle", "octagon", "trapezoid", "triangle"] 

This array needs to be sorted in alphabetical order. The following pseudocode will sort this array:

while flag == False flag = True for index = 0 to 5, step 1 if shapes[index] > shapes[index + 1] then flag == False swap = shapes[index] shapes[index] = shapes[index + 1] shapes[index + 1] = swap end if end while 

Why does our loop only go to 5 rather than 6?

1. The algorithm needs to always check the current element in the array with the element next to it. Otherwise we get an out of bounds error.

2. There is an error in the algorithm and the for loop should read:

for index = 0 to 6, step 1 
3. The loop needs to go to 5 because there are 5 elements in the array.
4. The algorithm doesn't need to check the last element. It will always be in the right order.

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

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

Students also viewed these Databases questions

Question

When would you use one approach, and when would you use another?

Answered: 1 week ago