9.5 排序:   有一种排序的方法,非常好理解,详见本题的步骤,先找出最大值和最小值,把最小值打印出来后,把它存在另一个数组b当中,再删除此最小值,之后再来一次找出最小值,打印出最小值以后,再把它存在另一个数组b当中,再删除此最小值,这样循环往复,直到做完,你就会发觉,你已经把排了序数放在b数组当中了,而这里的彻底删除最小值的方法就是用比最大值还大一的数来取代最小值。(自己想想为什么?)参考后面的答案你会发觉,按照下面的四步,你已经把一个数组排序了。 i)make a method called getMin to find the minimal value of the array. ii)make a method called getMax to find the maximum value of the array. iii) replace the minimal value with the maximum+1. iiii) sort an array.

public class Test {
    static int minPosition=0;//用这个全局变量来记录最小数的位置索引,
    public static void main(String[] args) {
        int[] a = {6, 12, 7, 23, 4};
        int max = getMax(a);
        int[] b = new int[a.length];
        for (int j = 0; j < a.length; j++) {
            int min = getMin(a); 
 /*把a数组当中最小的值,马克-to-win给替换成max+1,这样就相当于把原来的最小值从这个数组当中彻底清除掉了。整个循环做完,a数组就彻底废了。*/
            a[minPosition] = max + 1; 
            minPosition=0;//把minPosition重置一下, 因为最小的位置已经不是这了。
            b[j] = min;//用此方法a数组当中的数就一个一个从小到大捣到b数组当中了
        }
        for (int j = 0; j < a.length; j++) System.out.println(b[j]);
    }
    static int getMax(int[] a) { 
       int max=a[0];
       for (int i = 1; i < a.length ; i++) {
           if(max<a[i])
           {
               max=a[i];
           }
       }        
       return max;
    }
    static int getMin(int[] a) { 
        int min=a[0];
        for (int i = 1; i < a.length ; i++) {
            if(min>a[i])
            {
                min=a[i];
                minPosition=i;//用这个全局变量来记录最小数的位置索引,
            }
        }        
        return min;
    }

}

更多请见:https://blog.csdn.net/qq_44639795/article/details/103142934

最新文章

  1. download ncRNA sequences form NCBI
  2. The property on could not be set to a &#39;Int16&#39; value.You must set this property to a non-null value of type ‘Int32’.”
  3. PC远程调试移动设备
  4. string、math、random、datetime类
  5. nodejs--偏函数
  6. X86 I/O端口
  7. linker command failed with exit code 1 (use -v to see invocation),经典Xcode编译错误的出现和解决!
  8. PHP之验证码代码
  9. 【第七篇】Volley之处理Gzip数据
  10. Oracle 获取当天数据
  11. 使用HTML5地理位置定位到城市的方法及注意事项
  12. Java I/O 从0到1 - 第Ⅰ滴血 File
  13. Django(一)
  14. TF-卷积函数 tf.nn.conv2d 介绍
  15. Spring_Spring与DAO_Spring的Jdbc模板
  16. ios &#160;国际化开发
  17. vue2 兼容ie8
  18. jq 点击按钮显示div,点击页面其他任何地方隐藏div
  19. JS 遍历JSON中每个key值
  20. Spring Boot 揭秘与实战(五) 服务器篇 - 其他内嵌服务器 发表于 2017-01-03 | Spring框架 | Spri

热门文章

  1. SpringBoot入门二:与Mybatis整合
  2. SQL从零到迅速精通【数据更新】
  3. 表格的td行利用css显示...
  4. tensorflow源码解析系列文章索引
  5. LINUX安装 RPM与YUM
  6. Solon 1.6.33 发布,更现代感的应用开发框架
  7. .NET Core剪裁器Zack.DotNetTrimmer升级瘦身引擎,并支持剪裁计划的录制和回放
  8. pip安装使用国内源的两种方法
  9. 使用 Spring Cloud 有什么优势?
  10. ZooKeeper 面试题?