A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

最近学习了几个基本的算法,特贴在这里一起分享哈

  1. public class Sort {

  2.         public static void main(String[] args) {
  3.                 //调用自定制方法生成int[]
  4.                 int[] src=getIntegerArray(20);
  5.                 for(int i:src){
  6.                         System.out.print(i+"        ");
  7.                 }
  8.                 System.out.println();
  9.                
  10.                 long startTime=System.nanoTime();
  11.                 bubbleSort(src);
  12.                 long endTime=System.nanoTime();
  13.                 System.out.println("bubbleSort used time:"+(endTime-startTime));
  14.                
  15.                 long startTime1=System.nanoTime();
  16.                 selectionSort(src);
  17.                 long endTime1=System.nanoTime();
  18.                 System.out.println("selectionsort used time:"+(endTime1-startTime1));
  19.                
  20.                
  21.                 long startTime2=System.nanoTime();
  22.                 insertionSort(src);
  23.                 long endTime2=System.nanoTime();
  24.                 System.out.println("insertionSort used time:"+(endTime2-startTime2));
  25.                 //打印排序后数组
  26.                 for(int i:src){
  27.                         System.out.print(i+"        ");
  28.                 }
  29.                
  30.         }
  31.        
  32.         public static int[] bubbleSort(int[] src){
  33.                 if(src==null||src.length==0)
  34.                         return null;
  35.                 int out,in;
  36.                 for(out=src.length-1;out>0;out--){
  37.                         for(in=0;in<out;in++){
  38.                                 if(src[in]>src[in+1]){
  39.                                         int temp=src[in];
  40.                                         src[in]=src[in+1];
  41.                                         src[in+1]=temp;
  42.                                 }
  43.                         }
  44.                 }
  45.                 return src;
  46.         }
  47.        
  48.         public static int[] selectionSort(int[] src){
  49.                 if(src==null||src.length==0)
  50.                         return null;
  51.                 int out,in,min;
  52.                 for(out=0;out<src.length-1;out++){
  53.                         min=out;
  54.                         for(in=out+1;in<src.length;in++){
  55.                                 if(src[in]<src[min]){
  56.                                         min=in;
  57.                                 }
  58.                                 int temp=src[out];
  59.                                 src[out]=src[min];
  60.                                 src[min]=temp;
  61.                         }
  62.                 }
  63.                 return src;
  64.         }
  65.        
  66.         public static int[] insertionSort(int[] src){//最快
  67.                 if(src==null||src.length==0)
  68.                         return null;
  69.                 int out,in;
  70.                 for(out=1;out<src.length;out++){
  71.                         int temp=src[out];
  72.                         in=out;
  73.                         while(in>0&&src[in-1]>temp){
  74.                                 src[in]=src[in-1];
  75.                                 in--;
  76.                         }
  77.                 /*        for (; in > 0; in--) {    // 用时更少
  78.                                 if (src[in - 1] > temp) {
  79.                                         src[in] = src[in - 1];
  80.                                 } else {
  81.                                         break;
  82.                                 }
  83.                         }*/
  84.                         src[in]=temp;
  85.                 }
  86.                 return src;
  87.         }
  88.        
  89.         //生成随机int[]供测试用
  90.         public static int[] getIntegerArray(int n){
  91.                 if(n<=0)
  92.                         return null;
  93.                
  94.                 int[] src=new int[n];
  95.                 int index=0;
  96.                 while(index<n){
  97.                         src[index]=(int)(Math.random()*100);
  98.                         index++;
  99.                 }
  100.                 return src;
  101.         }
  102. }
复制代码


14 个回复

倒序浏览
希望楼主写一下思路和注释
回复 使用道具 举报
本帖最后由 cat73 于 2015-5-3 10:10 编辑

说实话 楼主这代码风格不是很好- -
该留空格的地方不留空格
该有注释的地方没有注释提出一些改进意见
1.永远不要省略if for之类函数里的大括号
2.该留空格的地方要留空格 比如等于号两边
3.不相关的两段代码要用空行隔开 最好能分开两个函数

