0

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

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by jlm699
0
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. ;)

0

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

0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.