插入,冒泡,选择,快速排序,二分查找

一. 直接插入排序
Java代码 复制代码
  1. voidinsertSort(int[]a){
  2. for(inti=1;i<a.length;i++){
  3. if(a[i]<a[i-1]){
  4. temp=a[i];//1
  5. a[i]=a[i-1];//2
  6. //继续和前面的进行比较
  7. for(intj=i-2;j>=0;j--){
  8. if(temp<a[j])
  9. a[j+1]=a[j];//3
  10. }
  11. a[j+1]=temp;//4
  12. }
  13. }
  14. }
void insertSort(int[] a){
      for(int i=1;i<a.length; i++){
           if (a[i]<a[i-1]){
               temp = a[i];  //1 
               a[i] = a[i-1]; //2

               // 继续和前面的进行比较
               for(int j=i-2; j>=0; j--){
                    if(temp < a[j])
                         a[j+1] =a[j];//3
               }
               a[j+1] = temp;//4
           }
      }
}


二.冒泡排序
冒泡排序,就是从最底那个开始往上比较,遇到比它小的就交换,相当于过五关看六将,不断地向前冲。接着循环第二个...
Java代码 复制代码
  1. voidbubbleSort(int[]a){
  2. //每个都进行冒泡(一个一个来)
  3. for(inti=0;i<a.length;i++){
  4. //和后面的每个都进行比较(过五关看六将)
  5. for(intj=i;j<a.length-1;j++){
  6. if(a[j]>a[j+1]){
  7. temp=a[j];
  8. a[j]=a[j+1];
  9. a[j+1]=temp;
  10. }
  11. }
  12. }
  13. }
void bubbleSort(int[] a){ 
            //每个都进行冒泡(一个一个来) 
           for (int i=0; i<a.length; i++){ 
   
                   //和后面的每个都进行比较(过五关看六将) 
                    for (int j=i; j<a.length-1; j++){ 
                            if (a[j]>a[j+1]){ 
                                   temp = a[j]; 
                                    a[j] = a[j+1]; 
                                    a[j+1] = temp; 
                            } 
                    } 
            } 
    } 

三.选择排序
选择排序,就是选择最小的,然后置换,循环再找到最小的,再置换...
Java代码 复制代码
  1. voidselectSort(int[]a){
  2. for(inti=0;i<a.length;i++){
  3. small=i;
  4. //找出最小的
  5. for(intj=i+1;j<a.lenth;j++){
  6. if(a[small]>a[j]){
  7. small=j;
  8. }
  9. }
  10. //置换位置
  11. if(i!=small){
  12. temp=a[small];
  13. a[small]=a[i];
  14. a]i]=temp;
  15. }
  16. }
  17. }
void selectSort(int[] a){ 
        for (int i=0; i<a.length; i++){ 
                small = i; 

                //找出最小的 
                 for (int j=i+1; j<a.lenth; j++){ 
                        if (a[small]>a[j]){ 
                                small = j; 
                        } 
                } 

                //置换位置 
                 if (i != small){ 
                        temp = a[small]; 
                        a[small] = a[i]; 
                        a]i] = temp; 
                } 
        } 
}

四.快速排序
快速排序的基本过程:
得到枢轴索引:compare首先从high位置向前搜索找到第一个小于compare值的索引,并置换(这时high索引位置上的值为compare 值);然后从low位置往后搜索找到第一个大于compare值的索引,并与high索引上的值置换(这时low索引位置上的值为compare值);重复这两步直到low=high为止。
得到枢轴索引后,则递归进行枢轴两边的队列的排序....
Java代码 复制代码
  1. voidquickSort(int[]a,intlow,inthigh){
  2. intp=get(a,low,high);
  3. quickSort(a,low,p-1);
  4. quickSort(a,p+1,high);
  5. }
  6. intget(int[]a,intlow,inthigh){
  7. compare=a[low];
  8. while(low<high){//无论如何置换,被置换的都包含compare的值
  9. while(low<high&&a[high]>=compare)
  10. high--;
  11. //在low<high的情况下找到a[high]<compare并置换
  12. temp=a[low];
  13. a[low]=a[high];
  14. a[high]=temp;
  15. while(low<high&&a[low]<=compare)
  16. low++;
  17. //在low<high的情况下找到a[low]>compare并置换
  18. temp=a[low];
  19. a[low]=a[high];
  20. a[high]=temp;
  21. }
  22. returnlow;//while(low==hight)停止循环,并返回枢轴位置
  23. }
void quickSort(int[] a, int low, int high) { 
        int p = get(a, low, high); 
        quickSort(a, low, p-1); 
        quickSort(a, p+1, high); 
} 

int get(int[] a, int low, int high){ 
     compare = a[low]; 

     while(low < high){ //无论如何置换, 被置换的都包含compare的值
         while(low<high && a[high]>=compare)    
              high--; 

         //在 low<high 的情况下找到a[high]<compare并置换 
         temp = a[low];
         a[low] = a[high];
         a[high] = temp;

         while(low<high && a[low]<=compare) 
              low++; 

         //在 low<high 的情况下找到a[low]>compare并置换 
          temp = a[low]; 
              a[low] = a[high]; 
              a[high] = temp; 
     } 
     return low; //while(low==hight)停止循环, 并返回枢轴位置 
}
非递归的实现
Java代码 复制代码
  1. voidQsort(int[]a){
  2. intstack=newint[a.len];
  3. inttop=-1;
  4. inthigh=0,low=0;
  5. stack[++top]=0;
  6. stack[++top]=array.length-1;
  7. while(top!=-1){
  8. high=stack[top--];
  9. low=stack[top--];
  10. if(low<high){
  11. intpos=get(a,low,high);
  12. stack[++top]=low;
  13. stack[++top]=pos-1;
  14. stack[++top]=pos+1;
  15. stack[++top]=high;
  16. }
  17. }
  18. }
void Qsort(int[] a){
    int stack = new int[a.len];
    int top = -1;
  int high = 0, low = 0;
  stack[++top] = 0;
  stack[++top] = array.length-1;
  while(top != -1){
       high = stack[top--];
       low = stack[top--];
       if(low < high){
             int pos = get(a, low, high); 
             stack[++top] = low;
             stack[++top] = pos-1;
             stack[++top] = pos+1;
             stack[++top] = high;
       }
   }
}

五.二分查找
二分查找原理很容易懂,想象为二叉查找树就明白了。
Java代码 复制代码
  1. intbinarySearch(int[]a,intvalue){
  2. intlow=0;
  3. inthigh=a.length-1;
  4. while(low<=high){
  5. mid=(low+high)/2;//**
  6. if(a[mid]==value)
  7. returnmid;
  8. elseif(a[mid]>value)
  9. high=mid-1;
  10. else
  11. low=mid+1;
  12. }
  13. return-1;
  14. }
int binarySearch(int[] a, int value){ 
        int low = 0; 
        int high = a.length-1; 
         
        while(low <= high){ 
                mid = (low+high)/2; //**
                if (a[mid] == value) 
                        return mid; 
                else if (a[mid] > value) 
                        high = mid-1; 
                else 
                        low = mid +1; 
        } 
        return -1; 
}
** mid = (low + high) / 2; 有问题,当low + high大于int范围时就会溢出的。Sun的jdk里面的二分查找源码原先也有同样的问题。
解决的方法是mid = low/2 + high/2。这样用2先除一下,就不会溢出了。

http://binbin8497.iteye.com/blog/489724
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值