I have sorted the second array and arrange first array with respect to sorted second array in O(n^2) time and my code is something like this-
//c++ code
void sort(int size,int arr1[],int arr2[])
{
for(int i=0;i<size-1;++i)
{
for(int j=i+1;j<size;++j)
{
if(arr2[i]>arr2[j])
{
swap(arr2[i],arr2[j]);
swap(arr1[i],arr1[j]);
}
//for duplicate data in arr2
if(arr2[i]==arr2[j])
{
if(arr1[i]>arr1[j])
{
swap(arr1[i],arr1[j]);
}
}
}
}
}
for example if-
arr1=[2,1,3,9,7,12,5,13]
arr2=[1,3,6,9,2,3,1,3]
after sorting arr2 with respect to arr1
arr1=[2,5,7,1,12,13,3,9]
arr2=[1,1,2,3,3,3,6,9]
the time complexity of this function is O(n^2) now what will be the better approach to sort it?