[JS data structure and algorithm] Encapsulation of sorting algorithm

Simple sorting: bubble sorting, selection sorting, insertion sorting
Advanced sorting: Hill sorting, quick sorting, heap sorting, merge sorting

Package list

Before starting to write the sorting algorithm, encapsulate an array (list) to store data, define the corresponding attributes, and then encapsulate different sorting algorithms on the prototype.

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

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

<body>
    <script>
        // 封装列表类
        function ArrayList(){
     
     
            // 属性
            this.array = [];

            // 方法
            // 将数据可以插入到数组中的方法
            ArrayList.prototype.insert = function(item){
     
     
                this.array.push(item);
            }

            // toString,输出数据,方便测试
            ArrayList.prototype.toString = function(){
     
     
                return this.array.join('-');
            }

            // 实现排序的算法
            // 冒泡排序

            // 选择排序

            // 插入排序

            // 希尔排序

            // 快速排序
        }

        // 测试实例
        var arrayList = new ArrayList();
    </script>
</body>
</html>

After encapsulation, let us enter the sorting algorithm together!
[JS Data Structure and Algorithm] Implementation of bubbling, selection, and insertion sorting algorithms

Guess you like

Origin blog.csdn.net/weixin_42339197/article/details/102880195