There are two sequence A and b,a= (A1,A2,..., AK), b= (B1,B2,..., BK), A and B are all in ascending order, for 1<=i,j<=k, K minimum (AI+BJ), requiring the algorithm to be as efficient as possible

Source: Internet
Author: User

The basic idea is the same as the previous one:

1: Apply for a K size auxiliary array.

2: Assigning a value to an auxiliary array and sorting from small to large

3: In turn, use the following number and the maximum number of the secondary array to compare, if less than the maximum number of auxiliary arrays replaced, greater than the next comparison

The efficiency of the code is not high, who has a good way to please guide:

#define Min_kij 5
void Binsert_kij (int *array)//To insert sort of auxiliary array
{
	
	int mid,low=0,high=min_kij-2;//Last and former ( K-1) Number comparison
	while (Low<=high)      //Because it is a sequential array, so direct binary lookup
	{
		mid= (Low+high)/2;
		if (Array[min_kij-1]>array[mid])
		{
			low=mid+1;
		}
		else
		{
			high=mid-1;
		}
	}
	int temp=array[min_kij-1];
	for (int i=min_kij-2;i>=high+1;i--)
	{
		array[i+1]=array[i];
	}
	
	array[high+1]=temp;
}
void Min_kijt (int *array1,int *array2)
{
	int temp;
	int Assint[min_kij];
	for (i=1;i<min_kij;i++)//From the second start of the first array and the elements of the second array
	{for
		(int j=0;j<min_kij;j++)
		{
			Temp =ARRAY1[I]+ARRAY2[J];
			if (temp<assint[min_kij-1])//If the maximum value is less than the secondary array, replace
			{
				assint[min_kij-1]=temp;
				Binsert_kij (Assint); Reorder Secondary Arrays}} for
	(i=0;i<min_kij;i++)
	{
		printf ("%d", Assint[i]);
	}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.