以及,其实我个人是从来不用IDE的自动代码整理的,时间长了自己就习惯了,自然而然的就写出来符合规范的代码格式了。
现在我写任何语言的代码,几乎都是照那一份格式来,这样的好处就是,我出门平板上只有个Sublime Text(一种记事本),照样可以写各种语言的代码,而不用一个语言一个IDE。
回复 使用道具 举报
  1. public class Sort {
  2.     /**
  3.      * main方法 程序从这里启动
  4.      * @param args 从外部传入的参数列表
  5.      */
  6.     public static void main(String[] args) {
  7.         //生成要排序的数组
  8.         int[] src = getIntegerArray(20);

  9.         //输出下要排序的数组
  10.         for(int i : src){
  11.             System.out.print(i + "\t");
  12.         }
  13.         System.out.println();

  14.         //冒泡排序
  15.         long startTime = System.nanoTime();
  16.         bubbleSort(src);
  17.         long endTime = System.nanoTime();
  18.         System.out.println("bubbleSort used time:" + (endTime - startTime));
  19.         
  20.         //选择排序
  21.         long startTime1 = System.nanoTime();
  22.         selectionSort(src);
  23.         long endTime1 = System.nanoTime();
  24.         System.out.println("selectionsort used time:" + (endTime1 - startTime1));
  25.         
  26.         //插入排序
  27.         long startTime2 = System.nanoTime();
  28.         insertionSort(src);
  29.         long endTime2 = System.nanoTime();
  30.         System.out.println("insertionSort used time:" + (endTime2 - startTime2));
  31.         
  32.         //输出下排序后数组
  33.         for(int i : src){
  34.             System.out.print(i+"\t");
  35.         }
  36.     }

  37.     /**
  38.      * 冒泡排序
  39.      * @param src 要被排序的数字数组
  40.      * @return 排序后的数字数组
  41.      */
  42.     public static int[] bubbleSort(int[] src) {
  43.         if(src == null || src.length == 0) {
  44.             return null;
  45.         }

  46.         int out, in;
  47.         for(out = src.length - 1; out > 0; out--) {
  48.             for(in = 0; in < out; in++) {
  49.                 if(src[in] > src[in + 1]) {
  50.                     int temp = src[in];
  51.                     src[in] = src[in + 1];
  52.                     src[in + 1] = temp;
  53.                 }
  54.             }
  55.         }

  56.         return src;
  57.     }

  58.     /**
  59.      * 选择排序
  60.      * @param src 要被排序的数字数组
  61.      * @return 排序后的数字数组
  62.      */
  63.     public static int[] selectionSort(int[] src) {
  64.         if(src == null || src.length == 0) {
  65.             return null;
  66.         }

  67.         int out, in, min;
  68.         for(out = 0; out < src.length - 1; out++) {
  69.             min = out;
  70.             for(in = out + 1; in < src.length; in++) {
  71.                 if(src[in] < src[min]) {
  72.                     min = in;
  73.                 }
  74.                 int temp = src[out];
  75.                 src[out] = src[min];
  76.                 src[min] = temp;
  77.             }
  78.         }

  79.         return src;
  80.     }

  81.     /**
  82.      * 插入排序
  83.      * @param src 要被排序的数字数组
  84.      * @return 排序后的数字数组
  85.      */
  86.     public static int[] insertionSort(int[] src) { //最快
  87.         if(src == null || src.length == 0) {
  88.             return null;
  89.         }

  90.         int out,in;
  91.         for(out = 1; out < src.length; out++) {
  92.             int temp = src[out];
  93.             in = out;
  94.             while(in > 0 && src[in - 1] > temp) {
  95.                 src[in] = src[in - 1];
  96.                 in--;
  97.             }
  98. /*
  99.             for (; in > 0; in--) {    // 用时更少
  100.                 if (src[in - 1] > temp) {
  101.                     src[in] = src[in - 1];
  102.                 } else {
  103.                     break;
  104.                 }
  105.             }
  106. */
  107.             src[in] = temp;
  108.         }

  109.         return src;
  110.     }
  111.    
  112.     /**
  113.      * 随机生成一些整数并以数组形式返回, 供测试排序函数用
  114.      * @param n 要生成的整数数量
  115.      * @return 一些随机大小的整数的数组
  116.      */
  117.     public static int[] getIntegerArray(int n) {
  118.         if(n <= 0) {
  119.             return null;
  120.         }
  121.         
  122.         int[] src = new int[n];
  123.         int index = 0;
  124.         while(index < n){
  125.             src[index] = (int)(Math.random() * 100);
  126.             index++;
  127.         }
  128.         
  129.         return src;
  130.     }
  131. }
复制代码


经过人工简单整理后的代码
回复 使用道具 举报
cat73同学已然培养了良好的编程习惯
回复 使用道具 举报
注释好少,不过大赞一下啊
回复 使用道具 举报
3kx share  
回复 使用道具 举报
写的很规范了,方便调用。
回复 使用道具 举报
学习了~
回复 使用道具 举报
有心人,nice,赞一个
回复 使用道具 举报
路过一下..不过看懂了
回复 使用道具 举报
加注释!!!!!!!!!!!!!!!!!
回复 使用道具 举报
那个,代码是怎么贴的??
回复 使用道具 举报
非常不错!
回复 使用道具 举报
建议做好注释
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马