Skip to content
Related Articles
Sort an array according to the order defined by another array
• Difficulty Level : Medium
• Last Updated : 29 May, 2021

Given two arrays A1[] and A2[], sort A1 in such a way that the relative order among the elements will be same as those are in A2. For the elements not present in A2, append them at last in sorted order.
Example:

```Input: A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
Output: A1[] = {2, 2, 1, 1, 8, 8, 3, 5, 6, 7, 9}```

The code should handle all cases like the number of elements in A2[] may be more or less compared to A1[]. A2[] may have some elements which may not be there in A1[] and vice versa is also possible.

## We strongly recommend that you click here and practice it, before moving on to the solution.

Method 1 (Using Sorting and Binary Search)
Let the size of A1[] be m and the size of A2[] be n.

• Create a temporary array temp of size m and copy the contents of A1[] to it.
• Create another array visited[] and initialize all entries in it as false. visited[] is used to mark those elements in temp[] which are copied to A1[].
• Sort temp[]
• Initialize the output index ind as 0.
• Do following for every element of A2[i] in A2[]
• Binary search for all occurrences of A2[i] in temp[], if present then copy all occurrences to A1[ind] and increment ind. Also mark the copied elements visited[]
• Copy all unvisited elements from temp[] to A1[]

Below image is a dry run of the above approach: Below is the implementation of the above approach:

## C++

 `// A C++ program to sort an array according to the order defined``// by another array``#include ``using` `namespace` `std;` `// A Binary Search based function to find index of FIRST occurrence``// of x in arr[].  If x is not present, then it returns -1` `// The same can be done using the lower_bound``// function in C++ STL``int` `first(``int` `arr[], ``int` `low, ``int` `high, ``int` `x, ``int` `n)``{` `    ``// Checking condition``    ``if` `(high >= low) {` `        ``// FInd the mid element``        ``int` `mid = low + (high - low) / 2;` `        ``// Check if the element is the extreme left``        ``// in the left half of the array``        ``if` `((mid == 0 || x > arr[mid - 1]) && arr[mid] == x)``            ``return` `mid;` `        ``// If the element lies on the right half``        ``if` `(x > arr[mid])``            ``return` `first(arr, (mid + 1), high, x, n);` `        ``// Check for element in the left half``        ``return` `first(arr, low, (mid - 1), x, n);``    ``}` `    ``// ELement not found``    ``return` `-1;``}` `// Sort A1[0..m-1] according to the order defined by A2[0..n-1].``void` `sortAccording(``int` `A1[], ``int` `A2[], ``int` `m, ``int` `n)``{``    ``// The temp array is used to store a copy of A1[] and visited[]``    ``// is used mark the visited elements in temp[].``    ``int` `temp[m], visited[m];``    ``for` `(``int` `i = 0; i < m; i++) {``        ``temp[i] = A1[i];``        ``visited[i] = 0;``    ``}` `    ``// Sort elements in temp``    ``sort(temp, temp + m);` `    ``// for index of output which is sorted A1[]``    ``int` `ind = 0;` `    ``// Consider all elements of A2[], find them in temp[]``    ``// and copy to A1[] in order.``    ``for` `(``int` `i = 0; i < n; i++) {``        ``// Find index of the first occurrence of A2[i] in temp``        ``int` `f = first(temp, 0, m - 1, A2[i], m);` `        ``// If not present, no need to proceed``        ``if` `(f == -1)``            ``continue``;` `        ``// Copy all occurrences of A2[i] to A1[]``        ``for` `(``int` `j = f; (j < m && temp[j] == A2[i]); j++) {``            ``A1[ind++] = temp[j];``            ``visited[j] = 1;``        ``}``    ``}` `    ``// Now copy all items of temp[]``    ``// which are not present in A2[]``    ``for` `(``int` `i = 0; i < m; i++)``        ``if` `(visited[i] == 0)``            ``A1[ind++] = temp[i];``}` `// Utility function to print an array``void` `printArray(``int` `arr[], ``int` `n)``{` `    ``// Iterate in the array``    ``for` `(``int` `i = 0; i < n; i++)``        ``cout << arr[i] << ``" "``;``    ``cout << endl;``}` `// Driver Code``int` `main()``{``    ``int` `A1[] = { 2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8 };``    ``int` `A2[] = { 2, 1, 8, 3 };``    ``int` `m = ``sizeof``(A1) / ``sizeof``(A1);``    ``int` `n = ``sizeof``(A2) / ``sizeof``(A2);` `    ``// Prints the sorted array``    ``cout << ``"Sorted array is \n"``;``    ``sortAccording(A1, A2, m, n);``    ``printArray(A1, m);``    ``return` `0;``}`

