-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble sort.py
More file actions
28 lines (24 loc) · 797 Bytes
/
bubble sort.py
File metadata and controls
28 lines (24 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution(object):
def sortedSquares(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
# Step 1: Square each number
for i in range(len(nums)):
nums[i] = nums[i] * nums[i]
# Step 2: Sort the squared numbers using Bubble Sort
n = len(nums)
for i in range(n):
for j in range(0, n - i - 1):
if nums[j] > nums[j + 1]:
nums[j], nums[j + 1] = nums[j + 1], nums[j]
return nums
class main:
def main():
solution = Solution()
nums = [-4, -1, 0, 3, 10]
result = solution.sortedSquares(nums)
print(result) # Output: [0, 1, 9, 16, 100]
if __name__ == "__main__":
main()