js代码实现希尔排序和快速排序

1.希尔排序:
把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至 1 时,整个文件恰被分成一组,算法便终止。
2.快速排序:
(1)首先设定一个分界值,通过该分界值将数组分成左右两部分。
(2)将大于或等于分界值的数据集中到数组右边,小于分界值的数据集中到数组的左边。此时,左边部分中各元素都小于分界值,而右边部分中各元素都大于或等于分界值。
(3)然后,左边和右边的数据可以独立排序。对于左侧的数组数据,又可以取一个分界值,将该部分数据分成左右两部分,同样在左边放置较小值,右边放置较大值。右侧的数组数据也可以做类似处理。
(4)重复上述过程,可以看出,这是一个递归定义。通过递归将左侧部分排好序后,再递归排好右侧部分的顺序。当左、右两个部分各数据排序完成后,整个数组的排序也就完成了。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>排序算法</title>
</head>

<body>

    <script>
        //封装ArrayList
        function ArrayList() {
            this.items = []
        }
        //
        ArrayList.prototype.insert = function (data) {
            this.items.push(data)
        }
        //toSting 方法
        ArrayList.prototype.toSting = function () {
            return this.items.join('_')
        }
          //希尔排序
        ArrayList.prototype.shellSort = function () {
            //获取数组长度
            var size = this.items.length
            //初始化增量(间隔)
            var gap = Math.floor(size / 2)
            while (gap >= 1) {
                //gap分组,分组进行排序
                for (var i = gap; i < size; i++) {
                    var temp = this.items[i]
                    var j = i
                    while (this.items[j - gap] > temp & j > gap - 1) {
                        this.items[j] = this.items[j - gap]
                        j -= gap
                    }
                    this.items[j] = temp
                }
                gap = Math.floor(gap / 2)
            }
        }
        //快速排序
        //选择枢纽
        ArrayList.prototype.middle=function(left, right) {
            //取出中间位置
            var center = Math.floor((left + right) / 2)
            //三者先进行排序,由小到大
            var temp = this.items[center]
            if (this.items[left] > this.items[center]) {
                this.items[center] = this.items[left]
                this.items[left] = temp
            }
            if (this.items[center] > this.items[right]) {
                temp = this.items[center]
                this.items[center] = this.items[right]
                this.items[right] = temp
            }
            if (this.items[left] > this.items[center]) {
                temp = this.items[center]
                this.items[center] = this.items[left]
                this.items[left] = temp
            }
            //将center 放到right-1 位置
            temp = this.items[center]
            this.items[center] = this.items[right - 1]
            this.items[right - 1] = temp
            return this.items[right - 1]

        }
        //快速排序递归
        ArrayList.prototype.recursionQuick=function(left, right) {
            //结束条件
            if (left >= right) return;
            //寻找中位值
            var pivot = this.middle(left, right)
            //定义变量,记录当前位置
            var i = left
            var j = right - 1
            var temp = ''
            //进行交换
            while (true) {
                while (this.items[++i] < pivot) {
                  
                 }
                while (this.items[--j] > pivot) { 
                   
                }
                if (i < j) {
                    temp = this.items[i]
                    this.items[i] = this.items[j]
                    this.items[j] = temp
                } else {
                    break
                }
            }
            //将枢纽放在正确位置
            temp = this.items[i]
            this.items[i] = this.items[right-1]
            this.items[right-1] = temp
            //对枢纽两边进行排序
            this.recursionQuick(left,i-1)
            this.recursionQuick(i+1,right)

        }
        ArrayList.prototype.quickSort = function () {
            this.recursionQuick(0, this.items.length - 1)
        }
        //测试
        var al = new ArrayList()
        al.insert(88)
        al.insert(66)
        al.insert(33)
        al.insert(11)
        al.insert(55)
        al.insert(100)
        console.log(al.toSting())
        al.shellSort()
        console.log(al.toSting())
        al.insert(22)
        al.insert(99)
        al.insert(44)
        al.insert(77)
        al.quickSort()
        console.log(al.toSting())
    </script>
</body>

</html>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值