Sorting data (i.e. placing data into some particular order, such as ascending or descending) is one of the most important computing applications. Python lists provide a sort meth- od. In this exercise, readers implement their own sorting function, using the bubble-sort method. In the bubble sort (or sinking sort), the smaller values gradually “bubble” their way upward to the top of the
list like air bubbles rising in water, while the larger values sink to the bottom of the list. The process that compares each adjacent pair of elements in a list in turn and swaps the elements if the second element is less than the first element is called a pass. The technique makes several passes through the list. On each pass, successive pairs of elements are compared. If a pair is in increasing order, bubble sort leaves the values as they are. If a pair is in decreasing order, their values are swapped in the list. After the first pass, the largest value is guaranteed to sink to the highest index of a list. After the sec- ond pass, the second largest value is guaranteed to sink to the second highest index of a list, and so on. Write a program that uses function bubbleSort to sort the items in a list.
What will be an ideal response?
```
# Bubble sort.
import random
# implements bubble sort on a list
def bubbleSort( bList ):
# make len( bList ) passes
for item in range( len( bList ) - 1 ):
# make one pass through len( bList ) - 1 elements
for index in range( len( bList ) - 1 ):
# if this element is greater than next element,
# swap elements.
if bList[ index ] > bList[ index + 1 ]:
bList[ index ], bList[ index + 1 ] = \
bList[ index + 1 ], bList[ index ]
# main program
bubbleSortList = []
# insert 10 random numbers in the list
for i in range( 1, 11 ):
bubbleSortList.append( random.randrange( 1, 100 ) )
# print unsorted list
print "List before bubble sort:",
for item in bubbleSortList:
print "%2d" % item,
print
# pass list to function bubbleSort (function modifies list)
bubbleSort( bubbleSortList )
# print sorted list
print "List after bubble sort: ",
for item in bubbleSortList:
print "%2d" % item,
print
```
List before bubble sort: 46 29 6 6 95 92 43 73 69 2
List after bubble sort: 2 6 6 29 43 46 69 73 92 95
You might also like to view...
Joe’s Online Pizza Problem Statement
a) Functional Requirements Find all functional requirements and actors in the Pizza Ordering System problem statement above and represent them as a use case diagram. You don’t need to write textual descriptions. b) Non-functional Requirements Write down all non-functional and pseudo requirements in the Pizza Ordering System problem statement, categorized into Usability, Reliability, Performance, Supportability, Implementation, Interface, Operation, Packaging and Legal.
The Import Spreadsheet Wizard gives you the option to change the ________ and data types before you import the data
Fill in the blank(s) with correct word
To begin creating sparklines on a worksheet, click the ________ tab
A) Design B) Insert C) Format D) Chart Tools Design
DSL technology is currently faster than cable or fiber-optics.
Answer the following statement true (T) or false (F)