排序算法代码总结-java版

冒泡排序:

public void BubbleSort(int[] temp)
{
    for ( int i = 0; i < temp.length-1; i++)
    {
        for (int j = 0; j < temp.length-i-1; j++)
        {
            if(temp[j] > temp[j+1])
            {
                int tem = temp[j];
                temp[j] = temp[j+1];
                temp[j+1] = tem;
            }
        }
    }
    for (int a=0;a<temp.length; a++)
        System.out.println(temp[a]+"\t");
}

选择排序:

public void SelectSort(int[] a)
{
    if(a == null || a.length <=0)
        return;
    for( int i = 0; i < a.length; i ++)
    {
        int min = i;
        for (int j=i+1; j<a.length; j++)
        {
            if(a[min] > a[j])
            {
                min = j;
            }
        }
        if (i != min )
        {
            int tmp = a[i];
            a[i] = a[min];
            a[min] = tmp;
        }
    }
}

 

插入排序:

public void InsertSort(int k[])
{
    for (int i = 1; i < k.length; i++)
    {
        if(k[i] <k[i-1])
        {
            int tmp = k[i];
            for(j = i-1;k[j] > tmp; j--)
            {
                k[j+1] = k[j];
            }
            k[j+1] = tmp;
        }
    }
}

 

希尔排序:

public void shellSort(int[] arr)
{
    int j;
    int len = arr.length;
    for( int val = len>>1; val >0; val>>=1)
    {
        for(int i = val; i<len; i++)
        {
            int temp = arr[i];
            for( j=i; j>=val; j-=val){
                if(temp > arr[j-val])
                    arr[j] = arr[j-val];
                else
                    break;
            }
        }
        arr[j] = temp;
    }
}

 

归并排序:

public void mergeSort(int[] nums,int low,int high)
{
    int mid = (low+high) / 2;
    if(low < high )
    {
        mergeSort(nums,low,mid);
        mergeSort(nums,mid +1, high);
        merge(nums,low,mid,high);
    }
}
public void merge(int[] nums, int low, int mid, int high)
{
        int[] temp = new int[high - low + 1];  
        int i = low;// 左指针  
        int j = mid + 1;// 右指针  
        int k = 0;   
        // 把较小的数先移到新数组中  
        while (i <= mid && j <= high) {  
            if (nums[i] < nums[j]) {  
                temp[k++] = nums[i++];  
            } else {  
                temp[k++] = nums[j++];  
            }  
        }  
        // 把左边剩余的数移入数组  
        while (i <= mid) {  
            temp[k++] = nums[i++];  
        }   
        // 把右边边剩余的数移入数组  
        while (j <= high) {  
            temp[k++] = nums[j++];  
        }  
        // 把新数组中的数覆盖nums数组  
        for (int k2 = 0; k2 < temp.length; k2++) {  
            nums[k2 + low] = temp[k2];  
        }  
}

 

快速排序:

public int Partition(int arr[],int low,int high)
{
    int point = arr[(low+high)/2];
    while(low <high)
    {
        while(low<high && k[high] >=point)
            high--;
        Swap(k,low,high);
        while(low<high && k[low] <= point)
            low++;
        Swap(k,low,high);
    }
    return low;
}
public void quickSort(int arr[],int left, int right)
{
    int index;
    if(left < right)
    {
        index = Partition(arr,left,right);
        quickSort(arr,left,index-1);
        quickSort(arr,index+1,right);
        
    }
}

 

堆排序:

public void heapSort(int[] data)
{
    for(int i = 0; i < data.length; i++)
    {
        createMaxdHeap(data,data.length-1-i);
        Swap(data,0,data.length-1-i);
    }
}

public void createMaxdHeap(int[] data,int lastIndex)
{
    for(int i=(lastIndex -1) / 2; i >=0; i--)
    {
        int k = i;
        while (2*k +1 <= lastIndex)
        {
            int bigger = 2* k +1;
            if(bigger < lastIndex)
            {
                if(data[bigger] < data[bigger +1])
                    bigger++;
            }
            if(data[k] < data[bigger])
            {
                Swap(data,k,bigger);
                k =bigger;
            }
            else
                break;
        }
    }
}

 

最后附上各排序算法的性能分析图

 

 

posted @ 2016-03-01 20:50  Trey  阅读(186)  评论(0编辑  收藏  举报