## Java

 `// A JAVA program to sort an array according``// to the order defined by another array``import` `java.io.*;``import` `java.util.Arrays;` `class` `GFG {` `    ``/* A Binary Search based function to find``    ``index of FIRST occurrence of x in arr[].``    ``If x is not present, then it returns -1 */``    ``static` `int` `first(``int` `arr[], ``int` `low, ``int` `high,``                     ``int` `x, ``int` `n)``    ``{``        ``if` `(high >= low) {``            ``/* (low + high)/2; */``            ``int` `mid = low + (high - low) / ``2``;` `            ``if` `((mid == ``0` `|| x > arr[mid - ``1``]) && arr[mid] == x)``                ``return` `mid;``            ``if` `(x > arr[mid])``                ``return` `first(arr, (mid + ``1``), high,``                             ``x, n);``            ``return` `first(arr, low, (mid - ``1``), x, n);``        ``}``        ``return` `-``1``;``    ``}` `    ``// Sort A1[0..m-1] according to the order``    ``// defined by A2[0..n-1].``    ``static` `void` `sortAccording(``int` `A1[], ``int` `A2[], ``int` `m,``                              ``int` `n)``    ``{``        ``// The temp array is used to store a copy``        ``// of A1[] and visited[] is used to mark the``        ``// visited elements in temp[].``        ``int` `temp[] = ``new` `int``[m], visited[] = ``new` `int``[m];``        ``for` `(``int` `i = ``0``; i < m; i++) {``            ``temp[i] = A1[i];``            ``visited[i] = ``0``;``        ``}` `        ``// Sort elements in temp``        ``Arrays.sort(temp);` `        ``// for index of output which is sorted A1[]``        ``int` `ind = ``0``;` `        ``// Consider all elements of A2[], find them``        ``// in temp[] and copy to A1[] in order.``        ``for` `(``int` `i = ``0``; i < n; i++) {``            ``// Find index of the first occurrence``            ``// of A2[i] in temp``            ``int` `f = first(temp, ``0``, m - ``1``, A2[i], m);` `            ``// If not present, no need to proceed``            ``if` `(f == -``1``)``                ``continue``;` `            ``// Copy all occurrences of A2[i] to A1[]``            ``for` `(``int` `j = f; (j < m && temp[j] == A2[i]);``                 ``j++) {``                ``A1[ind++] = temp[j];``                ``visited[j] = ``1``;``            ``}``        ``}` `        ``// Now copy all items of temp[] which are``        ``// not present in A2[]``        ``for` `(``int` `i = ``0``; i < m; i++)``            ``if` `(visited[i] == ``0``)``                ``A1[ind++] = temp[i];``    ``}` `    ``// Utility function to print an array``    ``static` `void` `printArray(``int` `arr[], ``int` `n)``    ``{``        ``for` `(``int` `i = ``0``; i < n; i++)``            ``System.out.print(arr[i] + ``" "``);``        ``System.out.println();``    ``}` `    ``// Driver program to test above function.``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `A1[] = { ``2``, ``1``, ``2``, ``5``, ``7``, ``1``, ``9``, ``3``, ``6``, ``8``, ``8` `};``        ``int` `A2[] = { ``2``, ``1``, ``8``, ``3` `};``        ``int` `m = A1.length;``        ``int` `n = A2.length;``        ``System.out.println(``"Sorted array is "``);``        ``sortAccording(A1, A2, m, n);``        ``printArray(A1, m);``    ``}``}` `/*This code is contributed by Nikita Tiwari.*/`

## Python3

 `"""A Python 3  program to sort an array``according to the order defined by``another array"""` `"""A Binary Search based function to find``index of FIRST occurrence of x in arr[].``If x is not present, then it returns -1 """` `def` `first(arr, low, high, x, n) :``    ``if` `(high >``=` `low) :``        ``mid ``=` `low ``+` `(high ``-` `low) ``/``/` `2``;  ``# (low + high)/2;``        ``if` `((mid ``=``=` `0` `or` `x > arr[mid``-``1``]) ``and` `arr[mid] ``=``=` `x) :``            ``return` `mid``        ``if` `(x > arr[mid]) :``            ``return` `first(arr, (mid ``+` `1``), high, x, n)``        ``return` `first(arr, low, (mid ``-``1``), x, n)``        ` `    ``return` `-``1``    ` `# Sort A1[0..m-1] according to the order``# defined by A2[0..n-1].``def` `sortAccording(A1, A2, m, n) :``  ` `    ``"""The temp array is used to store a copy``    ``of A1[] and visited[] is used mark the``    ``visited elements in temp[]."""``    ``temp ``=` `[``0``] ``*` `m``    ``visited ``=` `[``0``] ``*` `m``    ` `    ``for` `i ``in` `range``(``0``, m) :``        ``temp[i] ``=` `A1[i]``        ``visited[i] ``=` `0`` ` `    ``# Sort elements in temp``    ``temp.sort()``    ` `    ``# for index of output which is sorted A1[]``    ``ind ``=` `0`   ` ` `    ``"""Consider all elements of A2[], find``    ``them in temp[] and copy to A1[] in order."""``    ``for` `i ``in` `range``(``0``, n) :``        ` `        ``# Find index of the first occurrence``        ``# of A2[i] in temp``        ``f ``=` `first(temp, ``0``, m``-``1``, A2[i], m)`` ` `        ``# If not present, no need to proceed``        ``if` `(f ``=``=` `-``1``) :``            ``continue`` ` `        ``# Copy all occurrences of A2[i] to A1[]``        ``j ``=` `f``        ``while` `(j

