zoukankan      html  css  js  c++  java
  • 冒泡排序法

    采用两两比较并交换位置的思路,就仿佛泡泡一样,较大的元素会慢慢“上浮”1从数列一端开始,比较相邻两个元素,如果第一个元素比第二个元素大,交换两个元素位置然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,2移动至下一对相邻元素,再次进行比较和交换,直到数列最后一个元素3保证倒数第一的元素是数列中最大的)4不断重复以上操作。每次重复的时候,需要比较的元素个数都减1
    public class MainActivity extends AppCompatActivity {
        int[] arr = new int[]{9, 5, 4, 8, 7, 3, 1};
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            int[] maopao = maopao(arr);
            for (int a : maopao) {
                System.out.println(a);
            }
    
        }
    
        public int[] maopao(int[] array) {
            for (int f=0; f < array.length-1; f++) {//遍历所有的数
                for (int i=0; i < array.length - f-1; i++){//找最大的放后面
                    if (array[i] > array[i + 1]) {
                        int temp = array[i + 1];
                        array[i + 1] = array[i];
                        array[i] = temp;
                    }
                }
            }
            return array;
        }
    }
  • 相关阅读:
    UVA11825 Hackers' Crackdown
    UVA 11346 Probability
    Codeforces 12 D Ball
    bzoj 4766: 文艺计算姬
    Codeforces 757 F Team Rocket Rises Again
    [HAOI2011] problem C
    Atcoder 3857 Median Sum
    bzoj4399 魔法少女LJJ
    bzoj2638 黑白染色
    bzoj4197 [Noi2015]寿司晚宴
  • 原文地址:https://www.cnblogs.com/Ocean123123/p/11028798.html
Copyright © 2011-2022 走看看