Answered step by step
Verified Expert Solution
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started