0% found this document useful (0 votes)
2 views4 pages

AOA Lab6

The document provides a Python implementation of the merge sort algorithm for sorting float values using recursion and without recursion. It includes example inputs and outputs demonstrating the sorting process. The implementation passes all tests and is marked as correct.

Uploaded by

saravanan.pv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

AOA Lab6

The document provides a Python implementation of the merge sort algorithm for sorting float values using recursion and without recursion. It includes example inputs and outputs demonstrating the sorting process. The implementation passes all tests and is marked as correct.

Uploaded by

saravanan.pv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Write a Program for Implementing merge sort on float values using python recursion.

For example:

Test Input Result


merge_sort(inp_arr) 5 Input Array:
3.2 [3.2, 1.6, 9.5, 4.3, 4.55]
1.6 Sorted Array:
9.5 [1.6, 3.2, 4.3, 4.55, 9.5]
4.3
4.55

merge_sort(inp_arr) 6 Input Array:


3.2 [3.2, 1.2, 5.3, 9.6, 8.5, 7.4]
1.2 Sorted Array:
5.3 [1.2, 3.2, 5.3, 7.4, 8.5, 9.6]
9.6
8.5
7.4

Answer: (penalty regime: 0 %)


1 def merge_sort(arr):
2 mergeArr(arr,0,len(arr)-1)
3 print("Sorted Array:")
4 print(inp_arr)
5 def mergeArr(arr,l,r):
6 mid=l+(r-l)//2
7 if(l<r):
8 mergeArr(arr,l,mid)
9 mergeArr(arr,mid+1,r)
10 merge(arr,l,mid,r)
11 def merge(arr,l,mid,r):
12 n1=mid-l+1
13 n2=r-mid
14 left=[]
15 right=[]
16 for i in range(0,n1):
17 left.append(arr[i+l])
18 for j in range(n2):
19 right.append(arr[mid+1+j])
20 m=0
21 n=0
22 k=l

Test Input Expected Got


merge_sort(inp_arr) 5 Input Array: Input Array:
3.2 [3.2, 1.6, 9.5, 4.3, 4.55] [3.2, 1.6, 9.5, 4.3, 4.55]
1.6 Sorted Array: Sorted Array:
9.5 [1.6, 3.2, 4.3, 4.55, 9.5] [1.6, 3.2, 4.3, 4.55, 9.5]
4.3
4.55

merge_sort(inp_arr) 6 Input Array: Input Array:


3.2 [3.2, 1.2, 5.3, 9.6, 8.5, 7.4] [3.2, 1.2, 5.3, 9.6, 8.5, 7.4]
1.2 Sorted Array: Sorted Array:
5.3 [1.2, 3.2, 5.3, 7.4, 8.5, 9.6] [1.2, 3.2, 5.3, 7.4, 8.5, 9.6]
9.6
8.5
7.4

merge_sort(inp_arr) 4 Input Array: Input Array:


3.2 [3.2, 1.5, 6.9, 8.0] [3.2, 1.5, 6.9, 8.0]
1.5 Sorted Array: Sorted Array:
6.9 [1.5, 3.2, 6.9, 8.0] [1.5, 3.2, 6.9, 8.0]
8.0
Passed all tests!

Correct
Marks for this submission: 40.00/40.00.
Question 2

Correct

Mark 50.00 out of 50.00

Write a python program to implement merge sort without using recursive function on the given list of values.
For example:

Input Result
7 left: [33]
33 Right: [42]
42 left: [9]
9 Right: [37]
37 left: [8]
8 Right: [47]
47 left: [5]
5 Right: []
left: [33, 42]
Right: [9, 37]
left: [8, 47]
Right: [5]
left: [9, 33, 37, 42]
Right: [5, 8, 47]
[5, 8, 9, 33, 37, 42, 47]

6 left: [10]
10 Right: [3]
3 left: [5]
5 Right: [61]
61 left: [74]
74 Right: [92]
92 left: [3, 10]
Right: [5, 61]
left: [74, 92]
Right: []
left: [3, 5, 10, 61]
Right: [74, 92]
[3, 5, 10, 61, 74, 92]

Answer: (penalty regime: 0 %)


1
2 def merge(left,right):
3 result=[]
4 x,y=0,0
5 for k in range(0,len(left)+len(right)):
6 if x==len(left):
7 result.append(right[y])
8 y+=1
9 elif y==len(right):
10 result.append(left[x])
11 x+=1
12 elif right[y]<left[x]:
13 result.append(right[y])
14 y+=1
15 else:
16 result.append(left[x])
17 x+=1
18 return result
19 def Merge_Sort(S):
20 length=len(S)
21 size=1
22 while size<length:
Input Expected Got
7 left: [33] left: [33]
33 Right: [42] Right: [42]
42 left: [9] left: [9]
9 Right: [37] Right: [37]
37 left: [8] left: [8]
8 Right: [47] Right: [47]
47 left: [5] left: [5]
5 Right: [] Right: []
left: [33, 42] left: [33, 42]
Right: [9, 37] Right: [9, 37]
left: [8, 47] left: [8, 47]
Right: [5] Right: [5]
left: [9, 33, 37, 42] left: [9, 33, 37, 42]
Right: [5, 8, 47] Right: [5, 8, 47]
[5, 8, 9, 33, 37, 42, 47] [5, 8, 9, 33, 37, 42, 47]

6 left: [10] left: [10]


10 Right: [3] Right: [3]
3 left: [5] left: [5]
5 Right: [61] Right: [61]
61 left: [74] left: [74]
74 Right: [92] Right: [92]
92 left: [3, 10] left: [3, 10]
Right: [5, 61] Right: [5, 61]
left: [74, 92] left: [74, 92]
Right: [] Right: []
left: [3, 5, 10, 61] left: [3, 5, 10, 61]
Right: [74, 92] Right: [74, 92]
[3, 5, 10, 61, 74, 92] [3, 5, 10, 61, 74, 92]

5 left: [4] left: [4]


4 Right: [12] Right: [12]
12 left: [6] left: [6]
6 Right: [98] Right: [98]
98 left: [3] left: [3]
3 Right: [] Right: []
left: [4, 12] left: [4, 12]
Right: [6, 98] Right: [6, 98]
left: [3] left: [3]
Right: [] Right: []
left: [4, 6, 12, 98] left: [4, 6, 12, 98]
Right: [3] Right: [3]
[3, 4, 6, 12, 98] [3, 4, 6, 12, 98]

Passed all tests!

Correct
Marks for this submission: 50.00/50.00.

You might also like