Given a list as input to the Python program your task is to accept the two list containing integers and merge the two lists, ort the new list and then in the sorted list as output.
let's use bubble sort for sorting the list
Write a Python Program to Merge Two Lists And Sort it
Let's take an integer variable length1 and ask a user to enter the first list length, iterate up to the first list length using the following and ask a user to enter the list item studies list of integers after the user enters the list integers append to the list1.
length1 = int(input("Enter List1 Length : "))
list1 = []
for _ in range(length1):
temp = int(input("Enter List1 Item (Integer): "))
list1.append(temp)
Take another integer variable length to and ask user to enter the list2 length, once is the user enters the list2 length, iterate up to list2 length using for loop and ask a user to enter the list2 integers and append to a list2
length2 = int(input("Enter List2 Length : "))
list2 = []
for _ in range(length2):
temp = int(input("Enter List2 Item (Integer): "))
list2.append(temp)
Once we get the list1 and list2 containing the integers use the inbuilt function extent to add lists to one.
print("New List After Merging Two List is : ")
list1.extend(list2)
print(list1)
Once we get the two lists merged into one, use the Bubble sort to to sort the list of integers. using the for loop iterate aap to list one length and using another for loop iterate the list elements up to to reverse for loop index.
Within the two for loops check if the next item is greater, then swap the items
n = len(list1)
for index in range(n):
for next in range(0, n - index - 1):
if list1[next] > list1[next + 1]:
list1[next], list1[next + 1] = list1[next + 1], list1[next]
Once the for loop ends you will be having the list1 having the sorted items in the ascending order using the Bubble sort print the list1
print("\nThe sorted list is …")
print(list1)
Complete Python program
===========================
length1 = int(input("Enter List1 Length : "))
list1 = []
for _ in range(length1):
temp = int(input("Enter List1 Item (Integer): "))
list1.append(temp)
length2 = int(input("Enter List2 Length : "))
list2 = []
for _ in range(length2):
temp = int(input("Enter List2 Item (Integer): "))
list2.append(temp)
print("New List After Merging Two List is : ")
list1.extend(list2)
print(list1)
n = len(list1)
for index in range(n):
for next in range(0, n - index - 1):
if list1[next] > list1[next + 1]:
list1[next], list1[next + 1] = list1[next + 1], list1[next]
print("\nThe sorted list is …")
print(list1)
Output
=================
Enter List1 Length : 3
Enter List1 Item (Integer): 1
Enter List1 Item (Integer): 2
Enter List1 Item (Integer): 1
Enter List2 Length : 4
Enter List2 Item (Integer): 4
Enter List2 Item (Integer): 3
Enter List2 Item (Integer): 2
Enter List2 Item (Integer): 1
New List After Merging Two List is :
[1, 2, 1, 4, 3, 2, 1]
The sorted list is …
[1, 1, 1, 2, 2, 3, 4]
Output
====================
Enter List1 Length : 5
Enter List1 Item (Integer): 55
Enter List1 Item (Integer): 44
Enter List1 Item (Integer): 33
Enter List1 Item (Integer): 22
Enter List1 Item (Integer): 11
Enter List2 Length : 3
Enter List2 Item (Integer): 66
Enter List2 Item (Integer): 77
Enter List2 Item (Integer): 88
New List After Merging Two List is :
[55, 44, 33, 22, 11, 66, 77, 88]
The sorted list is …
[11, 22, 33, 44, 55, 66, 77, 88]
Conclusion
=======================
Execute the above program and provide the random integers as input to the Python program on the results. Comment down below if you have any suggestions to above program
0 Comments