## C#

 `// A C# program to sort an array according``// to the order defined by another array``using` `System;` `class` `GFG {` `    ``/* A Binary Search based function to find``    ``index of FIRST occurrence of x in arr[].``    ``If x is not present, then it returns -1 */``    ``static` `int` `first(``int``[] arr, ``int` `low,``                     ``int` `high, ``int` `x, ``int` `n)``    ``{``        ``if` `(high >= low) {``            ``/* (low + high)/2; */``            ``int` `mid = low + (high - low) / 2;` `            ``if` `((mid == 0 || x > arr[mid - 1]) && arr[mid] == x)``                ``return` `mid;``            ``if` `(x > arr[mid])``                ``return` `first(arr, (mid + 1), high,``                             ``x, n);``            ``return` `first(arr, low, (mid - 1), x, n);``        ``}``        ``return` `-1;``    ``}` `    ``// Sort A1[0..m-1] according to the order``    ``// defined by A2[0..n-1].``    ``static` `void` `sortAccording(``int``[] A1, ``int``[] A2,``                              ``int` `m, ``int` `n)``    ``{` `        ``// The temp array is used to store a copy``        ``// of A1[] and visited[] is used to mark``        ``// the visited elements in temp[].``        ``int``[] temp = ``new` `int``[m];``        ``int``[] visited = ``new` `int``[m];` `        ``for` `(``int` `i = 0; i < m; i++) {``            ``temp[i] = A1[i];``            ``visited[i] = 0;``        ``}` `        ``// Sort elements in temp``        ``Array.Sort(temp);` `        ``// for index of output which is``        ``// sorted A1[]``        ``int` `ind = 0;` `        ``// Consider all elements of A2[], find``        ``// them in temp[] and copy to A1[] in``        ``// order.``        ``for` `(``int` `i = 0; i < n; i++) {` `            ``// Find index of the first occurrence``            ``// of A2[i] in temp``            ``int` `f = first(temp, 0, m - 1, A2[i], m);` `            ``// If not present, no need to proceed``            ``if` `(f == -1)``                ``continue``;` `            ``// Copy all occurrences of A2[i] to A1[]``            ``for` `(``int` `j = f; (j < m && temp[j] == A2[i]); j++) {``                ``A1[ind++] = temp[j];``                ``visited[j] = 1;``            ``}``        ``}` `        ``// Now copy all items of temp[] which are``        ``// not present in A2[]``        ``for` `(``int` `i = 0; i < m; i++)``            ``if` `(visited[i] == 0)``                ``A1[ind++] = temp[i];``    ``}` `    ``// Utility function to print an array``    ``static` `void` `printArray(``int``[] arr, ``int` `n)``    ``{``        ``for` `(``int` `i = 0; i < n; i++)``            ``Console.Write(arr[i] + ``" "``);``        ``Console.WriteLine();``    ``}` `    ``// Driver program to test above function.``    ``public` `static` `void` `Main()``    ``{``        ``int``[] A1 = { 2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8 };``        ``int``[] A2 = { 2, 1, 8, 3 };``        ``int` `m = A1.Length;``        ``int` `n = A2.Length;``        ``Console.WriteLine(``"Sorted array is "``);``        ``sortAccording(A1, A2, m, n);``        ``printArray(A1, m);``    ``}``}` `// This code is contributed by nitin mittal.`

