9.
Write a program in Python to create a list of 10 numbers and sort it using bubble sort
method in descending order.
OR
Answers
Answered by
1
Answer:
import random
def bubbleSort(arr):
n = len(arr)
# Traverse through all array elements
for i in range(n):
# Last i elements are already in place
for j in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
# using list comprehension + randrange()
# to generate random number list
arr = [random.randrange(1, 50, 1) for i in range(10)]
bubbleSort(arr)
print(arr)
Similar questions
Computer Science,
5 months ago
Math,
5 months ago
Math,
5 months ago
World Languages,
11 months ago
English,
11 months ago
Social Sciences,
1 year ago
Social Sciences,
1 year ago