19.7k views
4 votes
Instructions

Implement a sorting algorithm and sort the vocab list (found below) by the length of the strings. For this question, sort the list directly: do not define
a sort function or a swap function. Print the vocab list before and after it is sorted.
Note 1: Only swap the elements at index a and index b if the string at index a is a greater length than the string at index b.
Note 2: You should implement a sorting algorithm similar to the one from the lessons.
vocab ["Libraries", "Software", "Cybersecurity", "Logic", "Productivity"]
Expected Output
["Libraries', 'Software', 'Cybersecurity", "Logic', 'Productivity"]
[Logic, Software', 'Libraries", "Productivity", "Cybersecurity]

Instructions Implement a sorting algorithm and sort the vocab list (found below) by-example-1
User BottleZero
by
6.8k points

1 Answer

1 vote

Answer:

vocab = [ "Libraries", "Bandwidth", "Hierarchy", "Software", "Firewall", "Cybersecurity","Phishing", "Logic", "Productivity"]

print(vocab)

needNextPass = True

k = 1

#list = [x.lower() for x in list]

while k < len(vocab)-1 and needNextPass:

# List may be sorted and next pass not needed

needNextPass = False

for i in range(len(vocab) - k):

if vocab[i] > vocab[i + 1]:

# swap list[i] with list[i + 1]

temp = vocab[i]

vocab[i] = vocab[i + 1]

vocab[i + 1] = temp

needNextPass = True # Next pass still needed

print(vocab)

Step-by-step explanation:

Not sure its correct beacause it took me so long

User Cheerless Bog
by
6.9k points