Ex 10

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Ex 10: Merge Sort Chocolates

To sort the chocolates in the supermarket based on its cost and size in ascending order
using merge sort algorithm.
ALGORITHM:
Input: List of chocolates with their cost and size
Output: Sorted list of chocolates in ascending order based on cost

1. Procedure merge_sort_chocolates(chocolates):
a. If the length of chocolates is greater than 1:
i. Find the middle index (mid) of the chocolates list.
ii. Split the chocolates list into left_half (from the beginning to mid) and right_half (from
mid to the end).
iii. Recursively call merge_sort_chocolates on left_half.
iv. Recursively call merge_sort_chocolates on right_half.
v. Call merge to merge the sorted left_half and right_half into the original chocolates list.

2. Procedure merge(chocolates, left_half, right_half):


a. Initialize three indices i, j, and k to 0, which correspond to the indices of chocolates,
left_half, and right_half.
b. While i is less than the length of left_half and j is less than the length of right_half:
i. If left_half[i]['cost'] is less than or equal to right_half[j]['cost']:
- Set chocolates[k] to left_half[i].
- Increment i and k.
ii. Else:
- Set chocolates[k] to right_half[j].
- Increment j and k.
c. While i is less than the length of left_half:
- Set chocolates[k] to left_half[i].
- Increment i and k.
d. While j is less than the length of right_half:
- Set chocolates[k] to right_half[j].
- Increment j and k.

3. Example usage:
a. Initialize a list of chocolates with dictionaries containing 'name', 'cost', and 'size'.
b. Print the list before sorting.
c. Call merge_sort_chocolates with the list of chocolates.
d. Print the list after sorting in ascending order based on cost.

PROGRAM:
def merge_sort_chocolates(chocolates):
if len(chocolates) > 1:
mid = len(chocolates) // 2
left_half = chocolates[:mid]
right_half = chocolates[mid:]

merge_sort_chocolates(left_half)
merge_sort_chocolates(right_half)

merge(chocolates, left_half, right_half)

def merge(chocolates, left_half, right_half):


i=j=k=0

while i < len(left_half) and j < len(right_half):


if left_half[i]['cost'] <= right_half[j]['cost']:
chocolates[k] = left_half[i]
i += 1
else:
chocolates[k] = right_half[j]
j += 1
k += 1

while i < len(left_half):


chocolates[k] = left_half[i]
i += 1
k += 1

while j < len(right_half):


chocolates[k] = right_half[j]
j += 1
k += 1

# Example usage:
if __name__ == "__main__":
chocolates = [
{'name': 'Milk Chocolate', 'cost': 2.50, 'size': 'Small'},
{'name': 'Dark Chocolate', 'cost': 3.00, 'size': 'Medium'},
{'name': 'White Chocolate', 'cost': 2.00, 'size': 'Large'},
{'name': 'Assorted Chocolates', 'cost': 4.50, 'size': 'Small'}
]

print("Before sorting:")
for chocolate in chocolates:
print(chocolate)

merge_sort_chocolates(chocolates)

print("\nAfter sorting in ascending order based on cost:")


for chocolate in chocolates:
print(chocolate)
OUTPUT:
Before sorting:
{'name': 'Milk Chocolate', 'cost': 2.5, 'size': 'Small'}
{'name': 'Dark Chocolate', 'cost': 3.0, 'size': 'Medium'}
{'name': 'White Chocolate', 'cost': 2.0, 'size': 'Large'}
{'name': 'Assorted Chocolates', 'cost': 4.5, 'size': 'Small'}

After sorting in ascending order based on cost:


{'name': 'White Chocolate', 'cost': 2.0, 'size': 'Large'}
{'name': 'Milk Chocolate', 'cost': 2.5, 'size': 'Small'}
{'name': 'Dark Chocolate', 'cost': 3.0, 'size': 'Medium'}
{'name': 'Assorted Chocolates', 'cost': 4.5, 'size': 'Small'}

You might also like