自学内容网 自学内容网

使用java实现快速排序算法的性能测试

Date: 2024.07.12 16:32:32 author: lijianzhan

**简述:**在我的上一篇文章中简单的提到过算法,关于算法,现在再次的说明一下,算法是指在解决问题时,按照某种机械步骤一定可以得到问题结果的处理过程,一个算法的质量优劣将影响到算法乃至程序的效率,而一个算法的评价主要从时间复杂度和空间复杂度来考虑。
**快速排序的简述以及原理:**快速排序是一种非常高效的排序算法,它的实现,增大了记录的比较和移动的距离,将关键字较大的记录从前面直接移动到后面,关键字较小的记录从后面直接移动到前面,从而减少了总的比较次数和移动次数。同时采用“分而治之”的思想,把大的拆分为小的,小的拆分为更小的,其原理如下:对于给定的一组记录,选择一个基准元素,通常选择第一个元素或者最后一个元素,通过一趟扫描,将待排序列分成两部分,一部分比基准元素小,一部分大于等于基准元素,此时基准元素在其排好序后的正确位置,然后再用同样的方法递归地排序划分的两部分,直到序列中的所有记录均有序为止。

随机获取数组长度为1000的初始化动态数组并进行快速排序算法程序的秒级计算,以及获取快速排序后的数组,JAVA编码过程,如下图示例:

package com.sort.wrap;

import java.util.Arrays;
import java.util.Random;

public class NewTestProgram2 {
    public static void main(String[] args) {
        // 定义数组大小
        int size = 1000;

        // 定义随机数组范围
        int min = 0;
        int max = 10000;

        // 创建Random对象
        Random random = new Random();

        // 创建整数数组并填充随机数
        int[] arr = new int[size];
        for (int i = 0; i < size; i++) {
            arr[i] = random.nextInt(max - min + 1) + min;
        }
        System.out.println("随机生成的数组:"+ Arrays.toString(arr));

        long currentTimeStart = System.currentTimeMillis();
        quickSort(arr);
        long currentTimeEnd = System.currentTimeMillis();

        long timeConsuming = currentTimeEnd - currentTimeStart;
        System.out.println("快速排序消耗时长:"+ timeConsuming + "秒");

        System.out.println("快速排序后的数组:"+ Arrays.toString(arr));
    }

    /**
     * 快速排序方法
     * */
    public static void quickSort(int data[]) {
        sort(data, 0, data.length - 1);
    }

    public static void sort(int data[], int low, int hight) {
        int i, j, index;
        if (low > hight) {
            return;
        }
        i = low;
        j = hight;

        // 用子表的第一个记录做基准
        index = data[i];
        // 从表的两端交替向中间扫描
        while (i < j) {
            while (i < j && data[j] >= index)
                j--;
            // 用比基准小的记录替换低位记录
            if (i < j)
                data[i++] = data[j];
            while (i < j && data[i] < index)
                i++;
            // 用比基准大的记录替换高位记录
            if (i < j)
                data[j--] = data[i];
        }
        // 将基准数值替换回 a[i]
        data[i] = index;
        // 对低子表进行递归排序
        sort(data, low, i - 1);
        // 对高子表进行递归排序
        sort(data, i + 1, hight);
    }

}

输出的结果为:

