zoukankan      html  css  js  c++  java
  • 选择排序

     1     (1)排序
     2         A:冒泡排序
     3             相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处。同理,其他的元素就可以排好。
     4             
     5             public static void bubbleSort(int[] arr) {
     6                 for(int x=0; x<arr.length-1; x++) {
     7                     for(int y=0; y<arr.length-1-x; y++) {
     8                         if(arr[y] > arr[y+1]) {
     9                             int temp = arr[y];
    10                             arr[y] = arr[y+1];
    11                             arr[y+1] = temp;
    12                         }
    13                     }
    14                 }
    15             }
    16             
    17         B:选择排序
    18             把0索引的元素,和索引1以后的元素都进行比较,第一次完毕,最小值出现在了0索引。同理,其他的元素就可以排好。
    19             
    20             public static void selectSort(int[] arr) {
    21                 for(int x=0; x<arr.length-1; x++) {
    22                     for(int y=x+1; y<arr.length; y++) {
    23                         if(arr[y] < arr[x]) {
    24                             int temp = arr[x];
    25                             arr[x] = arr[y];
    26                             arr[y] = temp;
    27                         }
    28                     }
    29                 }
    30             }

  • 相关阅读:
    Go Map
    Go XORM
    Go切片
    Go函数
    dockerfile常用指令
    Goroutine并发控制
    Go 格式转换
    Go 常用知识点及实例
    Go 时间
    Go error
  • 原文地址:https://www.cnblogs.com/fuck1/p/5373588.html
Copyright © 2011-2022 走看看