常用排序算法及代码实现

来源:互联网 发布:深圳市一号仓佳速网络 编辑:程序博客网 时间:2024/06/05 22:47

1. [代码]选择排序     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
publicclass SelectionSort {
    publicvoid selectionSort(int[] array) {
        inttemp;
        for(inti = 0; i < array.length - 1; i++) {
            for(intj = i + 1; j <= array.length - 1; j++) {// 第i个和第j个比较j可以取到最后一位,所以要用j<=array.length-1
                if(array[i] > array[j]) {// 注意和冒泡排序的区别,这里是i和j比较。
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
            // 打印每趟排序结果
            for(intm = 0; m <= array.length - 1; m++) {
                System.out.print(array[m] + "\t");
            }
            System.out.println();
        }
    }
   
    publicstatic void main(String[] args) {
        SelectionSort selectionSort = newSelectionSort();
        int[] array = { 5,69,12,3,56,789,2,5648,23};
        selectionSort.selectionSort(array);
        for(intm = 0; m <= array.length - 1; m++) {
            System.out.print(array[m] + "\t");
        }
    }
}

2. [代码]插入排序     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
publicclass InsertSort {
    publicvoid insertSort(int[] array, intfirst, intlast) {
        inttemp, i, j;
        for(i = first + 1; i <= last - 1; i++) {// 默认以第一个数为有序序列,后面的数为要插入的数。
            temp = array[i];
            j = i - 1;
            while(j >= first && array[j] > temp) {// 从后进行搜索如果搜索到的数小于temp则该数后移继续搜索,直到搜索到小于或等于temp的数即可
                array[j + 1] = array[j];
                j--;
            }
            array[j + 1] = temp;
            // 打印每次排序结果
            for(intm = 0; m <= array.length - 1; m++) {
                System.out.print(array[m] + "\t");
            }
            System.out.println();
        }
    }
   
    publicstatic void main(String[] args) {
        InsertSort insertSort = newInsertSort();
        int[] array = { 5,69,12,3,56,789,2,5648,23};
        insertSort.insertSort(array,0, array.length);// 注意此处是0-9而不是0-8
        for(inti = 0; i <= array.length - 1; i++) {
            System.out.print(array[i] + "\t");
        }
    }
}

3. [代码]归并排序     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
publicclass MergeSortTest {
    publicvoid sort(int[] array, intleft, intright) {
        if(left >= right)
            return;
        // 找出中间索引
        intcenter = (left + right) / 2;
        // 对左边数组进行递归
        sort(array, left, center);
        // 对右边数组进行递归
        sort(array, center + 1, right);
        // 合并
        merge(array, left, center, right);
        // 打印每次排序结果
        for(inti = 0; i < array.length; i++) {
            System.out.print(array[i] + "\t");
        }
        System.out.println();
   
    }
   
    /**
     * 将两个数组进行归并,归并前面2个数组已有序,归并后依然有序
     *
     * @param array
     *            数组对象
     * @param left
     *            左数组的第一个元素的索引
     * @param center
     *            左数组的最后一个元素的索引,center+1是右数组第一个元素的索引
     * @param right
     *            右数组最后一个元素的索引
     */
    publicvoid merge(int[] array, intleft, intcenter, intright) {
        // 临时数组
        int[] tmpArr = newint[array.length];
        // 右数组第一个元素索引
        intmid = center + 1;
        // third 记录临时数组的索引
        intthird = left;
        // 缓存左数组第一个元素的索引
        inttmp = left;
        while(left <= center && mid <= right) {
            // 从两个数组中取出最小的放入临时数组
            if(array[left] <= array[mid]) {
                tmpArr[third++] = array[left++];
            }else{
                tmpArr[third++] = array[mid++];
            }
        }
        // 剩余部分依次放入临时数组(实际上两个while只会执行其中一个)
        while(mid <= right) {
            tmpArr[third++] = array[mid++];
        }
        while(left <= center) {
            tmpArr[third++] = array[left++];
        }
        // 将临时数组中的内容拷贝回原数组中
        // (原left-right范围的内容被复制回原数组)
        while(tmp <= right) {
            array[tmp] = tmpArr[tmp++];
        }
    }
   
    publicstatic void main(String[] args) {
        int[] array = newint[] { 5,69,12,3,56,789,2,5648,23};
        MergeSortTest mergeSortTest = newMergeSortTest();
        mergeSortTest.sort(array,0, array.length - 1);
        System.out.println("排序后的数组:");
        for(intm = 0; m <= array.length - 1; m++) {
            System.out.print(array[m] + "\t");
        }
    }
}

4. [代码]希尔排序     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
publicclass ShellSort {
    publicvoid shellSort(int[] array, intn) {
        inti, j, gap;
        inttemp;
        for(gap = n / 2; gap > 0; gap /= 2) {// 计算gap大小
            for(i = gap; i < n; i++) {// 将数据进行分组
                for(j = i - gap; j >= 0&& array[j] > array[j + gap]; j -= gap) {// 对每组数据进行插入排序
                    temp = array[j];
                    array[j] = array[j + gap];
                    array[j + gap] = temp;
                }
                // 打印每趟排序结果
                for(intm = 0; m <= array.length - 1; m++) {
                    System.out.print(array[m] + "\t");
                }
                System.out.println();
            }
        }
    }
   
    publicstatic void main(String[] args) {
        ShellSort shellSort = newShellSort();
        int[] array = { 5,69,12,3,56,789,2,5648,23};
        shellSort.shellSort(array, array.length);// 注意为数组的个数
        for(intm = 0; m <= array.length - 1; m++) {
            System.out.print(array[m] + "\t");
        }
    }
}

5. [代码]快速排序     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
publicclass QuickSort {
    publicint partition(int[] sortArray, intlow, intheight) {
        intkey = sortArray[low];// 刚开始以第一个数为标志数据
        while(low < height) {
            while(low < height && sortArray[height] >= key)
                height--;// 从后面开始找,找到比key值小的数为止
            sortArray[low] = sortArray[height];// 将该数放到key值的左边
            while(low < height && sortArray[low] <= key)
                low++;// 从前面开始找,找到比key值大的数为止
            sortArray[height] = sortArray[low];// 将该数放到key值的右边
        }
        sortArray[low] = key;// 把key值填充到low位置,下次重新找key值
        // 打印每次排序结果
        for(inti = 0; i <= sortArray.length - 1; i++) {
            System.out.print(sortArray[i] + "\t");
        }
        System.out.println();
        returnlow;
    }
   
    publicvoid sort(int[] sortArray, intlow, intheight) {
        if(low < height) {
            intresult = partition(sortArray, low, height);
            sort(sortArray, low, result - 1);
            sort(sortArray, result + 1, height);
        }
    }
   
    publicstatic void main(String[] args) {
        QuickSort quickSort = newQuickSort();
        int[] array = { 5,69,12,3,56,789,2,5648,23};
        for(inti = 0; i <= array.length - 1; i++) {
            System.out.print(array[i] + "\t");
        }
        System.out.println();
        quickSort.sort(array,0,8);
        for(inti = 0; i <= array.length - 1; i++) {
            System.out.print(array[i] + "\t");
        }
    }
}
0 0
原创粉丝点击