How do you get programs like bubble sort, quick sort to print a result.

def bubblesort(list):
for passes in range(len(list)-1, 0, -1):
for index in range(passes):
if list[index] < list[index + 1]:
list[index], list[index + 1] = list[index + 1], list[index]
return list

Thanks

def bubblesort(list):
    for passes in range(len(list)-1, 0, -1):
        for index in range(passes):
            if list[index] < list[index + 1]:
                list[index], list[index + 1] = list[index + 1], list[index]
    return list

print bubblesort([3,2,66,5,22,62,61,16])

The return statement can be printed just with the keyword print.

I dont know if that is exactly what you want but if not, just ask a more specific question. ;)

yes it did thanksso much. does it work for quicksort, selection sort, insertion sort, and merge sort as well?

sorry i forgot to include the codes

def quicksort(list):
if list == []:
return []
else:
pivot = list[0]
lesser = quicksort([x for x in list[1:] if x < pivot])
greater = quicksort([x for x in list[1:] if x >= pivot])
return lesser + [pivot] + greater

def selectionsort(list):
for k in range(len(list) - 1):
j = i = k
for i in range(k, len(list)):
if list < list[j]:
j = i
list[j], list[k] = list[k], list[j]
return list

print bubblesort([3,2,66,5,22,62,61,16])

def InsertionSort(list):
for i in range(1,len(list)):
key = list
j = i - 1
while j>0 and list[j]>key:
list[j + 1] = list[j]
j = j - 1
list[j + 1] = key


def sort(list):
if len(list) <= 1: return list
middle = len(list) // 2
return merge (sort(list[0:middle]),
sort(list[middle:]))


def merge(left, right):
merged = []
i = 0
j = 0
while(len(merged) < len(left)+len(right)):
if left < right[j]:
merged.append(left)
i += 1
if i == len(left):
y = right[j:]
for x in y:
merged.append(x)
break
else:
merged.append(right[j])
j += 1
if j == len(right):
y = left[i:]
for x in y:
merged.append(x)
break
return merged

You use print to print anything in Python. Since your functions are returning values, you can use print to output those values.

This article has been dead for over six months. Start a new discussion instead.