Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
H.W.5 ALL Solutions
#python 3.5.2 print ("Hello, world!") #----------------------------H.W 5--------------------------------------------------------------------------- #----------------------------Bubble sort in descending order (for loop)------------------------------------- def bubbleSort(alist): length = len(alist) for passnumber in range(length-1,0,-1): print ('Passnumber: ', passnumber) for i in range((length-1) ,0,-1 ): print ('Possible values for i:',i) if alist[i-1] > alist[i]: temp = alist[i-1] alist[i-1] = alist[i] alist[i] = temp print (i,'-->',alist) print () print ('------------------------------------------------') return alist sampleList = [100, 5, 105, 89, 8, 23, 1] print (sampleList) sortedList = bubbleSort(sampleList) print (sortedList) #-----------------------Bubble sort in descending order (while)------------------------------------- def x(y): length=len(y) p=length-1 while p>0: print ('p: ', p) i=length-1 while i>0: print ('Possible values for i:',i) if y[i-1] > y[i]: temp = y[i-1] y[i-1] = y[i] y[i] = temp print (i,'-->',y) print () i=i-1 p=p-1 print ('------------------------------------------------') return y Input=[3,1,1,5,2] print('y=',Input) R=x(Input) print('R=',R) #-----------------------Selection sort in descending order (for)------------------------------------- def selectionSort(alist): length = len(alist) # -> 6 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] maxIndex = 0 for numIndex in range(0, nLastPosition+1): if alist[numIndex] > alist[maxIndex]: maxIndex = numIndex print ("Max number: ", alist[maxIndex]) temp = alist[nLastPosition] alist[nLastPosition] = alist[maxIndex] alist[maxIndex] = temp print ("After swap: ", alist) print () return alist alist = [26,54,93,17,77,31] # length = 6 sortList = selectionSort(alist) print (sortList) #-----------------------Selection sort in descending order (while)------------------------------------- def SelectionSort(y): length=len(y) for position in range(length-1,0,-1): maxy=position # not convinced for numby in range (0, position+1): if y[numby] > y[maxy]: maxy = numby print ("Max number: ", y[maxy]) temp = y[position] y[position] = y[maxy] y[maxy] = temp print ("After swap: ", y) print () return y s=[9,8,30,41,0,1,40,6] r=SelectionSort(s) print('r',r) #-----------------------Try to (roughly) implement Insertion sort---------------------------------------
run
|
edit
|
history
|
help
0
teretere
Ball
playing around with python
pyCustomEnum
gj
Random module
PyClosure
числа близнецы
Insertion Sort
My webapp demo