设计模式

2019-08-13  本文已影响0人  小鱼儿_f32a

一、冒泡排序


publicclass BubbleSort {

    public static void BubbleSort(int[] arr) {

        int temp;//定义一个临时变量

        for(int i=0;i

            for(intj=0;j

                if(arr[j+1]

                    temp = arr[j];

                    arr[j] = arr[j+1];

                    arr[j+1] = temp;

                }

            }

        }

    }

    public static void main(String[] args) {

        int arr[] = new int[]{1,6,2,2,5};

        BubbleSort.BubbleSort(arr);

       System.out.println(Arrays.toString(arr));

    }

}

二、插入排序

https://blog.csdn.net/qq_28081081/article/details/80594386

插入排序(英语:Insertion Sort)是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间。

上一篇 下一篇

猜你喜欢

热点阅读