## PHP

 `= ``\$low``)``    ``{``        ``\$mid` `= ``intval``(``\$low` `+ (``\$high` `- ``\$low``) / 2);``        ``if` `((``\$mid` `== 0 || ``\$x` `> ``\$arr``[``\$mid` `- 1]) &&``                               ``\$arr``[``\$mid``] == ``\$x``)``            ``return` `\$mid``;``        ``if` `(``\$x` `> ``\$arr``[``\$mid``])``            ``return` `first(``\$arr``, (``\$mid` `+ 1), ``\$high``, ``\$x``, ``\$n``);``        ``return` `first(``\$arr``, ``\$low``, (``\$mid` `- 1), ``\$x``, ``\$n``);``    ``}``    ``return` `-1;``}` `// Sort A1[0..m-1] according to the order``// defined by A2[0..n-1].``function` `sortAccording(&``\$A1``, &``\$A2``, ``\$m``, ``\$n``)``{``    ``// The temp array is used to store a copy``    ``// of A1[] and visited[] is used mark the``    ``// visited elements in temp[].``    ``\$temp` `= ``array_fill``(0, ``\$m``, NULL);``    ``\$visited` `= ``array_fill``(0, ``\$m``, NULL);``    ``for` `(``\$i` `= 0; ``\$i` `< ``\$m``; ``\$i``++)``    ``{``        ``\$temp``[``\$i``] = ``\$A1``[``\$i``];``        ``\$visited``[``\$i``] = 0;``    ``}` `    ``// Sort elements in temp``    ``sort(``\$temp``);` `    ``\$ind` `= 0; ``// for index of output which is sorted A1[]` `    ``// Consider all elements of A2[], find``    ``// them in temp[] and copy to A1[] in order.``    ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++)``    ``{``        ``// Find index of the first occurrence``        ``// of A2[i] in temp``        ``\$f` `= first(``\$temp``, 0, ``\$m` `- 1, ``\$A2``[``\$i``], ``\$m``);` `        ``// If not present, no need to proceed``        ``if` `(``\$f` `== -1) ``continue``;` `        ``// Copy all occurrences of A2[i] to A1[]``        ``for` `(``\$j` `= ``\$f``; (``\$j` `< ``\$m` `&&``             ``\$temp``[``\$j``] == ``\$A2``[``\$i``]); ``\$j``++)``        ``{``            ``\$A1``[``\$ind``++] = ``\$temp``[``\$j``];``            ``\$visited``[``\$j``] = 1;``        ``}``    ``}` `    ``// Now copy all items of temp[] which``    ``// are not present in A2[]``    ``for` `(``\$i` `= 0; ``\$i` `< ``\$m``; ``\$i``++)``        ``if` `(``\$visited``[``\$i``] == 0)``            ``\$A1``[``\$ind``++] = ``\$temp``[``\$i``];``}` `// Utility function to print an array``function` `printArray(&``\$arr``, ``\$n``)``{``    ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++)``        ``echo` `\$arr``[``\$i``] . ``" "``;``    ``echo` `"\n"``;``}` `// Driver Code``\$A1` `= ``array``(2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8);``\$A2` `= ``array``(2, 1, 8, 3);``\$m` `= sizeof(``\$A1``);``\$n` `= sizeof(``\$A2``);``echo` `"Sorted array is \n"``;``sortAccording(``\$A1``, ``\$A2``, ``\$m``, ``\$n``);``printArray(``\$A1``, ``\$m``);` `// This code is contributed by ita_c``?>`

## Javascript

 ``
Output
```Sorted array is
2 2 1 1 8 8 3 5 6 7 9 ```

