File tree 2 files changed +15
-16
lines changed
2 files changed +15
-16
lines changed Original file line number Diff line number Diff line change 2
2
3
3
4
4
# 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
13
13
Original file line number Diff line number Diff line change 4
4
# Best: O(n^2) time | O(1) space
5
5
# Avarage: O(n^2) time | O(1) space
6
6
# Worst: O(n^2) time | O(1) space
7
- def selection_sort ( input_array ):
7
+ def selectionSort ( array ):
8
8
current_index = 0
9
- while current_index < len (input_array ) - 1 :
9
+ while current_index < len (array ) - 1 :
10
10
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 ]:
13
13
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
18
17
19
18
def swap (i , j , array ):
20
19
array [i ], array [j ] = array [j ], array [i ]
You can’t perform that action at this time.
0 commit comments