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

     1 package cn.stringbuffer.com;
     2 
     3 public class SelectDemo {
     4 
     5     public static void main(String[] args) {
     6         
     7         // TODO Auto-generated method stub
     8         int[] arr={24,56,12,10,32}; 
     9         System.out.println(arrayStringBuffer(arr));
    10         
    11         getselect(arr);//放到方法区里面排序
    12         
    13         System.out.println(arrayStringBuffer(arr));//因为getselect是静态方法所以可以直接从方法区里面拿到数组进行打印
    14 
    15     }
    16     public static void getselect(int[] arr){
    17          for(int x=0;x<arr.length-1;x++){//外循环的次数等于数组字符-1;
    18              for(int y=x+1;y<arr.length;y++){//x+1因为前面排序过后第一个是最小的数字所以x+1是从第二个开始
    19                  if(arr[y]<arr[x]){//如果第二个值小于第一个值
    20                      int temp=arr[x];//把值传给temp保存
    21                      arr[x]=arr[y];//把第二个小的值传给arr[x],方到第一位
    22                      arr[y]=temp;//把大的值传给arr[y]放到第二位
    23                  }
    24              }
    25              
    26          }
    27          
    28         
    29     }
    30     
    31     
    32     //遍历的函数方法
    33 public static String arrayStringBuffer(int[] array){
    34 StringBuffer sb=new StringBuffer();
    35 
    36  for(int x=0;x<array.length;x++){
    37      if(x==array.length-1){//索引值到4的时候,相当于结束了循环
    38         sb.append(array[x]);
    39      }else{
    40          sb.append(array[x]);//继续循环
    41          
    42          sb.append(".");//每个值后面添加.号
    43      }
    44  }
    45     
    46      return sb.toString();//以字符串返回一个值
    47      
    48 }     
    58 }
  • 相关阅读:
    297. Serialize and Deserialize Binary Tree
    331. Verify Preorder Serialization of a Binary Tree
    332. Reconstruct Itinerary
    329. Longest Increasing Path in a Matrix
    319. Bulb Switcher
    292. Nim Game
    299. Bulls and Cows
    Ice Cream Tower Gym
    B
    C
  • 原文地址:https://www.cnblogs.com/yschung/p/9273561.html
Copyright © 2011-2022 走看看