Write the Python Program to Sort an Array According to OtherIn this tutorial, we will write the Python program to sort an array according other given array. In this problem, we are given a two arrays size on N and M, we need to sort the first array such that all relative positions of the elements in the first array are the same as the elements in the second array. Let's see the following example - Example - 1 Input: Output: [5, 5, 3, 3, 1, 1, 1, 2, 9] Explanation: The first array elements of a1 are sorted according to the position of element a2. So 5 comes first then 3 comes, then comes 1, then finally 2 comes, now we append remaining elements in sorted order. Example - 2: Input: Output: [1, 1, 2, 2, 3, 3] Explanation - Since there are no elements in a1 that are present in a2, we cannot sort a1 according to a2. As a result, we sort the elements in a1 in non-decreasing order. Let's move to the solution part. Solution - Brute Force MethodLet's understand the following code snippet - Example - Output: [5, 5, 3, 3, 1, 1, 1, 2, 9] Explanation - In the above code, we define a function named sorted_array() that takes two arrays, a1 and a2, as input. The function aims to sort the elements in a1 according to the order specified by a2.
Method - 2:Let's understand another example. Example - Output: [1, 1, 1, 2, 3, 3, 5, 5, 9] Explanation - In the above code, we define sort_array() function takes two arguments: arr (the array to be sorted) and order (the reference array used for sorting). In the sort_array function, we create a dictionary called order_dict to store the indices of elements in the order array. This dictionary will help us determine the sorting order for each element in arr. We define a custom sorting key function called custom_key, which takes an element from arr as an argument. This function will be used by the sorted function to determine the sorting order. Inside the custom_key function, we first check if the element exists in the order_dict dictionary. If it does, we return the corresponding index from the order array. This ensures that elements from arr that match the order in order will be sorted accordingly. If the element is not found in the order_dict dictionary, it means there is no match with the order array. In this case, we assign a higher index to the element by adding len(order) to it. This ensures that unmatched elements from arr are sorted after the matched elements, in non-decreasing order. Finally, we use the sorted function to sort the arr array using the custom_key function as the sorting key. The sorted array is stored in the sorted_arr variable. The sorted_arr is then returned from the sort_array function. Finally, we print the sorted_a1 array, which contains a1 sorted according to the order of a2, with unmatched elements sorted in non-decreasing order. Next TopicAll Nodes at Distance K Python Solution |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India