Time complexity: The steps 1 and 2 require O(m) time. Step 3 requires O(M * Log M) time. Step 5 requires O(N Log M) time. Therefore overall time complexity is O(M Log M + N Log M).

Thanks to vivek for suggesting this method.

Method 2 (Using Self-Balancing Binary Search Tree)
We can also use a self-balancing BST like AVL Tree, Red Black Tree, etc. Following are detailed steps.

• Create a self-balancing BST of all elements in A1[]. In every node of BST, also keep track of count of occurrences of the key and a bool field visited which is initialized as false for all nodes.
• Initialize the output index ind as 0.
• Do following for every element of A2[i] in A2[]
1. Search for A2[i] in the BST, if present then copy all occurrences to A1[ind] and increment ind. Also mark the copied elements visited in the BST node.
• Do an inorder traversal of BST and copy all unvisited keys to A1[].

Time Complexity of this method is the same as the previous method. Note that in a self-balancing Binary Search Tree, all operations require logm time.

Method 3 (Use Hashing)

• Loop through A1[], store the count of every number in a HashMap (key: number, value: count of number)
• Loop through A2[], check if it is present in HashMap, if so, put in output array that many times and remove the number from HashMap.
• Sort the rest of the numbers present in HashMap and put in the output array.

Thanks to Anurag Sigh for suggesting this method.

Below is the implementation of the above approach:

## Python3

 `from` `collections ``import` `Counter` `# Function to sort arr1``# according to arr2``def` `solve(arr1, arr2):``    ``# Our output array``    ``res ``=` `[]``    ` `    ``# Counting Frequency of each``    ``# number in arr1``    ``f ``=` `Counter(arr1)``    ` `    ``# Iterate over arr2 and append all``    ``# occurences of element of``    ``# arr2 from arr1``    ``for` `e ``in` `arr2:``      ` `        ``# Appending element 'e',``        ``# f[e] number of times``        ``res.extend([e]``*``f[e])``        ` `        ``# Count of 'e' after appending is zero``        ``f[e] ``=` `0``        ` `    ``# Remaining numbers in arr1 in sorted``    ``# order (Numbers with non-zero frequency)``    ``rem ``=` `list``(``sorted``(``filter``(``      ``lambda` `x: f[x] !``=` `0``, f.keys())))``    ` `    ``# Append them also``    ``for` `e ``in` `rem:``        ``res.extend([e]``*``f[e])``        ` `    ``return` `res`  `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``    ``arr1 ``=` `[``2``, ``1``, ``2``, ``5``, ``7``, ``1``, ``9``, ``3``, ``6``, ``8``, ``8``]``    ``arr2 ``=` `[``2``, ``1``, ``8``, ``3``]``    ``print``(``*``solve(arr1, arr2))`

## C++

 `// A C++ program to sort an array according to the order``// defined by another array``#include ``using` `namespace` `std;` `// function to sort A1 according to A2 using hash map in C++``void` `sortA1ByA2(``int` `A1[], ``int` `N, ``int` `A2[], ``int` `M, ``int` `ans[])``{``    ``map<``int``, ``int``> mp;` `    ``// indexing for answer = ans array``    ``int` `ind = 0;` `    ``// initially storing frequency of each element of A1 in``    ``// map [ key, value ] = [ A1[i] , frequency[ A1[i] ] ]``    ``for` `(``int` `i = 0; i < N; i++) {``        ``mp[A1[i]] += 1;``    ``}` `    ``// traversing each element of A2, first come first serve``    ``for` `(``int` `i = 0; i < M; i++) {` `        ``// checking if current element of A2 is present in``        ``// A1 or not if not present go to next iteration``        ``// else store number of times it is appearing in A1``        ``// in ans array``        ``if` `(mp[A2[i]] != 0) {` `            ``// mp[ A2[i] ] = frequency of A2[i] element in``            ``// A1 array``            ``for` `(``int` `j = 1; j <= mp[A2[i]]; j++)``                ``ans[ind++] = A2[i];``        ``}` `        ``// to avoid duplicate storing of same element of A2``        ``// in ans array``        ``mp.erase(A2[i]);``    ``}` `    ``// store the remaining elements of A1 in sorted order in``    ``// ans array``    ``for` `(``auto` `it : mp) {` `        ``// it.second = frequency of remaining elements``        ``for` `(``int` `j = 1; j <= it.second; j++)``            ``ans[ind++] = it.first;``    ``}``}` `// Utility function to print an array``void` `printArray(``int` `arr[], ``int` `n)``{``    ``// Iterate in the array``    ``for` `(``int` `i = 0; i < n; i++)``        ``cout << arr[i] << ``" "``;``    ``cout << endl;``}` `// Driver Code``int` `main()``{``    ``int` `A1[] = { 2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8 };``    ``int` `A2[] = { 2, 1, 8, 3 };``    ``int` `n = ``sizeof``(A1) / ``sizeof``(A1);``    ``int` `m = ``sizeof``(A2) / ``sizeof``(A2);` `    ``// The ans array is used to store the final sorted array``    ``int` `ans[n];``    ``sortA1ByA2(A1, n, A2, m, ans);` `    ``// Prints the sorted array``    ``cout << ``"Sorted array is \n"``;``    ``printArray(ans, n);` `    ``return` `0;``}`
