The greatest common divisor algorithm --- four different data size of the average running time algorithm

The greatest common divisor "four heavenly kings" Algorithm
1. Euclidean

2. exhaustive method

3. Method Decreases

4.Stein algorithm

Algorithm for a Euclidean
algorithm procedure: Prerequisite: set two atoms a, b provided wherein a do dividend, b do divisor, TEMP number I
(1) large numbers put a, decimal place b; and
(2) find a / b remainder; and
(3) if temp = 0 then b is the greatest common divisor;
! (4) temp = 0 if the value of b to put the value a, temp to a;
(. 5) back to Step;

        //辗转相除法
        public int divisor1(int a,int b){
            int temp;
            if(a < b){
                temp = a;
                a = b;
                b = temp;
            }
            while(b != 0){
                temp = a%b;
                a = b;
                b = temp;
            }
            return a;

        }

Flowchart below it, by my worship
Here Insert Picture Description
algorithm two brute-force
algorithm process: the premise: Let the two numbers is a, b. Where, a large value, b is a smaller value
(1) the smaller of two numbers include numbers begin descending;
(2) is immediately interrupted until you find the divisors include;
divisor (3) will be obtained i.e. It is the common denominator;

//穷举法
public int divisor2(int a,int b){
            int temp = a<b?a:b;
            while(temp > 0){
                if(a%temp==0 && b%temp==0){
                    break;
                }
                temp--;
            }
            return temp;
        }

Flowchart lower it, by a prayer I
Here Insert Picture Description
algorithm Midnight Decreases method
algorithmic processes:
(1) any two given positive integer; determining whether they are even. If, with the reduction 2; the second step is performed if it is not;
(2) with a large number of small reduced number, then the resultant difference is small and the number of comparison, and to reduce the number of large numbers. This operation continues until the subtrahend and difference resultant equal.
A plurality of product (3) about the first step and the second step out of the medium 2 is the desired number of the greatest common divisor.

//更相减损法
public int divisor3(int a,int b){
            int temp;
            int count = 0;
            while(a%2==0 && b%2==0){
                a /= 2;
                b /= 2;
                count++;
            }
            //保证a大于b
            if(a < b){
                temp = a;
                a = b;
                b = temp;
            }
            while((a-b) != 0){
                temp = a - b;
                a = temp>b?temp:b;//a保存较大值
                b = temp<b?temp:b;//b保存较小值
                if(temp == (a - b)){
                    break;
                }
            }
            if(count == 0){
                return b;
            }else{
                return (int)Math.pow(2,count)*b;
            }
        }

Flowchart lower it, by a prayer I
Here Insert Picture Description
algorithm four Stein algorithm
algorithmic process: two positive integers X> Y:
(. 1) are the even divisor4 (x, y) = 2 * divisor (x / 2, y / 2) ;
(2) both odd divisor4 (X, y) = divisor4 ((X + y) / 2, (XY) / 2);
(. 3) y X odd even divisor4 (x, y) = divisor4 (x, y / 2);
(. 4) y X odd even divisor4 (x, y) = divisor4 (x / 2, y) , or divisor4 (x, y) = divisor4 (x, y / 2);

//Stein算法
public int divisor4(int a,int b) {
            int m, n;
            m = a >= b ? a : b;   //m保存a、b中的较大者
            n = a < b ? a : b;    //n保存a、b中的较小者
            if (0 == n)
                return m;
            if(m%2 == 0&&n%2 == 0)//m偶数n偶数
                return 2 * divisor4(m / 2, n / 2);
            if (m % 2 == 0)//m偶数n奇数
                return divisor4(m / 2, n);
            if (n % 2 == 0)//m奇数n偶数
                return divisor4(m, n / 2);
            return divisor4((m + n) / 2, (m - n) / 2);//m奇数n奇数
        }

Flowchart below it, I worship by
Here Insert Picture Description
key algorithm is ready;
Key Question 1: The data generated
using the Random generates random numbers to form a two-dimensional array, each set of data elements that is a two-dimensional array.
The core algorithm is as follows:

Random random=new Random();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入测试数据规模:");
        int num = sc.nextInt();
        int[][] array=new int[num][2];//声明二维数组
        //给数组赋值
        for(int i=0;i<num;i++){
            for(int j=0;j<2;j++){
               //在0-100内随机生成一个正整数
                array[i][j]= random.nextInt(100);
            }
        }

Key Issues: calculating an average running time
java have a specific run-time code program seeking
time = (End Time - Start Time) / number of executions of the algorithm, unit: NS (ns)
core algorithm is as follows:

long start1=System.nanoTime();   //获取开始时间
        for(int i=0;i<array.length;i++){
            new MaxDivisor().divisor1(array[i][0],array[i][1]);
        }
        long end1=System.nanoTime(); //获取结束时间
        System.out.println("程序运行时间: "+(end1-start1)/array.length+"ns");

Ha ha ha, the main components list them, then, to give it the "four kings" of the source code, in a program like Hello winded, and see if the source code can not get the focus, you can look at the code listed above Oh block

