zoukankan      html  css  js  c++  java
  • java基础 第五章 下(选择排序,冒泡排序)

    一、选择排序。

           每次选择容器中的最值。

            例:

                  public class demo2{
                      public static void main( String[] args ){
                           int[] arr = new int[]{6, 3, 8, 2, 14, 16,30,29,23};
                           int arr1[] = sort(arr);
                               for(int i = 0; i < arr1.length; i++){
                                    System.out.print(arr[i] + " ");
                         }
                   }
                   static int[] sort( int[] arr){
                        for(int i = 0; i < arr.length - 1; i++){
                            for(int j = i + 1; j < arr.length; j++){
                                if(arr[i] < arr[j]){
                                swag(arr,i,j);

                            }
                        }
                   }
                                return arr;
                }

                   static void swag(int[] arr, int a , int b){
                        int temp;
                        temp = arr[a];
                        arr[a] = arr[b];
                        arr[b] = temp;

                   }
              }

    二、冒泡排序

            public class demo4{
                public static void main(String[] args){
                    int[] arr = new int[]{5, 6,3 ,2 ,7 ,10 };
                    int arr1[] = sort(arr);
                        for(int a : arr1){
                            System.out.print(a + " ");
                        }


                }
                static int[] sort(int[] arr){
                     int temp;
                         for(int i = 0; i < arr.length - 1; i++){
                             for(int j = 0; j < arr.length-1-i; j++){
                                 if(arr[j] < arr[j + 1]){
                                     temp = arr[j];
                                     arr[j] = arr[j + 1];
                                     arr[j + 1] = temp;

                              }

                         }

                     }
                                        return arr;
                 }


            }

  • 相关阅读:
    inputstream和outputstream读写数据模板代码
    如何显示包的上一层包
    我的cnblogs设置代码
    myeclipse ctrl+shift+F失效
    数据包加密解密
    用VisualSVN Server创建版本库,以及TortoiseSVN的使用
    权限验证MVC
    Asp.net MVC23 使用Areas功能的常见错误
    MVC基础知识
    最全的Resharper快捷键汇总
  • 原文地址:https://www.cnblogs.com/catcoffer/p/8758493.html
Copyright © 2011-2022 走看看