Output
`2 2 1 1 8 8 3 5 6 7 9`

Steps 1 and 2 on average take O(m+n) time under the assumption that we have a good hashing function that takes O(1) time for insertion and search on average. The third step takes O(p Log p) time where p is the number of elements remained after considering elements of A2[].

Method 4 (By Writing a Customized Compare Method)
We can also customize compare method of a sorting algorithm to solve the above problem. For example, qsort() in C allows us to pass our own customized compare method

• If num1 and num2 both are in A2 then the number with a lower index in A2 will be treated smaller than others.
• If only one of num1 or num2 present in A2, then that number will be treated smaller than the other which doesn’t present in A2.
• If both are not in A2, then the natural ordering will be taken.

The time complexity of this method is O(mnLogm) if we use a O(nLogn) time complexity sorting algorithm. We can improve time complexity to O(mLogm) by using a Hashing instead of doing linear search.
Below is the implementation of the above approach:

## C

 `// A C++ program to sort an array according to the order defined``// by another array``#include ``#include ` `// A2 is made global here so that it can be accesed by compareByA2()``// The syntax of qsort() allows only two parameters to compareByA2()``int` `A2;` `// size of A2[]``int` `size = 5;` `int` `search(``int` `key)``{``    ``int` `i = 0, idx = 0;``    ``for` `(i = 0; i < size; i++)``        ``if` `(A2[i] == key)``            ``return` `i;``    ``return` `-1;``}` `// A custom comapre method to compare elements of A1[] according``// to the order defined by A2[].``int` `compareByA2(``const` `void``* a, ``const` `void``* b)``{``    ``int` `idx1 = search(*(``int``*)a);``    ``int` `idx2 = search(*(``int``*)b);``    ``if` `(idx1 != -1 && idx2 != -1)``        ``return` `idx1 - idx2;``    ``else` `if` `(idx1 != -1)``        ``return` `-1;``    ``else` `if` `(idx2 != -1)``        ``return` `1;``    ``else``        ``return` `(*(``int``*)a - *(``int``*)b);``}` `// This method mainly uses qsort to sort A1[] according to A2[]``void` `sortA1ByA2(``int` `A1[], ``int` `size1)``{``    ``qsort``(A1, size1, ``sizeof``(``int``), compareByA2);``}` `// Driver program to test above function``int` `main(``int` `argc, ``char``* argv[])``{``    ``int` `A1[] = { 2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8, 7, 5, 6, 9, 7, 5 };` `    ``// A2[] = {2, 1, 8, 3, 4};``    ``A2 = 2;``    ``A2 = 1;``    ``A2 = 8;``    ``A2 = 3;``    ``A2 = 4;``    ``int` `size1 = ``sizeof``(A1) / ``sizeof``(A1);` `    ``sortA1ByA2(A1, size1);` `    ``printf``(``"Sorted Array is "``);``    ``int` `i;``    ``for` `(i = 0; i < size1; i++)``        ``printf``(``"%d "``, A1[i]);``    ``return` `0;``}`
Output
`Sorted Array is 2 2 1 1 8 8 3 5 5 5 6 6 7 7 7 9 9 `

This method is based on comments by readers (Xinuo Chen, Pranay Doshi and javakurious) and compiled by Anurag Singh.
This article is compiled by Piyush. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up