Skip to content

Commit a2f8d74

Browse files
author
Joseph Luce
authored
Update 912_sort_an_array.md
1 parent 5917fc2 commit a2f8d74

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

leetcode/medium/912_sort_an_array.md

+20
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,23 @@ class Solution:
109109
110110
return merge_sort(nums)
111111
```
112+
113+
## Bubble Sort
114+
115+
- Runtime: O(N^2)
116+
- Space: O(1)
117+
- N = Number of elements in list
118+
119+
Each time we perform a bubble sort, we are essential pushing the max value of the array to the back of the array.
120+
121+
The advantage of bubble sort is the lack of memory or constant memory space.
122+
123+
```
124+
class Solution:
125+
def sortArray(self, nums: List[int]) -> List[int]:
126+
for i in reversed(range(0, len(nums))):
127+
for curr in range(0, i):
128+
if nums[curr] > nums[curr+1]:
129+
nums[curr], nums[curr+1] = nums[curr+1], nums[curr]
130+
return nums
131+
```

0 commit comments

Comments
 (0)