Chapter II experiment on the computer algorithm

     The first algorithm on the first machine test report

                            Panjun Lin Ding Lili

7-1 binary search: a value for n (1 <= n <= 1000 ), n non-descending order of an integer and the number of x to find, using the binary search algorithm to find x, output x where the subscripts (0 ~ n -1) and the number of comparisons. If x does not exist, the output 1 and the number of comparisons. Input formats:

The input common three lines: The first line is the value of n; n is an integer second line; the third line is the x value.

Output formats:

Where the subscript x output (0 ~ n-1) and the number of comparisons. If x does not exist, the output 1 and the number of comparisons.

Sample input:

4

1 2 3 4

1

Sample output:

0

2

Problem-solving ideas: This title is the most important binary search. We started using the algorithm of the book, using a recursive function, there have been a few questions. The main function of the problem is not too large, it was seen in a recursive function. Beginning in place of n-1 out of the question, when is to find the number does not exist, impossible to escape recursive algorithm. To switch to other functions, iterative, like this is even more simple. Second, the key point is counted. Outside the loop of the added count ++.

import java.util.Scanner;

 

import static java.util.Arrays.sort;

 

public class Main {

    static int count = 0;

public static int biSearch(int[] a,int left,int right,int x)

{

    while(left <= right)

    {

        int mid = (left + right) / 2;

        if(a[mid] == x)

            {

                count++;

                return mid;

            }

         if(a[mid] < x)

     {

         count++;

         left = mid + 1;

     }

         if(a[mid] > x)

         {

             count++;

             right = mid - 1;

         }

    }

    return -1;

}

    

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int[] a = new int[n];

for(int i = 0; i < n; i++)

a[i] = input.nextInt();

int x = input.nextInt();

System.out.println(biSearch(a,0,n - 1,x));

        System.out.print(count);

}

}

7-2 rewrite the binary search algorithm

Set a [0: n-1] is an array row have good sequence, rewrite the binary search algorithm , such that when x is not in the array, return the position of the largest element of x is less than i and greater than a minimum J x element of the position. When searching for an element in an array, i and j the same, both the position x in the array.

Input formats:

Enter two lines:

The first row is n and the value of x; the second line is a non-descending sequence of n different integers, separated by spaces between each integer.

Output formats:

Subscript j is less than the minimum x maximum output element and the maximum index i is greater than the smallest element of x. When searching for an element in an array, i and j same. Tip: If x is less than the full value, the output: -1 0 if x is greater than all the values, the output: the value of n-1, n

Sample input:

Here we are given a set of inputs. E.g:

6 5

2 4 6 8 10 12

Sample output:

Given here corresponding output. E.g:

1 2

Problem-solving ideas: first half of this question in the search elements can follow the code of the first title fight. This issue requires Points. When this number of time can be found in the array, the array can not be found in time. More should be noted that when the situation does not exist. Title is required in a specific order, when not present, the subject is required when the determined number is smaller than the maximum element position and the determined number is larger than the smallest element position, is adjacent positions. Wherein there is a problem we array out of range, the problem lies in the range of the wrong table defines n = 10, in operation 11 was used.

import java.util.Scanner;

 

import static java.util.Arrays.sort;

 

public class Main{

 

public static void main(String[] args) {

    int max = 0;

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int x = input.nextInt();

int[] a = new int[n];

for(int i = 0; i < n; i++)

a[i] = input.nextInt();

int left = 0; int right= n-1;

while(left <= right)

    {

        int mid = (left + right) / 2;

        if(a[mid] == x)

            {

                System.out.println(mid + " " + mid);

                break;

            }

        if(a[mid] < x)

     {

         left = mid + 1;

     }

        if(a[mid] > x)

         {

             right = mid - 1;

         }

}

for(int i = 1 ; i < n ;i ++)

{

    if(a[i]!=x)

    {

        if(x < a[i] && x > a[i-1])

        {

            max = i;

            System.out.print((max-1) + " " + max);

        }

    }

}

if(x < a[0])

System.out.print(-1 + " " + 0);

if(x > a[n-1])

    System.out.print(n-1 + " " + n);

}

}

Median 7-3 two ordered sequences

There are two known non-equal length descending sequence S1, S2, S1 and S2 function evaluation design and set median. Ordered sequence A 0, A 1, ⋯, median A N-1 refers to a value of A (N-1) / 2, i.e., the first ⌊ (N + 1 ) / 2⌋ 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

import java.util.Scanner;

public class Main{

public static int integration(int[] a1, int[] a2){

int i = 0,j = 0,k = 0;

int N;

int[] integration = new int[a1.length+a2.length];

while(i < a1.length && j < a2.length ) {

if (a1[i] <= a2[j]) {

integration[k] = a1[i];

i++;

k++;

} else {

integration[k] = a2[j];

k++;

j++;

}

}

if(i == a1.length){

for(;j < a2.length;j++,k++)

integration[k] = a2[j];

}

if(j == a2.length){

for(;i < a1.length;i++,k++)

integration[k] = a2[i];

}

return N = integration[(k-1) / 2];

}

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

int[] a1 = new int[n];

int[] a2 = new int[n];

for(int i = 0; i < n; i++)

a1[i] = input.nextInt();

for(int i = 0; i < n; i++)

a2[i] = input.nextInt();

int a = integration(a1,a2);

System.out.println(a);

}

}

Guess you like

Origin www.cnblogs.com/liuningwoaini/p/11575383.html