Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
H.W5
#python 3.5.2 print ("Hello, world!") #----------------------------H.W 5--------------------------------------------------------------------------- #----------------------------Bubble sort in descending order (for loop)------------------------------------- def bubbleSort(alist): # do all operations here length = len(alist) # length = 4 (in this example) for passnumber in range(length-1,0,-1): # 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-1] > alist[i]: temp = alist[i-1] # temp = 89 alist[i-1] = alist[i] # 89 with 5 alist[i] = temp # 89 => 5 # print (i,'-->',alist) # print () #print ('------------------------------------------------') return alist sampleList = [100, 5, 105, 89, 8, 23, 1] # Length = N # pass = N - 1 times # pass 1 - [--, --, --, 89] # pass 2 - [--, --, 24, 89] # pass 3 - [--, 8, 24, 89] print (sampleList) sortedList = bubbleSort(sampleList) print (sortedList) #-----------------------Bubble sort in descending order (while)------------------------------------- # def x(y): # length=len(y) # p=length-1 # while p<length: # print ('p: ', p) # i=length-1 # while i<((length-1)): # 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) # i=i-1 # print () # 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(alist): length = len(alist) # -> 6 nLastPosition=0 while nLastPosition > 0: maxIndex = 0 while numIndex < (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 () numIndex=numIndex-1 nLastPosition=nLastPosition-1 return alist alist = [26,54,93,17,77,31] # length = 6 sortList = selectionSort(alist) print (sortList)
run
|
edit
|
history
|
help
0
public,protec,pvt
Python. Even
#converting list into set
Hello world
Test
for
CalcV3 with PLY
Hello wold Happy
Counting
try1