相对排序算法

Description: This a coding problem came in the coding round of Amazon, Microsoft.

描述:这是亚马逊,微软的编码回合中的编码问题。

Problem Statement:

问题陈述:

Given two array A and B, sort A in such a way that the relative order among the elements will be the same as those in B. For the elements not present in B, append them at last in sorted order. It is also given that the number of elements in B is smaller than or equal to the number of elements in A and B has all distinct elements.

给定两个阵列AB,排序以这样的方式,该元件之间的相对顺序将是相同的那些B中 。 对于B中不存在的元素,最后按排序顺序附加它们。 还假定B中的元素数小于或等于A中的元素数,并且B具有所有不同的元素。

Solution

Algorithm:

算法:

  1. To solve the above problem vector is used to implement the arrays.

    为了解决上述问题,使用向量来实现数组。

  2. Initialize three vectors.

    初始化三个向量。

  3. Vector<int> a: for array A
    Vector<int> b: for array B
    Vector<int> c: for output array (relatively sorted array)
    
    
  4. Take input for array A and B

    接受数组A和B的输入

  5. Sort vector a using in-build sorting function.

    使用内置排序功能对向量a进行排序。

  6. For sorting the elements of a according to order of b,

    为了按照b的顺序对a的元素进行排序,

  7. For i=0:n2-1      //n2 be the no of elements of B&n1 of A
        For  j=0:n1-1 && a[j]<=b[i]    //maintaining the order of b
            if(a[j]==b[i])
                inserta[j] into c;
                Set a[j] to 0 for avoiding duplication
            End if
        End For loop
    End For loop
    
    
  8. The elements of vector a, also presented in vector b are sorted according to relative order of vector b. The rest of vector a is to be appended at the end of vector c in sorted way.

    向量a的元素, 矢量b还提出根据矢量b的相对排序。 向量a的其余部分将以排序方式附加在向量c的末尾。

  9. Just appended the rest of elements of vector a in vector c. (elements those are not zero).

    只需在向量c中追加向量a的其余元素。 (元素不为零)。

  10. vector c is the desired output.

    向量c是所需的输出。

C ++程序实现相对排序算法 (C++ program to implement relative sorting algorithm)

#include <bits/stdc++.h>
using namespace std;

vector<int> sorted(vector<int> a,vector<int> b,int n1,int n2){
	vector <int> c;
	// array a is sorted using labrary sorting function
	sort(a.begin(),a.end()); 

	for(int i=0;i<n2;i++){
		for(int j=0;j<n1 && a[j]<=b[i];j++){
			// elements of sorted a is entered to array c 
			// maintaing the element order as in b
			if(a[j]==b[i]){
				c.push_back(a[j]);
				//clear the element pushed into c
				a[j]=0;
			}
		}
	}

	// the elements that are not in b is in being entered to c 
	// in sorted manner as a is already sorted
	for(int i=0;i<n1;i++)  
		if(a[i]!=0)    //remaining elements of a
			c.push_back(a[i]);
			
	//return the output
	return c; 
}


int main() {
	int n1,n2,u;
	vector<int> :: iterator p; //iterator p

	scanf("%d %d",&n1,&n2);

	vector<int> a; //array a
	vector<int> b;//array b

	for(int j=0;j<n1;j++){
		scanf("%d",&u);
		// inputing elements of array a
		a.push_back(u); 
	}
	for(int j=0;j<n2;j++){
		scanf("%d",&u);
		// inputing elements of array b
		b.push_back(u);  
	}

	// implemented relative sorting function
	vector<int> c=sorted(a,b,n1,n2); 
	for(p=c.begin();p!=c.end();p++)	
		printf("%d ",*p);  // printing the sorted array
	printf("\n");

	return 0;
}

Output

输出量

enter length of array A & B respectively 
10 
5
enter elements of array A
2 5 12 2 8 9 13 5 8 12 
enter elements of array B
5 2 8 12 9 
printing the relatively sorted array 
5 5 2 2 8 8 12 12 9 13 


翻译自: https://www.includehelp.com/icp/relative-sorting.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值