Javatpoint Logo
Javatpoint Logo

Write the Python Program to Sort an Array According to Other

In 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 Method

Let'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.

  1. In the function, we initialize an empty dictionary called `count` and an empty list called sorted_array.
  2. It then proceeds to count the occurrences of each number in `a1` using a loop. For each number num in a1, it checks if num is already a key in count. If it is, the count for that number is incremented by 1. Otherwise, a new key num is added to count with an initial count of 1.
  3. Next, the code iterates over the elements in a2. For each number num in a2, it checks if num exists in count. If it does, the code extends sorted_array by repeating num the corresponding number of times (as stored in count[num]). It then removes the key `num` from `count` using the de` keyword.
  4. After processing all the elements in a2, the code checks if sorted_array is empty. If it is, it means that none of the elements in a2 were found in a1. In this case, the code assigns the sorted version of a1 to sorted_array using the sorted() function. This step ensures that sorted_array contains the sorted elements of a1 in non-decreasing order.
  5. Finally, the code adds the remaining elements from the `count` dictionary to `sorted_array`. It iterates over the sorted keys of `count` and extends sorted_array by repeating each key num the corresponding number of times (as stored in count[num]).
  6. The sorted array, sorted_array, is then returned as the result of the function.
  7. In the main part of the code, it creates two arrays, `a1` and `a2`, with specific values. It then calls the `sorted_array` function with these arrays as arguments and prints the resulting sorted array.

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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA