Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 157138a

Browse files
committed
Create sort_an_array.py
1 parent b7eca88 commit 157138a

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

‎arrays_hashing/sort_an_array.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
"""
2+
Problem: Sort an Array
3+
4+
You are given an array of integers `nums`. Your task is to sort the array in ascending order and return it.
5+
6+
Requirements:
7+
- You must not use any built-in sorting functions.
8+
- Your solution should run in O(n log n) time complexity.
9+
- Your solution should use the smallest possible space complexity.
10+
11+
Example 1:
12+
Input: nums = [10, 9, 1, 1, 1, 2, 3, 1]
13+
Output: [1, 1, 1, 1, 2, 3, 9, 10]
14+
15+
Example 2:
16+
Input: nums = [5, 10, 2, 1, 3]
17+
Output: [1, 2, 3, 5, 10]
18+
19+
Constraints:
20+
- 1 <= nums.length <= 50,000
21+
- -50,000 <= nums[i] <= 50,000
22+
"""
23+
from typing import List
24+
# Method 1:
25+
class Solution:
26+
def sortArray(self, nums: List[int]) -> List[int]:
27+
28+
n = len(nums)
29+
30+
for i in range(n-1):
31+
for j in range(n-i-1):
32+
33+
if nums[j] > nums[j+1]:
34+
nums[j] , nums[j+1] = nums[j+1] , nums[j]
35+
36+
return nums
37+
38+
# Method 2:
39+
class Solution:
40+
def sortArray(self, nums: List[int]) -> List[int]:
41+
42+
def merge(left, right):
43+
new = []
44+
i = 0
45+
j = 0
46+
47+
while i < len(left) and j < len(right):
48+
if left[i] < right[j]:
49+
new.append(left[i])
50+
i += 1
51+
else:
52+
new.append(right[j])
53+
j += 1
54+
55+
new.extend(left[i:])
56+
new.extend(right[j:])
57+
return new
58+
59+
def merge_sort(nums: List[int]):
60+
if len(nums) <= 1:
61+
return nums
62+
63+
mid = len(nums) // 2
64+
left_arr = nums[:mid]
65+
right_arr = nums[mid:]
66+
67+
left_arr = merge_sort(left_arr)
68+
right_arr = merge_sort(right_arr)
69+
return merge(left_arr, right_arr)
70+
71+
return merge_sort(nums)
72+
73+
# Method 3:
74+
class Solution:
75+
def sortArray(self, nums: List[int]) -> List[int]:
76+
77+
def merge_sort(nums):
78+
79+
if len(nums) == 1:
80+
return nums
81+
82+
mid = len(nums) // 2
83+
left_arr = nums[:mid]
84+
right_arr = nums[mid:]
85+
86+
left_arr = merge_sort(left_arr)
87+
right_arr = merge_sort(right_arr)
88+
89+
merge(left_arr,right_arr,nums)
90+
return nums
91+
92+
def merge(left,right,nums):
93+
i = 0
94+
j = 0
95+
k = 0
96+
97+
while i < len(left) and j < len(right):
98+
if left[i] < right[j]:
99+
nums[k] = left[i]
100+
i += 1
101+
else:
102+
nums[k] = right[j]
103+
j += 1
104+
105+
k +=1
106+
107+
while i < len(left):
108+
nums[k] = left[i]
109+
i += 1
110+
k += 1
111+
112+
while j < len(right):
113+
nums[k] = right[j]
114+
j += 1
115+
k += 1
116+
117+
return merge_sort(nums)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /