多语言展示
当前在线:548今日阅读:103今日分享:49

算法——冒泡排序,选择排序,插入排序

在算法中,有几种常用且重要的排序算法一般需要掌握,如冒泡排序,选择排序,插入排序。这3种排序算法的思想不同,但是又有点相似。以下提供这3种算法的实例代码,作以区分比较。
方法/步骤
2

24,36,12,25,3,67,55,24,36,12,25,3,67,55,12,36,24,25,3,67,55,12,36,24,25,3,67,55,3,36,24,25,12,67,55,3,36,24,25,12,67,55,3,36,24,25,12,67,55,第1次排序结果:3,36,24,25,12,67,55,3,36,24,25,12,67,55,3,24,36,25,12,67,55,3,24,36,25,12,67,55,3,12,36,25,24,67,55,3,12,36,25,24,67,55,3,12,36,25,24,67,55,第2次排序结果:3,12,36,25,24,67,55,3,12,36,25,24,67,55,3,12,25,36,24,67,55,3,12,24,36,25,67,55,3,12,24,36,25,67,55,3,12,24,36,25,67,55,第3次排序结果:3,12,24,36,25,67,55,3,12,24,36,25,67,55,3,12,24,25,36,67,55,3,12,24,25,36,67,55,3,12,24,25,36,67,55,第4次排序结果:3,12,24,25,36,67,55,3,12,24,25,36,67,55,3,12,24,25,36,67,55,3,12,24,25,36,67,55,第5次排序结果:3,12,24,25,36,67,55,3,12,24,25,36,67,55,3,12,24,25,36,55,67,第6次排序结果:3,12,24,25,36,55,67,最终排序结果:3 12 24 25 36 55 67

3

public class SelectSort {    public static void main(String[] args) {        int[] line = {23,17,5,8,1,25,33};        selectSort(line);    }        //思路:在每步中选择最小数来重新排列    public static void selectSort(int[] line){        for(int i=0;iline[j]){                    int temp = line[i];                    line[i] = line[j];                    line[j] = temp;                }                for(int k=0;k

4

23,17,5,8,1,25,33,17,23,5,8,1,25,33,5,23,17,8,1,25,33,5,23,17,8,1,25,33,1,23,17,8,5,25,33,1,23,17,8,5,25,33,1,23,17,8,5,25,33,第1次排序结果:1 23 17 8 5 25 33 1,23,17,8,5,25,33,1,17,23,8,5,25,33,1,8,23,17,5,25,33,1,5,23,17,8,25,33,1,5,23,17,8,25,33,1,5,23,17,8,25,33,第2次排序结果:1 5 23 17 8 25 33 1,5,23,17,8,25,33,1,5,17,23,8,25,33,1,5,8,23,17,25,33,1,5,8,23,17,25,33,1,5,8,23,17,25,33,第3次排序结果:1 5 8 23 17 25 33 1,5,8,23,17,25,33,1,5,8,17,23,25,33,1,5,8,17,23,25,33,1,5,8,17,23,25,33,第4次排序结果:1 5 8 17 23 25 33 1,5,8,17,23,25,33,1,5,8,17,23,25,33,1,5,8,17,23,25,33,第5次排序结果:1 5 8 17 23 25 33 1,5,8,17,23,25,33,1,5,8,17,23,25,33,第6次排序结果:1 5 8 17 23 25 33 1,5,8,17,23,25,33,第7次排序结果:1 5 8 17 23 25 33 最终结果:1,5,8,17,23,25,33,

5

public class InsertSort {    public static void main(String[] args) {        int[] line = {34,27,15,17,28,2,33,5,64};        insertSort(line);    }        //对未排序的数据执行插入到合适位置进行排序        public static void insertSort(int[] line){        int i, j, k;        for(i = 1; i < line.length; i++) {            for(j = i; j > 0; j--) {                int data;                if(line[j-1] > line[j]) {                    data = line[j];                    line[j] = line[j-1];                    line[j-1] = data;                }                for(k=0;k

6

27,34,15,17,28,2,33,5,64,第1次排序结果:27 34 15 17 28 2 33 5 64 27,15,34,17,28,2,33,5,64,15,27,34,17,28,2,33,5,64,第2次排序结果:15 27 34 17 28 2 33 5 64 15,27,17,34,28,2,33,5,64,15,17,27,34,28,2,33,5,64,15,17,27,34,28,2,33,5,64,第3次排序结果:15 17 27 34 28 2 33 5 64 15,17,27,28,34,2,33,5,64,15,17,27,28,34,2,33,5,64,15,17,27,28,34,2,33,5,64,15,17,27,28,34,2,33,5,64,第4次排序结果:15 17 27 28 34 2 33 5 64 15,17,27,28,2,34,33,5,64,15,17,27,2,28,34,33,5,64,15,17,2,27,28,34,33,5,64,15,2,17,27,28,34,33,5,64,2,15,17,27,28,34,33,5,64,第5次排序结果:2 15 17 27 28 34 33 5 64 2,15,17,27,28,33,34,5,64,2,15,17,27,28,33,34,5,64,2,15,17,27,28,33,34,5,64,2,15,17,27,28,33,34,5,64,2,15,17,27,28,33,34,5,64,2,15,17,27,28,33,34,5,64,第6次排序结果:2 15 17 27 28 33 34 5 64 2,15,17,27,28,33,5,34,64,2,15,17,27,28,5,33,34,64,2,15,17,27,5,28,33,34,64,2,15,17,5,27,28,33,34,64,2,15,5,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,第7次排序结果:2 5 15 17 27 28 33 34 64 2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,2,5,15,17,27,28,33,34,64,第8次排序结果:2 5 15 17 27 28 33 34 64 最后排序结果:2 5 15 17 27 28 33 34 64

推荐信息