
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to create a sorted merged list of two unsorted list
Here two user input list is given, the elements of two lists are unsorted. Our task is to merged these two unsorted array and after that sort the list.
Example
Input: A [] = {100, 50, 150} B [] = {200, 30, 20} Output: Merge List:{20, 30, 50, 100, 150, 200}
Algorithm
Step 1: first we create two user input list. Step 2: Final merge list size is (size of the first list + the size of the second list). Step 3: then sort two list using sort() method. Step 4: Merge two sorted list and store it into a third list. Step 5: Merging remaining elements of a[] (if any).Merging remaining elements of b[] (if any). Step 6: display merged sorted list.
Example Code
# Python program to merge two unsorted lists # in sorted order # Function to merge array in sorted order def unsortedarray (a, b, res, n, m): # Sorting a[] and b[] a.sort() b.sort() # Merge two sorted arrays into res[] i, j, k = 0, 0, 0 while (i < n and j < m): if (a[i] <= b[j]): res[k] = a[i] i += 1 k += 1 else: res[k] = b[j] j += 1 k += 1 while (i < n): # Merging remaining # elements of a[] (if any) res[k] = a[i] i += 1 k += 1 while (j < m): # Merging remaining # elements of b[] (if any) res[k] = b[j] j += 1 k += 1 # Driver code A=list() n=int(input("Enter the size of the First List ::")) print("Enter the Element of First List ::") for i in range(int(n)): k=int(input("")) A.append(k) B=list() m=int(input("Enter the size of the Second List ::")) print("Enter the Element of Second List ::") for i in range(int(n)): k=int(input("")) B.append(k) # Final merge list res = [0 for i in range(n + m)] unsortedarray(A, B, res, n, m) print ("Sorted merged list :") for i in range(n + m): print (res[i],)
Output
Enter the size of the First List: 4 Enter the Element of First List:: 8 79 56 3 Enter the size of the Second List: 4 Enter the Element of Second List:: 67 1 9 45 Sorted merged list: 1 3 8 9 45 56 67 79
- Related Articles
- Python program to create a sorted merged list of two unsorted lists
- Java program to create a sorted merged array of two unsorted arrays
- Program to merge two sorted list to form larger sorted list in Python
- Python program to insert an element into sorted list
- Python program to create 3D list.
- How to generate a sorted list in Python?
- Python Program to Create a Linked List & Display the Elements in the List
- Convert list of string into sorted list of integer in Python
- Program to find the number of unique integers in a sorted list in Python
- Program to check sum of two numbers is up to k from sorted List or not in Python
- Program to find squared elements list in sorted order in Python
- Python program to create and display a Circular Linked List
- Python program to create and display a doubly linked list
- C++ Program to Implement Sorted Doubly Linked List
- C++ Program to Implement Sorted Singly Linked List

Advertisements