C++ program to Sort an array according to the order defined by another array

Sort an array according to the order defined by the other array

Here we will learn about C++ program to Sort an array according to the order defined by another array. An array is a data structure. Here are given two arrays A [] and B [], sort A in such a way that the relative order among the elements will be the same as those are in B. The elements in array A has to be printed accordingly to the sequence of order of elements specified in array B, the rest of the elements, remaining in array A are printed at the end.

Example:

Array A [] = {3, 6, 13, 3, 9, 10, 14, 6, 9, 13}

Array B[] = {6, 3, 9, 13, 10}

Output: {6, 6, 3, 3, 9, 9, 13, 13, 10, 14}

Sort an array according to the order defined by another array in c++

Keypoint

In this section we will learn about basic knowledge which we need to know before coding the above Program. So we must have knowledge of what is an array? 

What is an array?
An array is a data structure, it is collection of similar data elements which is stored at contiguous memory locations in which each data element can be accessed directly by only using its index number.
 
About C++ language:-
C++ Programming Object Oriented Programming C++ is a programming language developed by Bjarne Stroustrup in 1979 at Bell Labs. C++ is a cross-platform language that can be used to create high-performance applications. It is extension of C language. About C++ Programming it is Multi-paradigm Language – C++ supports at least seven different styles of programming. It is General Purpose Language – You can use C++ to develop games, desktop apps, operating systems, and so on and  Speed – Like C programming, the performance of optimized C++ code is exceptional. 
 
 
How to declare an array?
To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows − This is called a single-dimensional array. The array Size must be an integer constant greater than zero and type can be any valid C data type. For example, to declare a 10-element array called balance of type double, use this statement − Here balance is a variable array which is sufficient to hold up to 10 double numbers

Algorithm of C++ program to Sort an array according to the order defined by another array

1. Input both the arrays, arr1 and arr2.

2. Intialize rank=0

3. Traverse the second array B, and insert the element with their rank as value in the map  mpp[B[i]]=rank++

4. Traverse the first array A, and insert the element with their rank as value in the map mpp[A[i]]=rank++

5.Sort the array using inbuilt comparator which compares the elements based on their rank in map

6.Print the sorted array as output

sort-according-to-second-array
#include<bits/stdc++.h>

    using namespace std;

    map<int,int>mpp;

    bool compare(int a,int b){

        if(mpp[a]<mpp[b])

            return true;

        return false;

    }

    int main(){

        int a[]={3, 6, 13, 3, 9, 10, 14, 6, 9, 13};

        int b[]={6,3,9,13,10};

        int rank=0;

        // inserting the elements of array b in the map and counting their frequency 

        for(int i=0;i<5;i++){

            if(mpp.find(b[i])==mpp.end()){

                mpp[b[i]]=rank++;

            }

        }

        // inserting the elements of array b in the map and counting their frequency 

        for(int i=0;i<10;i++){

            if(mpp.find(a[i])==mpp.end()){

                mpp[a[i]]=rank++;

            }

        }

        sort(a,a+10,compare);

        //printing the sorted array

        for(int i=0;i<10;i++){

            cout<<a[i]<<" ";

        }


    }
Output:-

6 6 3 3 9 9 13 13 10 14