-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathMergeSort.py
More file actions
49 lines (38 loc) · 1.35 KB
/
MergeSort.py
File metadata and controls
49 lines (38 loc) · 1.35 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def merge(left_list, right_list):
sorted_list = []
left_list_index = right_list_index = 0
left_list_length, right_list_length = len(left_list), len(right_list)
for _ in range(left_list_length + right_list_length):
if left_list_index < left_list_length and right_list_index < right_list_length:
if left_list[left_list_index] <= right_list[right_list_index]:
sorted_list.append(left_list[left_list_index])
left_list_index += 1
else:
sorted_list.append(right_list[right_list_index])
right_list_index += 1
elif left_list_index == left_list_length:
sorted_list.append(right_list[right_list_index])
right_list_index += 1
elif right_list_index == right_list_length:
sorted_list.append(left_list[left_list_index])
left_list_index += 1
return sorted_list
def merge_sort(nums):
if len(nums) <= 1:
return nums
mid = len(nums) // 2
left_list = merge_sort(nums[:mid])
right_list = merge_sort(nums[mid:])
return merge(left_list, right_list)
A=[]
B=int(input("enter B-"))
n=int(input("enter the number of houses "))
for i in range(n):
A.append(int(input("enter the cost of the house-")))
C = merge_sort(A)
su=0
ct=0
while(su<B):
su=su+C[ct]
ct+=1
print(ct-1)