码迷,mamicode.com
首页 > 编程语言 > 详细

数组算法:

时间:2017-01-07 01:33:26      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:下标   冒泡排序   决定   lin   array   ann   next   最大   复制   

数组算法1),复制算法:  例如:(int[] a={1,2,3,4,5,};

                                 int[] b=new int[a.length];//b的长度由a的长度来决定;

                              for(int i=0;i<a.length;i++){//写a.length或者写b.length都可以。长度一样;

                                  a[i]=b[i];    //复制一份给b;  

                              }

                              for(int i=0;i<a.length;i++){ 输出b:(System.out.println(b[i]+" "));  

                               }//输出b[i]的值:

         2)、查找算法(搜索);例如:Scnner in=new Scanner(System.in);

                                     int[] array={1,2,3,4,5,};

                                     System.out.println("请输入您要插入的数:");

                                     int index=in.nextInt();

                                     arrray[a.length-1]=index;//a.length-1:是array数组的最大下标;

                                     for(int i=a.length-1;;i>0;i--){

                                       if(array[i]<array[i-1]){

                                        int t=array[i];//int t :借助中间变量;

                                        array[i]=array[i-1];

                                        array[i-1]=t;    

                                        }else{  

                                             break;

                                         }    

                                     }

                                     for(int i=0;i<array.length;i++){

                                        System.out.println(array[i]+" ");//输出 array[i];

                                     }

         3)、冒泡排序:      例如:

                                       for(int j=0;j<a.length-1;j++){

                                         if(a[j]>a[j+1]){

                                          int t=a[j];

                                          a[i]=a[j+1];

                                          a[j+1]=t;

                                          }

                                        

                                        }

       

数组算法:

标签:下标   冒泡排序   决定   lin   array   ann   next   最大   复制   

原文地址:http://www.cnblogs.com/tian520131/p/6257946.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!