Code Practice: Question 3 Instructions For this Code Practice, use the following initializer list, terms: terms = [“Bandwidth”, “Hierarchy”, “IPv6”, “Software”, “Firewall”, “Cybersecurity”, “Lists”, “Program”, “Logic”, “Reliability”] Write a sort program to alphabetize the list of computer terms, much like the preceding question. However, this time, define and use a function named swap as part of your solution. The swap function should not be a sort function, but should instead implement the swap functionality used in sorting. This function should swap the elements in the array at each of the indexes by comparing two elements to one another, and swapping them if they need to be in a different alphabetical order. Your function should take three parameters: the first is the array name, and the second and third are the indexes to swap. Print the terms array before and after it is sorted. Expected Output [‘Bandwidth’, ‘Hierarchy’, ‘IPv6’, ‘Software’, ‘Firewall’, ‘Cybersecurity’, ‘Lists’, ‘Program’, ‘Logic’, ‘Reliability’] [‘Bandwidth’, ‘Cybersecurity’, ‘Firewall’, ‘Hierarchy’, ‘IPv6’, ‘Lists’, ‘Logic’, ‘Program’, ‘Reliability’, ‘Software’]

The Correct Answer and Explanation is:
Here’s the Python code that fulfills the assignment exactly as requested:
# Initial list of terms
terms = ["Bandwidth", "Hierarchy", "IPv6", "Software", "Firewall", "Cybersecurity",
"Lists", "Program", "Logic", "Reliability"]
# Swap function
def swap(arr, i, j):
# Swap elements if they are in the wrong order
if arr[i] > arr[j]:
arr[i], arr[j] = arr[j], arr[i]
# Print the original list
print("Before sorting:")
print(terms)
# Simple bubble sort using swap function
n = len(terms)
for i in range(n):
for j in range(0, n - i - 1):
swap(terms, j, j + 1)
# Print the sorted list
print("\nAfter sorting:")
print(terms)
🔍 Explanation
This Python program implements a basic alphabetical sorting algorithm using a helper function called swap. The purpose is to sort a list of computer-related terms using this custom function rather than using Python’s built-in sort capabilities directly.
1. Initial List
The list named terms contains strings like "Bandwidth", "IPv6", "Software", and so on. This list is printed before sorting to show its original state.
2. Swap Function
The swap function is essential. It accepts three parameters:
arr: the list to work on.iandj: the indices of the elements to compare and possibly swap.
It checks whether arr[i] > arr[j] using Python’s built-in string comparison (which is lexicographical, or dictionary-order). If so, the two elements are swapped.
3. Sorting Algorithm
A bubble sort is used here for simplicity and clarity. It repeatedly compares adjacent pairs of items and uses swap to move them into the correct order. Each pass “bubbles” the largest item left to the end, and the process continues until the list is sorted.
While bubble sort is not efficient for large datasets, it is ideal for illustrating how swapping works and for small, controlled examples like this one.
4. Output
- The first output shows the original unsorted list.
- After the loop finishes, the sorted list is printed. The result matches the required output:
['Bandwidth', 'Cybersecurity', 'Firewall', 'Hierarchy', 'IPv6',
'Lists', 'Logic', 'Program', 'Reliability', 'Software']
This confirms that the code meets the problem requirements exactly.
