Open In App

Python | Integrity Sorting in two lists

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Often during the problem solving we come across too many problems where we need to sort the list. But sometimes we would also want to sort the another list so that the elements of are automatically shifted and remain at same index as the first list even after first list get sorted. Let’s discuss certain ways in which this can be done. 
Method #1 : Using sorted() + zip() + itemgetter() Combining the three functions we can possibly achieve the task. The zip functions binds the two list together, sorted function sorts the list and itemgetter function is used to define the metrics against which we need second list to shift, in this case first list. 
 

Python3




# Python3 code to demonstrate
# integrity sorting in two list
# using sorted() + zip() + itemgetter()
from operator import itemgetter
 
# initializing lists
test_list1 = [3, 4, 9, 1, 6]
test_list2 = [1, 5, 3, 6, 7]
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
 
# using sorted() + zip() + itemgetter()
# integrity sorting in two list
res = [list(x) for x in zip(*sorted(zip(test_list1, test_list2),
                                         key = itemgetter(0)))]
 
# printing result
print ("The lists after integrity sort : " +  str(res))


Output:

The original list 1 is : [3, 4, 9, 1, 6]
The original list 2 is : [1, 5, 3, 6, 7]
The lists after integrity sort : [[1, 3, 4, 6, 9], [6, 1, 5, 7, 3]]

Time Complexity: O(n*nlogn), where n is the length of the list test_list 
Auxiliary Space: O(n*n) additional space of size n is created where n is the number of elements in the res list 

  Method #2 : Using sorted() + zip() + lambda function This method performs the similar task, each function performing the similar function, the difference is just the instead of itemgetter function, lambda function performs the task of assigning a base to sort the list, i.e the first list in this case. 

Python3




# Python3 code to demonstrate
# integrity sorting in two list
# using sorted() + zip() + lambda function
from operator import itemgetter
 
# initializing lists
test_list1 = [3, 4, 9, 1, 6]
test_list2 = [1, 5, 3, 6, 7]
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
 
# using sorted() + zip() + lambda function
# integrity sorting in two list
res = [list(i) for i in zip(*sorted(zip(test_list1, test_list2),
                                  key = lambda dual: dual[0]))]
 
# printing result
print ("The lists after integrity sort : " +  str(res))


Output:

The original list 1 is : [3, 4, 9, 1, 6]
The original list 2 is : [1, 5, 3, 6, 7]
The lists after integrity sort : [[1, 3, 4, 6, 9], [6, 1, 5, 7, 3]]

  Method #3 : Using numpy:

  1. Initialize two arrays, arr1 and arr2, with the values to be sorted.
  2. Use np.argsort() to obtain the sorted indices of the first array, arr1.
  3. Use the sorted indices to sort both arrays, arr1 and arr2, based on the values in arr1.
  4. Return the sorted arrays, arr1_sorted and arr2_sorted.
  5. And here is a brief description of what each line of code in the Python program is doing:
  6. Initialize the two arrays, arr1 and arr2.
  7. Use np.argsort() to obtain the sorted indices of the first array, arr1.
  8. Use the sorted indices to sort both arrays, arr1 and arr2, based on the values in arr1.
  9. Print the sorted arrays, arr1_sorted and arr2_sorted.

Python3




import numpy as np
 
# initializing arrays
arr1 = np.array([3, 4, 9, 1, 6])
arr2 = np.array([1, 5, 3, 6, 7])
  
# printing original lists
print ("The original list 1 is : " + str(arr1))
print ("The original list 2 is : " + str(arr2))
# sorting arrays based on first array
sorted_indices = np.argsort(arr1)
arr1_sorted = arr1[sorted_indices]
arr2_sorted = arr2[sorted_indices]
 
# printing result
print("The arrays after integrity sort:")
print(arr1_sorted)
print(arr2_sorted)
#This code is contributed by Jyothi pinjala


Output:
The original list 1 is : [3 4 9 1 6]
The original list 2 is : [1 5 3 6 7]
The arrays after integrity sort:
[1 3 4 6 9]
[6 1 5 7 3]

Time complexity:

The np.argsort() function has a time complexity of O(n log n) for an array of n elements.
The array indexing operation arr1[sorted_indices] has a time complexity of O(n).
The overall time complexity of the algorithm is O(n log n).
Auxiliary Space:

The algorithm uses extra memory for the sorted indices, sorted_indices, and the sorted arrays, arr1_sorted and arr2_sorted.
The space complexity is O(n) for these arrays.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads