JavaScript之排序算法

31 阅读1分钟

​本文已参与「新人创作礼」活动,一起开启掘金创作之路

 本文主要介绍三种排序算法,分别是冒泡排序、选择排序和快速排序。为方便测试,本文会随机生成长度为10的数组,数组元素的值在[1,20]之间。

1.冒泡排序

核心思路:每一轮相邻两个数进行比较,选出一个最大值。(若数组长度为n,则比较轮数为n-1,首轮比较次数为n-1,后续比较次数依次减1.)

代码如下:

<!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>Document</title>
</head>

<body>
    <script>
        var a = [],
            tem;
        for (var z = 0; z < 10; z++) {
            a[z] = Math.floor(Math.random() * 20 + 1);
        }
        console.log('排序前:');
        console.log(a);
        for (var i = 0; i < a.length - 1; i++) {
            for (var j = 0; j < a.length - 1 - i; j++) {
                if (a[j] > a[j + 1]) {
                    tem = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = tem;
                }
            }
        }
        console.log('排序后:');
        console.log(a);
    </script>
</body>

</html>

2.选择排序

核心思路:每一轮找出数组元素中的最小值,若最小值不为首个元素,则与首个元素进行交换。(若数组长度为n,则需要找n-1轮,首轮比较次数为n-1,后续比较次数依次减1)

代码如下:

<!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>Document</title>
</head>

<body>
    <script>
        var a = [],
            tem;
        for (var z = 0; z < 10; z++) {
            a[z] = Math.floor(Math.random() * 20 + 1);
        }
        console.log('排序前:');
        console.log(a);
        for (var i = 0; i < a.length - 1; i++) {
            index = i;
            for (var j = 1 + i; j < a.length; j++) {
                if (a[index] > a[j]) {
                    index = j;
                }
            }
            if (index != i) {
                tem = a[i];
                a[i] = a[index];
                a[index] = tem;
            }
        }
        console.log('排序后:');
        console.log(a);
    </script>
</body>

</html>

3.快速排序:

核心思路(递归思想):获取数组中位于中间的元素,将其余各元素与之比较,小于它的数存在定义的左数组中,另外的数存在定义的右数组中,最后按照左数组、中间元素、右数组的顺序进行连接。按照这种方法再操作左右数组,直至排序完成。

代码如下:

<!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>Document</title>
</head>

<body>
    <script>
        var a = [];
        for (var z = 0; z < 10; z++) {
            a[z] = Math.floor(Math.random() * 20 + 1);
        }
        console.log('排序前:');
        console.log(a);

        function quickSort(a) {
            if (a.length <= 1)
                return a;
            var middle = Math.floor(a.length / 2),
                left = [],
                right = [];
            for (var i = 0; i < a.length; i++) {
                if (i == middle) {
                    continue
                } else if (a[i] < a[middle]) {
                    left.push(a[i])
                } else {
                    right.push(a[i])
                }
            }
            return quickSort(left).concat(a[middle], quickSort(right));
        }
        console.log('排序后:');
        console.log(quickSort(a));
    </script>
</body>

</html>

\