Relative Sorting
PROBLEM :
Given two array 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.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is M and N,M is the number of elements in A1 and N is the number of elements in A2.
The second line of each test case contains M elements.
The third line of each test case contains N elements.
Output:
Print the sorted array according order defined by another array.
Constraints:
1 = T = 50
1 = M = 50
1 = N = 10 & N = M
1 = A1[i], A2[i] = 1000
Example:
Input:
1
11 4
2 1 2 5 7 1 9 3 6 8 8
2 1 8 3
Output:
2 2 1 1 8 8 3 5 6 7 9
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define max 50
void RelativeSorting(int ,int ,int [],int []) ;
void swap(int [],int ,int ) ;
void BubbleSort(int [],int ,int ) ;
int main()
{
int t,m,n ;
int arr1[max],arr2[max] ;
cin>>t ;
while(t--)
{
cin>>m>>n ;
for(int i=0;i<m;i++)
cin>>arr1[i] ;
for(int i=0;i<n;i++)
cin>>arr2[i] ;
RelativeSorting(m,n,arr1,arr2) ;
for(int i=0;i<m;i++)
cout<<arr1[i]<<" " ;
cout<<endl ;
}
return 0;
}
void RelativeSorting(int m,int n,int arr1[],int arr2[])
{
int i,j,k ;
k=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(arr1[j]==arr2[i])
{
swap(arr1,k,j) ;
k++ ;
}
}
}
BubbleSort(arr1,k,m) ;
}
void swap(int arr[],int a,int b)
{
int temp ;
temp=arr[a] ;
arr[a]=arr[b] ;
arr[b]=temp ;
}
void BubbleSort(int arr[],int start,int end)
{
int temp ;
for(int i=start;i<end-1;i++)
{
for(int j=start;j<end-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j] ;
arr[j]=arr[j+1] ;
arr[j+1]=temp ;
}
}
}
}
---------------------------------------------------------------------------------
Given two array 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.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is M and N,M is the number of elements in A1 and N is the number of elements in A2.
The second line of each test case contains M elements.
The third line of each test case contains N elements.
Output:
Print the sorted array according order defined by another array.
Constraints:
1 = T = 50
1 = M = 50
1 = N = 10 & N = M
1 = A1[i], A2[i] = 1000
Example:
Input:
1
11 4
2 1 2 5 7 1 9 3 6 8 8
2 1 8 3
Output:
2 2 1 1 8 8 3 5 6 7 9
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define max 50
void RelativeSorting(int ,int ,int [],int []) ;
void swap(int [],int ,int ) ;
void BubbleSort(int [],int ,int ) ;
int main()
{
int t,m,n ;
int arr1[max],arr2[max] ;
cin>>t ;
while(t--)
{
cin>>m>>n ;
for(int i=0;i<m;i++)
cin>>arr1[i] ;
for(int i=0;i<n;i++)
cin>>arr2[i] ;
RelativeSorting(m,n,arr1,arr2) ;
for(int i=0;i<m;i++)
cout<<arr1[i]<<" " ;
cout<<endl ;
}
return 0;
}
void RelativeSorting(int m,int n,int arr1[],int arr2[])
{
int i,j,k ;
k=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(arr1[j]==arr2[i])
{
swap(arr1,k,j) ;
k++ ;
}
}
}
BubbleSort(arr1,k,m) ;
}
void swap(int arr[],int a,int b)
{
int temp ;
temp=arr[a] ;
arr[a]=arr[b] ;
arr[b]=temp ;
}
void BubbleSort(int arr[],int start,int end)
{
int temp ;
for(int i=start;i<end-1;i++)
{
for(int j=start;j<end-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j] ;
arr[j]=arr[j+1] ;
arr[j+1]=temp ;
}
}
}
}
---------------------------------------------------------------------------------
Comments
Post a Comment