"C:\Program Files\Java\jdk-21\bin\java.exe" -Dfile.encoding=GBK "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.4\lib\idea_rt.jar=60306:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.4\bin" -classpath "C:\Program Files\Java\jdk-21;C:\Users\admin\IdeaProjects\test\out\production\test" com.sort.wrap.NewTestProgram2
随机生成的数组:[2530, 658, 6140, 856, 9422, 3148, 3221, 7598, 1209, 2134, 993, 2745, 6358, 3378, 7782, 8632, 6017, 1879, 7354, 1200, 5445, 8675, 88, 7199, 6179, 4869, 498, 976, 2678, 1212, 1288, 8332, 6344, 7207, 7555, 8754, 8632, 2239, 6184, 2736, 7408, 6245, 8031, 504, 2805, 4398, 9431, 5202, 8124, 6139, 3754, 4740, 7742, 6918, 179, 2833, 7912, 6079, 6233, 1518, 2797, 9322, 1379, 1490, 1344, 8329, 3464, 9396, 1061, 5720, 4186, 8682, 5242, 2282, 2340, 8408, 6585, 419, 3401, 6108, 3321, 7848, 306, 833, 7425, 7587, 8613, 7356, 2008, 6424, 2925, 2339, 1110, 7896, 8507, 7693, 2715, 161, 3154, 3891, 3473, 8916, 469, 6411, 9272, 6638, 7051, 6472, 9900, 693, 6356, 8245, 8031, 3344, 7598, 9340, 2342, 4691, 436, 1739, 3839, 7590, 5570, 1573, 549, 2654, 1359, 2671, 8336, 2145, 4511, 3706, 4121, 5620, 3391, 9050, 132, 4867, 9912, 8906, 966, 8109, 4249, 3368, 5218, 2184, 903, 4546, 6651, 2260, 4910, 96, 5633, 1998, 7955, 1513, 5653, 6640, 2477, 6681, 3889, 3111, 8983, 7838, 2253, 5917, 4781, 2415, 3975, 9558, 9665, 3384, 3220, 3123, 4852, 2894, 4455, 1600, 7180, 7782, 3421, 8533, 4192, 7949, 3171, 590, 763, 6093, 6571, 1262, 9445, 4085, 4637, 4239, 1, 1003, 9973, 2262, 6215, 1631, 4252, 2763, 3705, 6863, 2743, 1768, 4062, 8593, 9430, 8087, 4616, 7481, 6237, 2985, 5518, 3498, 3939, 1896, 1434, 5649, 2071, 2971, 3062, 6882, 6975, 7451, 1487, 1886, 2494, 243, 4917, 7195, 6992, 7438, 9537, 9627, 5456, 1906, 2116, 2009, 7846, 7545, 9772, 1937, 4569, 9289, 5132, 2346, 7653, 4888, 8565, 4713, 2474, 3537, 5616, 4446, 2066, 677, 1958, 7790, 2631, 1410, 5417, 4368, 1676, 767, 744, 3720, 1168, 3845, 4967, 8695, 9557, 7955, 519, 8138, 5987, 9726, 1673, 1697, 7948, 7445, 8859, 9546, 3727, 2717, 7207, 3972, 3851, 6105, 3038, 5041, 2597, 205, 9757, 5551, 8044, 6224, 4903, 8835, 9546, 7050, 8508, 7924, 5229, 596, 2259, 9761, 8612, 7992, 3194, 157, 2926, 2122, 6760, 8688, 8181, 998, 1093, 4690, 1294, 6962, 4947, 5796, 8165, 5413, 7771, 7065, 1432, 6141, 1100, 905, 4299, 3078, 3641, 8021, 1866, 6254, 1010, 7228, 1790, 3455, 2551, 7309, 8520, 2929, 7701, 4050, 826, 417, 338, 9829, 3639, 7780, 5013, 9514, 4176, 4917, 6209, 3513, 2201, 8992, 5370, 3068, 9007, 3829, 9883, 5298, 4544, 5364, 5169, 3398, 6704, 7036, 3189, 7519, 2573, 4018, 7278, 9916, 1146, 7613, 3752, 2072, 1560, 6723, 2094, 4077, 8089, 9010, 6557, 5186, 4757, 3653, 3297, 399, 9005, 2768, 887, 3994, 5007, 8530, 5519, 5623, 7653, 700, 3297, 1358, 7145, 5847, 5118, 6788, 2586, 3873, 7164, 9993, 6426, 7369, 8913, 1576, 5159, 5919, 7384, 891, 8209, 1908, 1612, 3208, 6269, 6580, 3909, 1993, 2957, 8050, 7144, 2660, 2202, 3188, 2519, 176, 7504, 7871, 2512, 718, 9014, 7424, 2133, 7189, 4380, 7364, 6190, 8342, 1084, 6186, 4026, 1954, 3407, 5192, 3713, 367, 7926, 7930, 9020, 7825, 2331, 9513, 7136, 3056, 1705, 420, 7038, 1528, 5814, 8699, 2592, 6386, 7422, 7015, 7529, 5636, 5588, 9277, 7440, 7201, 6869, 8972, 7687, 3220, 5120, 2312, 2024, 5223, 3144, 9719, 7944, 8548, 8896, 1166, 2347, 2196, 9906, 5388, 1812, 2356, 6907, 8177, 7497, 562, 7624, 8047, 4238, 8220, 5564, 5085, 3399, 9663, 4089, 2888, 507, 7973, 309, 9265, 8602, 4787, 9634, 4781, 5899, 9216, 4993, 3361, 6175, 9460, 9461, 9481, 901, 6266, 9033, 461, 249, 6587, 645, 7546, 589, 2246, 4110, 2282, 8988, 6008, 8060, 7692, 7604, 5241, 6161, 2662, 3648, 9633, 9311, 3829, 3043, 6743, 5902, 7925, 6171, 510, 3314, 5452, 9551, 9634, 5338, 4419, 7153, 8145, 3805, 597, 6000, 5648, 9861, 4195, 2349, 5027, 3865, 5888, 8890, 2104, 3117, 9386, 6880, 7862, 3572, 2073, 1091, 2618, 890, 8682, 5583, 2441, 756, 7517, 42, 8569, 3839, 1768, 7041, 8960, 5875, 6714, 533, 3896, 9997, 2715, 9659, 1653, 6459, 6570, 7580, 7308, 910, 4942, 1123, 73, 185, 4386, 4141, 9227, 4809, 1121, 7883, 8069, 4797, 2782, 4713, 5791, 284, 400, 8616, 4729, 4317, 4412, 8855, 3122, 957, 917, 4466, 6987, 2847, 7800, 8477, 8793, 9603, 2645, 5527, 9186, 862, 7485, 8977, 1790, 4377, 5086, 328, 8766, 2074, 112, 5961, 4836, 3828, 4075, 2760, 5292, 439, 3579, 7660, 5026, 1371, 4863, 6820, 7793, 8339, 9064, 4019, 7937, 1461, 3677, 7472, 3703, 1241, 3386, 8523, 1462, 9614, 9831, 5403, 7535, 2997, 9244, 5871, 1224, 5877, 8648, 7260, 3931, 7714, 4674, 3753, 3396, 7065, 9317, 5069, 6512, 3799, 3644, 1479, 157, 7842, 7955, 4387, 3693, 2917, 963, 5836, 5118, 6436, 2683, 6979, 8450, 107, 8121, 3914, 1752, 1473, 2534, 5131, 5548, 8009, 1400, 3952, 1614, 6638, 4293, 4206, 4002, 4526, 5082, 7632, 2220, 1221, 3727, 8586, 9907, 2493, 7126, 5857, 5518, 7635, 9512, 4857, 5265, 7465, 5091, 1814, 9662, 9518, 9882, 2403, 3042, 9904, 5906, 4558, 4733, 5374, 6326, 7381, 2777, 7965, 6013, 258, 7781, 2537, 9066, 7398, 211, 5623, 3425, 4182, 6793, 3158, 4463, 1082, 1783, 694, 692, 7852, 4994, 5534, 7471, 4916, 2564, 4925, 8522, 4015, 1737, 509, 3792, 2389, 815, 3348, 7852, 7127, 5607, 5535, 991, 3080, 2710, 517, 930, 5740, 6772, 6319, 719, 7080, 9238, 6989, 6156, 1577, 4552, 3286, 6203, 7795, 3744, 4781, 8669, 8386, 5622, 4546, 771, 6715, 9114, 2028, 404, 5036, 2505, 2108, 5128, 8323, 5868, 4289, 8080, 7551, 3064, 8228, 4560, 4439, 8962, 6845, 1513, 127, 8703, 1076, 8476, 6392, 5548, 4066, 4905, 5398, 125, 4030, 5469, 9422, 6576, 4345, 4780, 941, 6773, 8178, 6821, 5554, 6403, 4159, 8161, 4336, 7554, 4628, 5038, 9759, 693, 2224, 9626, 614, 6717, 429, 7187, 1779, 6821, 2099, 9668, 9154, 3487, 3347, 4225, 2178, 4024, 7196, 4851, 4231, 668, 7032, 2482, 7526, 3415, 1498, 6542, 6050, 8173, 6935, 2656, 180, 8335, 2139, 604, 8453, 4048, 253, 5738, 2596, 8151, 6713, 710, 2623, 6418, 3194, 81, 2236, 5851, 2440, 5551, 6475, 174, 4287, 8874, 5223, 5647, 5328, 7237, 2865, 466, 4301, 7963, 552, 7316, 3875, 3529, 5278, 5205, 4750, 1043, 3321, 6252, 1479, 6151, 8060, 3857, 7594, 6648, 3458, 1583, 6306, 8637, 159, 8373, 9232, 8596, 6767, 3631, 2987, 8142, 1007, 454, 4201, 1307, 1265, 8050, 2134, 8257, 2513, 9636, 7876, 8600, 6737, 8591, 4106, 9453, 7652, 3097, 4217, 3588, 8516, 8597, 421, 1760, 6274, 150]
快速排序消耗时长:1秒
快速排序后的数组:[1, 42, 73, 81, 88, 96, 107, 112, 125, 127, 132, 150, 157, 157, 159, 161, 174, 176, 179, 180, 185, 205, 211, 243, 249, 253, 258, 284, 306, 309, 328, 338, 367, 399, 400, 404, 417, 419, 420, 421, 429, 436, 439, 454, 461, 466, 469, 498, 504, 507, 509, 510, 517, 519, 533, 549, 552, 562, 589, 590, 596, 597, 604, 614, 645, 658, 668, 677, 692, 693, 693, 694, 700, 710, 718, 719, 744, 756, 763, 767, 771, 815, 826, 833, 856, 862, 887, 890, 891, 901, 903, 905, 910, 917, 930, 941, 957, 963, 966, 976, 991, 993, 998, 1003, 1007, 1010, 1043, 1061, 1076, 1082, 1084, 1091, 1093, 1100, 1110, 1121, 1123, 1146, 1166, 1168, 1200, 1209, 1212, 1221, 1224, 1241, 1262, 1265, 1288, 1294, 1307, 1344, 1358, 1359, 1371, 1379, 1400, 1410, 1432, 1434, 1461, 1462, 1473, 1479, 1479, 1487, 1490, 1498, 1513, 1513, 1518, 1528, 1560, 1573, 1576, 1577, 1583, 1600, 1612, 1614, 1631, 1653, 1673, 1676, 1697, 1705, 1737, 1739, 1752, 1760, 1768, 1768, 1779, 1783, 1790, 1790, 1812, 1814, 1866, 1879, 1886, 1896, 1906, 1908, 1937, 1954, 1958, 1993, 1998, 2008, 2009, 2024, 2028, 2066, 2071, 2072, 2073, 2074, 2094, 2099, 2104, 2108, 2116, 2122, 2133, 2134, 2134, 2139, 2145, 2178, 2184, 2196, 2201, 2202, 2220, 2224, 2236, 2239, 2246, 2253, 2259, 2260, 2262, 2282, 2282, 2312, 2331, 2339, 2340, 2342, 2346, 2347, 2349, 2356, 2389, 2403, 2415, 2440, 2441, 2474, 2477, 2482, 2493, 2494, 2505, 2512, 2513, 2519, 2530, 2534, 2537, 2551, 2564, 2573, 2586, 2592, 2596, 2597, 2618, 2623, 2631, 2645, 2654, 2656, 2660, 2662, 2671, 2678, 2683, 2710, 2715, 2715, 2717, 2736, 2743, 2745, 2760, 2763, 2768, 2777, 2782, 2797, 2805, 2833, 2847, 2865, 2888, 2894, 2917, 2925, 2926, 2929, 2957, 2971, 2985, 2987, 2997, 3038, 3042, 3043, 3056, 3062, 3064, 3068, 3078, 3080, 3097, 3111, 3117, 3122, 3123, 3144, 3148, 3154, 3158, 3171, 3188, 3189, 3194, 3194, 3208, 3220, 3220, 3221, 3286, 3297, 3297, 3314, 3321, 3321, 3344, 3347, 3348, 3361, 3368, 3378, 3384, 3386, 3391, 3396, 3398, 3399, 3401, 3407, 3415, 3421, 3425, 3455, 3458, 3464, 3473, 3487, 3498, 3513, 3529, 3537, 3572, 3579, 3588, 3631, 3639, 3641, 3644, 3648, 3653, 3677, 3693, 3703, 3705, 3706, 3713, 3720, 3727, 3727, 3744, 3752, 3753, 3754, 3792, 3799, 3805, 3828, 3829, 3829, 3839, 3839, 3845, 3851, 3857, 3865, 3873, 3875, 3889, 3891, 3896, 3909, 3914, 3931, 3939, 3952, 3972, 3975, 3994, 4002, 4015, 4018, 4019, 4024, 4026, 4030, 4048, 4050, 4062, 4066, 4075, 4077, 4085, 4089, 4106, 4110, 4121, 4141, 4159, 4176, 4182, 4186, 4192, 4195, 4201, 4206, 4217, 4225, 4231, 4238, 4239, 4249, 4252, 4287, 4289, 4293, 4299, 4301, 4317, 4336, 4345, 4368, 4377, 4380, 4386, 4387, 4398, 4412, 4419, 4439, 4446, 4455, 4463, 4466, 4511, 4526, 4544, 4546, 4546, 4552, 4558, 4560, 4569, 4616, 4628, 4637, 4674, 4690, 4691, 4713, 4713, 4729, 4733, 4740, 4750, 4757, 4780, 4781, 4781, 4781, 4787, 4797, 4809, 4836, 4851, 4852, 4857, 4863, 4867, 4869, 4888, 4903, 4905, 4910, 4916, 4917, 4917, 4925, 4942, 4947, 4967, 4993, 4994, 5007, 5013, 5026, 5027, 5036, 5038, 5041, 5069, 5082, 5085, 5086, 5091, 5118, 5118, 5120, 5128, 5131, 5132, 5159, 5169, 5186, 5192, 5202, 5205, 5218, 5223, 5223, 5229, 5241, 5242, 5265, 5278, 5292, 5298, 5328, 5338, 5364, 5370, 5374, 5388, 5398, 5403, 5413, 5417, 5445, 5452, 5456, 5469, 5518, 5518, 5519, 5527, 5534, 5535, 5548, 5548, 5551, 5551, 5554, 5564, 5570, 5583, 5588, 5607, 5616, 5620, 5622, 5623, 5623, 5633, 5636, 5647, 5648, 5649, 5653, 5720, 5738, 5740, 5791, 5796, 5814, 5836, 5847, 5851, 5857, 5868, 5871, 5875, 5877, 5888, 5899, 5902, 5906, 5917, 5919, 5961, 5987, 6000, 6008, 6013, 6017, 6050, 6079, 6093, 6105, 6108, 6139, 6140, 6141, 6151, 6156, 6161, 6171, 6175, 6179, 6184, 6186, 6190, 6203, 6209, 6215, 6224, 6233, 6237, 6245, 6252, 6254, 6266, 6269, 6274, 6306, 6319, 6326, 6344, 6356, 6358, 6386, 6392, 6403, 6411, 6418, 6424, 6426, 6436, 6459, 6472, 6475, 6512, 6542, 6557, 6570, 6571, 6576, 6580, 6585, 6587, 6638, 6638, 6640, 6648, 6651, 6681, 6704, 6713, 6714, 6715, 6717, 6723, 6737, 6743, 6760, 6767, 6772, 6773, 6788, 6793, 6820, 6821, 6821, 6845, 6863, 6869, 6880, 6882, 6907, 6918, 6935, 6962, 6975, 6979, 6987, 6989, 6992, 7015, 7032, 7036, 7038, 7041, 7050, 7051, 7065, 7065, 7080, 7126, 7127, 7136, 7144, 7145, 7153, 7164, 7180, 7187, 7189, 7195, 7196, 7199, 7201, 7207, 7207, 7228, 7237, 7260, 7278, 7308, 7309, 7316, 7354, 7356, 7364, 7369, 7381, 7384, 7398, 7408, 7422, 7424, 7425, 7438, 7440, 7445, 7451, 7465, 7471, 7472, 7481, 7485, 7497, 7504, 7517, 7519, 7526, 7529, 7535, 7545, 7546, 7551, 7554, 7555, 7580, 7587, 7590, 7594, 7598, 7598, 7604, 7613, 7624, 7632, 7635, 7652, 7653, 7653, 7660, 7687, 7692, 7693, 7701, 7714, 7742, 7771, 7780, 7781, 7782, 7782, 7790, 7793, 7795, 7800, 7825, 7838, 7842, 7846, 7848, 7852, 7852, 7862, 7871, 7876, 7883, 7896, 7912, 7924, 7925, 7926, 7930, 7937, 7944, 7948, 7949, 7955, 7955, 7955, 7963, 7965, 7973, 7992, 8009, 8021, 8031, 8031, 8044, 8047, 8050, 8050, 8060, 8060, 8069, 8080, 8087, 8089, 8109, 8121, 8124, 8138, 8142, 8145, 8151, 8161, 8165, 8173, 8177, 8178, 8181, 8209, 8220, 8228, 8245, 8257, 8323, 8329, 8332, 8335, 8336, 8339, 8342, 8373, 8386, 8408, 8450, 8453, 8476, 8477, 8507, 8508, 8516, 8520, 8522, 8523, 8530, 8533, 8548, 8565, 8569, 8586, 8591, 8593, 8596, 8597, 8600, 8602, 8612, 8613, 8616, 8632, 8632, 8637, 8648, 8669, 8675, 8682, 8682, 8688, 8695, 8699, 8703, 8754, 8766, 8793, 8835, 8855, 8859, 8874, 8890, 8896, 8906, 8913, 8916, 8960, 8962, 8972, 8977, 8983, 8988, 8992, 9005, 9007, 9010, 9014, 9020, 9033, 9050, 9064, 9066, 9114, 9154, 9186, 9216, 9227, 9232, 9238, 9244, 9265, 9272, 9277, 9289, 9311, 9317, 9322, 9340, 9386, 9396, 9422, 9422, 9430, 9431, 9445, 9453, 9460, 9461, 9481, 9512, 9513, 9514, 9518, 9537, 9546, 9546, 9551, 9557, 9558, 9603, 9614, 9626, 9627, 9633, 9634, 9634, 9636, 9659, 9662, 9663, 9665, 9668, 9719, 9726, 9757, 9759, 9761, 9772, 9829, 9831, 9861, 9882, 9883, 9900, 9904, 9906, 9907, 9912, 9916, 9973, 9993, 9997]

进程已结束,退出代码0

使用Debugger方式进行代码追踪,如下图示例:
在这里插入图片描述

总结:得到结果为运行程序执行快速排序算法时长约为0秒,根据平均运行速度得出时间复杂度是O(nlogn),得出总结,从程序运行时间看,快速排序的算法性能比冒泡排序的算法性能更好,效率更高。


原文地址:https://blog.csdn.net/qq_44304677/article/details/140382890

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!