The second algorithm work - Chapter II Hands algorithm experimental report two reports -7-3 ordered sequence of digits

The median algorithm Chapter II Hands report -7-3 two ordered sequences

 
7-3 two median ordered sequence (20 minutes)
 

There are two known non-equal length descending sequence S1, S2, S1 and S2 function evaluation design and set median. Ordered sequence , means a median A ( N - 1 ) / 2 values, i.e., the first ⌊ number ( A 0 is the number 1).

Input formats:

Enter the three lines. The first line gives the sequence of a common length N (0 <N ≤100000), followed by a sequence of information per line, i.e., in descending order of N non-integer. Digital intervals by a space.

Output formats:

Two input and output sequences set of sequence bits in a row.

Sample Input 1:

5
1 3 5 7 9
2 3 4 5 6

Output Sample 1:

4

Sample Input 2:

6
-100 -10 1 1 1 1
-50 0 2 3 4 5

Output Sample 2:

1

算法描述:
  1.先用一次归并排序将两个数组放在一个新创建的大小为2*num.length的数组中  2.对该数组进行去重,并用k记录新排序的数组的大小
  3.返回该数组的第k/2个元素

代码如下:

public static int Mid(int[] num1,int[] num2){
  int[] temp=new int[2*num1.length];
  int i=0,j=0,m=0;  
  while(i<num1.length&&j<num2.length){
    temp[m++]=num1[i]<num2[j]?num1[i++]:num2[j++];
  }
  while(i<num1.length){
    temp[m++]=num1[i++];
  }
  while(j<num2.length){
    temp[m++]=num2[j++];
  }
  int k=0;
  for(int l=1;l<temp.length;l++){
    if(temp[l]!=temp[k]){
      temp[++k]=temp[l];
    }
  }

return temp[k/2];

}

Complexity analysis:

  Time complexity: a merge sort in a total of three cycles, plus one cycle up heavy, it should be O (n)

  Space complexity: create a temporary array of size 2 * num.length, it should be O (n)

Summary:
  1. The idea is simple, but there are problems, the biggest test case for N, appears to run overtime,

  Dichotomous still not very familiar with, the need to continue the familiar divide and conquer.

 

7-3 two median ordered sequence (20 minutes)
 

There are two known non-equal length descending sequence S1, S2, S1 and S2 function evaluation design and set median. Ordered sequence , means a median A ( N - 1 ) / 2 values, i.e., the first ⌊ number ( A 0 is the number 1).

Input formats:

Enter the three lines. The first line gives the sequence of a common length N (0 <N ≤100000), followed by a sequence of information per line, i.e., in descending order of N non-integer. Digital intervals by a space.

Output formats:

Two input and output sequences set of sequence bits in a row.

Sample Input 1:

5
1 3 5 7 9
2 3 4 5 6

Output Sample 1:

4

Sample Input 2:

6
-100 -10 1 1 1 1
-50 0 2 3 4 5

Output Sample 2:

1

算法描述:
  1.先用一次归并排序将两个数组放在一个新创建的大小为2*num.length的数组中  2.对该数组进行去重,并用k记录新排序的数组的大小
  3.返回该数组的第k/2个元素

代码如下:

public static int Mid(int[] num1,int[] num2){
  int[] temp=new int[2*num1.length];
  int i=0,j=0,m=0;  
  while(i<num1.length&&j<num2.length){
    temp[m++]=num1[i]<num2[j]?num1[i++]:num2[j++];
  }
  while(i<num1.length){
    temp[m++]=num1[i++];
  }
  while(j<num2.length){
    temp[m++]=num2[j++];
  }
  int k=0;
  for(int l=1;l<temp.length;l++){
    if(temp[l]!=temp[k]){
      temp[++k]=temp[l];
    }
  }

return temp[k/2];

}

Complexity analysis:

  Time complexity: a merge sort in a total of three cycles, plus one cycle up heavy, it should be O (n)

  Space complexity: create a temporary array of size 2 * num.length, it should be O (n)

Summary:
  1. The idea is simple, but there are problems, the biggest test case for N, appears to run overtime,

  Dichotomous still not very familiar with, the need to continue the familiar divide and conquer.

 

Guess you like

Origin www.cnblogs.com/ysmsxzs/p/11575631.html