Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
HW Descending Order Final
#python 3.5.2 print ("Hello, world!") #-------------Bubble Sort------------------- def bubbleSort(alist): length = len(alist) for passnumber in range(0, length): # number of passes = N - 1 print ('Passnumber: ', passnumber) for i in range((length-1) ,0,-1 ): # loop for bubbles print ('Possible values for i:',i) if alist[i] > alist[i-1]: temp = alist[i] # temp = 89 alist[i] = alist[i-1] # 89 with 5 alist[i-1] = temp # 89 => 5 print (i,'-->',alist) print () print ('------------------------------------------------') return alist #------------SelectionSort-------------------- def selectionSort(alist): length = len(alist) for nLastPosition in range(length-1, 0, -1): # nLastPosition -> 5 -> 4 -> 3 -> 2 -> 1 # this loop goes from last position to first position # 1: identifying the maximum number [come back to this] minIndex = 0 for numIndex in range(0, nLastPosition+1): if alist[numIndex] < alist[minIndex]: minIndex = numIndex print ("Min number: ", alist[minIndex]) temp = alist[nLastPosition] alist[nLastPosition] = alist[minIndex] alist[minIndex] = temp print ("After swap: ", alist) print () return alist #---------InsertionSort------------------------ def InsertionSort(alist): length = len(alist) for position in range(1, length): # 1, 2, 3 currentValue = alist[position] # = {17} while position > 0 and alist[position-1] < currentValue: alist[position] = alist[position-1] position = position - 1 # 3->2 alist[position] = currentValue print ('Updating: ', alist) return alist #---------------------------------------------- sampleList = [1, 5, 89, 8, 23, 1] sortedList = bubbleSort(sampleList) print (sortedList)
run
|
edit
|
history
|
help
0
Task1.py
binary search
P
Game2
Saurabh
reverse the number
print path module python
functions add
StringManipulation
LinkedNode and its use in Stack and Queue implementation