冒泡排序

来源:互联网 发布:ait8328 编程 编辑:程序博客网 时间:2024/06/14 20:27

2.比较相邻的元素。不符合条件,就交换他们两个。

冒泡排序最坏情况的时间复杂度是O(n²)
冒泡排序属于交换排序,稳定,空间复杂度为O(1)

public class maopao {        public int[] maopao(int shuzu[],int n) {//n为数组长度;            for (int j = 0; j< n-1;j++) {                for (int i = 0; i < n-j-1; i++) {                    int max;                    if(shuzu[i]>shuzu[i+1]){//两两相比,若i项数值大于i+1项,则交换他们的位置;                        max=shuzu[i];//将数值大的i项赋值给max;                        shuzu[i]=shuzu[i+1];//将i+1项赋值给i项;                        shuzu[i+1]=max;//将max赋值给i+1项;                    }                }            }               return shuzu;        }}
原创粉丝点击