Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Faiha #4
# python 3.5.2 # https://1drv.ms/f/s!ArsHV2FdZgQ1iQOclKKq2kqbxLdg # Function definitions # MATLAB # function [output] = sum(num1, num2) # output = num1 + num2 # end # a = 50 # b = 30 # result = sum(a, b) # Function for addition def simpleSum(a, b): return a + b # function 1 for subtraction def simpleSub(x,y): return x-y # ----------------------------- # ----- Lesson 4 - Sorting ---- # ----------------------------- # 1. Bubble Sort (look at two values) # original modified list checking in original list # Passing through the entire list for first time # [21, 5, 17, 9 ] -> [ 5, 21, 17, 9 ] # check (21,5) # [ 5, 21, 17, 9 ] -> [ 5, 17, 21, 9 ] # check (21,17) # [ 5, 17, 21, 9 ] -> [ 5, 17, 9, 21 ] # check (21,9) # Pass through the list once again for second time # [ 5, 17, 9, 21 ] -> [ 5, 17, 9, 21] # check (5, 17) # [ 5, 17, 9, 21 ] -> [ 5, 9, 17, 21] # check (17, 9) # [ 5, 9, 17, 21 ] -> [ 5, 9, 17, 21] # check (17,21) # Investigated swapping technique # challenge - a = [ 1, 7, 3] (hint - maybe use a temporary variable) # temp = a[0] # value 7 and storing it on a temporary variable # a[0] = a[1] # a[1] = temp def bubbleSort(alist): # do all operations here length = len(alist) # length = 4 (in this example) for passnumber in range(0, length): # number of passes = N - 1 print ('Passnumber: ', passnumber) for i in range(0, (length-1) - passnumber ): # 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 sampleList = [1, 5, 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) # Homework - Do bubble sort algorithm using WHILE LOOP
run
|
edit
|
history
|
help
0
Python virables
abc
7 Segment Display
Country Flags
mansur
Homework exercise
Ok
inerse_matrix_gauss_jordan
Calculate Volume of Sphere
word repeat