import java.util.Random;
import java.util.Scanner;

/**
 * @Author:Star
 * @Date:Created in 1:06 2019/3/9
 * @Description:测试不同算法在不同数据规模下的平均运行时间
 */
public class Test1 {
    public static void main(String[] args) {
        Random random=new Random();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入测试数据规模:");
        int num = sc.nextInt();
        int[][] array=new int[num][2];//声明二维数组
        //给数组赋值
        for(int i=0;i<num;i++){
            for(int j=0;j<2;j++){
               //在0-100内随机生成一个正整数
                array[i][j]= random.nextInt(100);
            }
        }
        //计算不同算法在相同数据规模下的平均运行时间
        new TextTime().time(array);
    }
}

    class MaxDivisor{
        //辗转相除法
        public int divisor1(int a,int b){
            int temp;
            if(a < b){
                temp = a;
                a = b;
                b = temp;
            }
            while(b != 0){
                temp = a%b;
                a = b;
                b = temp;
            }
            return a;

        }
        //穷举法
        public int divisor2(int a,int b){
            int temp = a<b?a:b;
            while(temp > 0){
                if(a%temp==0 && b%temp==0){
                    break;
                }
                temp--;
            }
            return temp;
        }
        //更相减损法
        public int divisor3(int a,int b){
            int temp;
            int count = 0;
            while(a%2==0 && b%2==0){
                a /= 2;
                b /= 2;
                count++;
            }
            //保证a大于b
            if(a < b){
                temp = a;
                a = b;
                b = temp;
            }
            while((a-b) != 0){
                temp = a - b;
                a = temp>b?temp:b;//a保存较大值
                b = temp<b?temp:b;//b保存较小值
                if(temp == (a - b)){
                    break;
                }
            }
            if(count == 0){
                return b;
            }else{
                return (int)Math.pow(2,count)*b;
            }
        }
        //Stein算法
        public int divisor4(int a,int b) {
            int m, n;
            m = a >= b ? a : b;   //m保存a、b中的较大者
            n = a < b ? a : b;    //n保存a、b中的较小者
            if (0 == n)
                return m;
            if(m%2 == 0&&n%2 == 0)//m偶数n偶数
                return 2 * divisor4(m / 2, n / 2);
            if (m % 2 == 0)//m偶数n奇数
                return divisor4(m / 2, n);
            if (n % 2 == 0)//m奇数n偶数
                return divisor4(m, n / 2);
            return divisor4((m + n) / 2, (m - n) / 2);//m奇数n奇数
        }
    }

    class TextTime{
    public void time(int[][] array){
        long start1=System.nanoTime();   //获取开始时间
        for(int i=0;i<array.length;i++){
            new MaxDivisor().divisor1(array[i][0],array[i][1]);
        }
        long end1=System.nanoTime(); //获取结束时间
        System.out.println("程序运行时间: "+(end1-start1)/array.length+"ns");

        long start2=System.nanoTime();   //获取开始时间
        for(int i=0;i<array.length;i++){
            new MaxDivisor().divisor2(array[i][0],array[i][1]);
        }
        long end2=System.nanoTime(); //获取结束时间
        System.out.println("程序运行时间: "+(end2-start2)/array.length+"ns");

        long start3=System.nanoTime();   //获取开始时间
        for(int i=0;i<array.length;i++){
            new MaxDivisor().divisor1(array[i][0],array[i][1]);
        }
        long end3=System.nanoTime(); //获取结束时间
        System.out.println("程序运行时间: "+(end3-start3)/array.length+"ns");

        long start4=System.nanoTime();   //获取开始时间
        for(int i=0;i<array.length;i++){
            new MaxDivisor().divisor1(array[i][0],array[i][1]);
        }
        long end4=System.nanoTime(); //获取结束时间
        System.out.println("程序运行时间: "+(end4-start4)/array.length+"ns");
    }
    }

End of program
next to share with you my test results
1. Data scale ---- 10 sets of data
Here Insert Picture Description
2. Data scale ---- 20 sets of data
Here Insert Picture Description
3. Data scale ---- 40 groups of data
Here Insert Picture Description
4. Data scale - --- 100 groups of data
Here Insert Picture Description
you want to see a larger data size, can go to practice Oh

The most important part came ------- today part of self-review
1. suddenly discovered that the greatest common divisor algorithm for solving so many, alas, really want to quietly;
2. the face of large-scale data, or to the average running time of winning the election algorithm to achieve, learn learn learn;
3. the program, I compare different algorithms under the premise of the same data and data size, write a lot of duplicate code, the code relative lack of indirection compare four algorithms, I wrote four times, but I've been thinking, if there are 100 algorithm, I was not trying to write 100 times, ah ah ah ah, think hard for a long time, her hair embarrassed or hard copy and paste modified three times.

Finally, would like to say a big:
If anyone really could not stand the code I wrote (including my ugly flow chart), you can leave a message to me, help me silly child. Hope bigwigs wing. (Manual bow)

Guess you like

Origin blog.csdn.net/weixin_44369212/article/details/88370069