数据结构排序问题---快排---插入---冒泡

快排实现基本思想:取个关键key值对整个序列进行比较,大的放一边,小的放另一边(这就分成两个序列了)。然后继续对两个序列(分开的)进行递归比较,最后实现整个序列的排序。

最坏情况的时间复杂度为O(n2),最好情况时间复杂度为O(nlog2n).

package com;

    //快速排序
public class quick {
    
    //主函数实现排后的输出显示
    public static void main(String[] args){
        int[]a = {21,32,1,332,42,3,12,78,96,23};
        Quick1(a,0,a.length-1);
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+" ");
        }
        
    } 
    //主体。。
    public static int Quick(int[]a,int low,int high){
        
        int key = a[low];    //挖坑,取数
        
        while(low<high){
            while(low<high && key <= a[high]){
                --high;
            }
                a[low] = a[high];  //从后往前,比key小就往前移,
                
            while(low<high && key >= a[low]){
                ++low;
            }
            a[high] = a[low];    //从前到后,比key大就往后移
        }
        a[low] = key;       //填坑
        return low;
        
    }
    //递归调用,实现整体排序
    public static void Quick1(int[]a,int low,int high){
        
        int pivot;
        
        if(low<high){            
            pivot = Quick(a,low,high);            
            Quick1(a,low,pivot-1);            
            Quick1(a,pivot+1,high);            
        }
    }

}

直接插入基本思路:从开始认定第一个已经被排好序了,然后取下一个数据从后往前扫描。直到都排好

 时间复杂度 o(n2) 

public class insertSort {
    
    
    public static void main(String[] args){
        
        int[] a = {1,3,2,21,5,12,98,54};
        insert(a);
        for(int i=0;i<a.length;i++){
        System.out.print(a[i]+" ");        
        }
    }
    
    public static void insert(int[]a){
        
        int temp,j;
        for(int i=1;i<a.length;i++){
            temp = a[i];           //设定一个基准
            j=i-1;
            
/*            for(j=i;j>0 && temp<a[j-1];j--){
                a[j] = a[j-1];                     
            }
            a[j] = temp;*/  
            
            while(j>=0 && temp<a[j]){  //大于基准的往后移
                a[j+1] = a[j];
                j--;
            }
            a[j+1] = temp;
        }
    }
    
}

 冒泡基本思路:从头开始扫描待排序的元素,在扫描过程中依次对相邻元素进行比较,将关键字值大的元素后移。每经过一趟排序后,关键字值最大的元素将移到末尾,此时记下该元素的位置,下一趟排序只需要比较到此位置为止,直到所有元素都已有序排列

 时间复杂度 o(n2) 

/**
 * 
 */
package com;

/**
 * @author wenb
 * @time 下午03:19:01
 * @date 2014-10-23
 */
    //冒泡排序
public class BubbleSort {
    
    public static void main(String[] args){
        
        int[] a = {1,3,2,21,5,12,98,54};
        Bubble(a);
        for(int i=0;i<a.length;i++){
        System.out.print(a[i]+" ");        
        }
    }
    
    public static void Bubble(int[]a){
        
        int temp;            

        for(int i=0;i<a.length;i++){   //控制整体

          for(int j=0;j<a.length-i-1;j++){    //控制比较

            if(a[j]>a[j+1]){   //交换,可以再写个swap()方法调用
                
              temp=a[j];
              a[j]=a[j+1];
              a[j+1]=temp;
              
            }
          }
        }
    }
}
奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗生命在于奋斗,也为自己带盐奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗
原文地址:https://www.cnblogs.com/hardwork/p/4045153.html