问答详情
源自:4-10 编程练习

冒泡排序怎么写?

冒泡排序怎么写?

提问者:慕后端6438115 2016-06-02 10:37

个回答

  • wxs_
    2016-06-02 11:53:02

    <script type="text/javascript">
    
            var temp=0, i, j;
            var arr = [2,5,4,3];
            for (i = 0; i < arr.length; i++)
            {
                //j必须从i的下一个位置数开始
                for (j = i+1; j < arr.length-i; j++)
                {
                    if (arr[i] >arr[j + 1])
                    {
                        temp = arr[i];
                        arr[i] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                    else { arr[i] = arr[i];}
                    
                } 
                document.write(arr[i]);
            }
            
        </script>


  • qq_稗草_0
    2016-06-02 10:46:57

    function Sort(x)

    {

    for(var i=0;i<x.length;i++)

        {

            for(var j=i+1;j<x.length;j++)

            {

                if(x[i]>x[j])

                    {

                var temp=x[i];
            x[i]=x[j];
            x[j]=temp;

                    }

                }

        }

    }