I was tasked with the following practice problem:
(Bubble sort) Write a sort function that uses the bubble-sort algorithm. The bubble-sort algorithm makes several passes through the list. On each pass, successive neighboring pairs are compared. If a pair is in decreasing order, its values are swapped; otherwise, the values remain unchanged. The technique is called a bubble sort or sinking sort because the smaller values gradually “bubble” their way to the top and the larger values “sink” to the bottom. Write a test program that reads in ten numbers, invokes the function, and displays the sorted numbers.
This is what I got so far,
def bubbleSort(array):
for i in range(len(array)):
for j in range(len(array) - 1, i, -1):
if (array[j] < array[j - 1]):
tmp = array[j]
array[j] = array[j - 1]
array[j - 1] = tmp
def printElm(array):
print("\n\n");
for i in range(len(array)):
print(" " + str(array[i]), end= " ")
def main():
list = [];
for i in range(10):
value = int(input('\n Enter Value ' + str(i + 1) + ": "));
list.append(value);
printElm(list);
bubbleSort(list);
printElm(list);
print("\n\n");
main();
But I keep getting the following error:
print(" " + str(array[i]), end= " ")
^
SyntaxError: invalid syntax
What is frustrating is that the the textbook references Python 3 while the professor wants work done in with Pycharm which is Python 2.6.
- How can I possibly solve this error?