File tree Expand file tree Collapse file tree 2 files changed +15
-16
lines changed
Expand file tree Collapse file tree 2 files changed +15
-16
lines changed Original file line number Diff line number Diff line change 22
33
44# O(n) time | O(1) space - where n is the length of the input array
5- def kadanes_algorithm (array ):
6- max_ending_here = array [0 ]
7- max_so_far = array [ 0 ]
8- for i in range ( 1 , len ( array )):
9- num = array [ i ]
10- max_ending_here = max (num , max_ending_here + num )
11- max_so_far = max (max_so_far , max_ending_here )
12- return max_so_far
5+ def kadanesAlgorithm (array ):
6+ maxEdingHere , maxSoFar = array [ 0 ], array [0 ]
7+ for i in range ( 1 , len ( array )):
8+ num = array [ i ]
9+ maxEdingAtPrevIdx = maxEdingHere
10+ maxEdingHere = max (num , num + maxEdingAtPrevIdx )
11+ maxSoFar = max (maxSoFar , maxEdingHere )
12+ return maxSoFar
1313
Original file line number Diff line number Diff line change 44# Best: O(n^2) time | O(1) space
55# Avarage: O(n^2) time | O(1) space
66# Worst: O(n^2) time | O(1) space
7- def selection_sort ( input_array ):
7+ def selectionSort ( array ):
88 current_index = 0
9- while current_index < len (input_array ) - 1 :
9+ while current_index < len (array ) - 1 :
1010 smallest_index = current_index
11- for i in range (current_index + 1 , len (input_array )):
12- if input_array [i ] < input_array [ current_index ]:
11+ for i in range (current_index + 1 , len (array )):
12+ if array [i ] < array [ smallest_index ]:
1313 smallest_index = i
14- swap (current_index , smallest_index , input_array )
15- current_index += 1
16- return input_array
17-
14+ swap (current_index , smallest_index , array )
15+ current_index += 1
16+ return array
1817
1918def swap (i , j , array ):
2019 array [i ], array [j ] = array [j ], array [i ]
You can’t perform that action at this time.
0 commit comments