进行冒泡排序、快速排序等效率比较并用echarts图显示

流过昼夜 提交于 2019-12-17 19:02:49
<!DOCTYPE html>
<html >
<head>
    <meta charset="utf-8">
    <title>Sort_echarts</title>
    <script src="echarts.js"></script>

</head>
<body>
<div id="main" style="width: 1000px;height:600px;margin:0px auto"></div>
<div id="demo" style="width: 100px;height:36px;margin:0px auto;text-align: center;font-weight: bold;font-family: 微软雅黑;font-size: 14px"></div>
<center>
    <button id="but" type="button" onClick="showtext();run()" style="width:74px;height:36px;font-weight: bold;font-family: 微软雅黑;font-size: 14px;border-radius: 28px;
background-color: #AAAAAA">计算</button>
    <button id="but2" type="button" onClick="show()" style="width:74px;height:36px;font-weight: bold;font-family: 微软雅黑;font-size: 14px;border-radius: 28px;
background-color: #AAAAAA">画图</button>
</center>

<script type="text/javascript">
    var myChart = echarts.init(document.getElementById("main"));

    babble1=[1,2,3,4];
    insert1=[5,6,7,8];
    quicksort1=[9,10,11,12];
    shell1=[13,14,15,16];
    function showtext() {
        document.getElementById("demo").innerHTML="Computing...";
    }
    function show() {
        document.getElementById("demo").style.visibility="hidden";
        document.getElementById("but").style.visibility="hidden";
        document.getElementById("but2").style.visibility="hidden";
        var option = {
            title: {
                text: '排序算法效率对比'
            },
            legend: {
                data: [
                    {
                        name: 'babble'
                    }, {
                        name: 'insert'
                    }, {
                        name: 'quicksort'
                    }, {
                        name: 'shell'
                    }
                ]},
            xAxis: {
                type: 'category',
                data: [10000, 20000, 30000, 40000]
            },
            yAxis: {
                type: 'value'
            },
            series: [{name: 'babble1', data: babble1, type: 'line'}, {name: 'insert1', data: insert1, type: 'line'},{name: 'quicksort1', data: quicksort1, type: 'line'},
                {name: 'shell1', data: shell1, type: 'line'}]
        };

        myChart.setOption(option);
    }

   function run() {
        //window.onload=function () {



        //执行排序算法,显示echarts,类似于主函数
        for (let j = 1; j <= 4; j++) {//j用来区分10万、20万...
            var rnumbers1 = randomnum_gene(j)//得到随机数组,给不同的函数用
            var rnumbers2 = rnumbers1.slice(0)//得到随机数组
            var rnumbers3 = rnumbers1.slice(0)//得到随机数组
            var rnumbers4 = rnumbers1.slice(0)//得到随机数组


            //冒泡排序
            let mytime = new Date().getTime();//获取当前时间
            babble(rnumbers1);
            let mytime1 = new Date().getTime();//获取截止时间
            let runtime = mytime1 - mytime;
            babble1[j - 1] = runtime;

            //插入排序
            mytime = new Date().getTime();//获取当前时间
            insert(rnumbers2);
            mytime1 = new Date().getTime();//获取截止时间
            runtime = mytime1 - mytime;
            insert1[j - 1] = runtime;
            //快速排序
            mytime = new Date().getTime();//获取当前时间
            quicksort(rnumbers3, 0, rnumbers3.length - 1);
            mytime1 = new Date().getTime();//获取截止时间
            runtime = mytime1 - mytime;
            quicksort1[j - 1] = runtime;
            //希尔排序
            mytime = new Date().getTime();//获取当前时间
            shell(rnumbers4);
            mytime1 = new Date().getTime();//获取截止时间
            runtime = mytime1 - mytime;
            shell1[j - 1] = runtime;
        }
        //echarts

        document.getElementById("demo").innerHTML="计算完毕";
        //if(document.getElementById("demo")==="计算完毕")
        //{ document.getElementById("but").style.visibility="visible";}

    }

        function randomnum_gene(n) {   //n是1 2 3 4-------------------
            let numbers = [];
            for (let i = 0; i < (n * 10000); i++) {
                numbers.push(Math.round(Math.random() * 100000))
            }
            return numbers;

        }

        //冒泡



        function babble(numbers) {
            let size = numbers.length;
            let s1 = 0;
            for (let i = 0; i < size - 1; i++) {
                for (let j = i + 1; j < size; j++) {
                    if (numbers[i] >= numbers[j]) {
                        s1 = numbers[i];
                        numbers[i] = numbers[j];
                        numbers[j] = s1;
                    }
                }
            }

        }

        //插入排序
        function insert(numbers) {
            let size = numbers.length;
            let s1 = 0;
            for (let i = 0; i < size - 1; i++) {
                for (let j = i + 1; j < size; j++) {
                    if (numbers[i] >= numbers[j]) {
                        s1 = numbers[i];
                        numbers[i] = numbers[j];
                        numbers[j] = s1;
                    }
                }
            }
        }

        //快速排序
        function quicksort(numbers, low, high) {
            //基准值取第一个
            let key = numbers[low];
            let start = low;
            let end = high;

            while (end > start) {
                while (end > start && numbers[end] >= key) //从后往前
                {
                    end--;
                }
                while (end > start && numbers[end] < key) {
                    let temp = numbers[end];
                    numbers[end] = numbers[start];
                    numbers[start] = temp;
                }

                while (end > start && numbers[start] <= key)//从前往后
                {
                    start++;
                }
                while (end > start && numbers[start] > key) {
                    let temp = numbers[start];
                    numbers[start] = numbers[end];
                    numbers[end] = temp;
                }
            }
            if (start > low)//递归调用
            {
                quicksort(numbers, low, start - 1);
            }
            if (end < high) {
                quicksort(numbers, end + 1, high);
            }
        }

        //希尔排序
        function shell(numbers) {
            let size = numbers.length;
            let temp, gap = size / 2;
            while (gap > 0) {
                for (let i = gap; i < size; i++) {
                    temp = numbers[i];
                    let preIndex = i - gap;
                    while (preIndex >= 0 && numbers[preIndex] > temp) {
                        numbers[preIndex + gap] = numbers[preIndex];
                        preIndex -= gap;
                    }
                    numbers[preIndex + gap] = temp;
                }
                gap /= 2;
            }
        }

</script>
</body>
</html>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!