Skip to content

Commit 5cbdc87

Browse files
authored
Create MergeSort.py
1 parent 12ae100 commit 5cbdc87

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

MergeSort.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
def merge(left_list, right_list):
2+
sorted_list = []
3+
left_list_index = right_list_index = 0
4+
5+
left_list_length, right_list_length = len(left_list), len(right_list)
6+
7+
for _ in range(left_list_length + right_list_length):
8+
if left_list_index < left_list_length and right_list_index < right_list_length:
9+
if left_list[left_list_index] <= right_list[right_list_index]:
10+
sorted_list.append(left_list[left_list_index])
11+
left_list_index += 1
12+
else:
13+
sorted_list.append(right_list[right_list_index])
14+
right_list_index += 1
15+
16+
elif left_list_index == left_list_length:
17+
sorted_list.append(right_list[right_list_index])
18+
right_list_index += 1
19+
20+
elif right_list_index == right_list_length:
21+
sorted_list.append(left_list[left_list_index])
22+
left_list_index += 1
23+
24+
return sorted_list
25+
26+
27+
def merge_sort(nums):
28+
29+
if len(nums) <= 1:
30+
return nums
31+
32+
mid = len(nums) // 2
33+
34+
left_list = merge_sort(nums[:mid])
35+
right_list = merge_sort(nums[mid:])
36+
37+
return merge(left_list, right_list)
38+
A=[]
39+
B=int(input("enter B-"))
40+
n=int(input("enter the number of houses "))
41+
for i in range(n):
42+
A.append(int(input("enter the cost of the house-")))
43+
C = merge_sort(A)
44+
su=0
45+
ct=0
46+
while(su<B):
47+
su=su+C[ct]
48+
ct+=1
49+
print(ct-1)

0 commit comments

Comments
 (0)