几种内部排序算法

通常说的排序指:
①.内部排序算法(数据在内存中进行排序)
②.外部排序算法(数据大,需要访问外存)
这里写图片描述
这里写图片描述
我们把排序进行了划分,并计算出排序的时间复杂度,稳定性;下来逐个对排序算法实现,并归纳出排序的实现过程和思想。

插入类排序

插入类排序的思想:在一个已经排好序的有序序列内,对待排序的无序序列区中记录逐个进行处理,每一步将一个待排序的记录与同组那些已经排好序的记录进行比较,然后有序的插到该有序序列区中,直到将所有待排记录全部插入为止。(类似于打扑克牌)

  • 直接插入排序

#include<stdio.h> 
int insertsort(int *L);
int main(void){
    int i;
    int L[10] = {2,99,88,54,23,76,34,13,4,5}; 
    insertsort(L);
    for(i = 0;i < 10;i++)
        printf("%d ",L[i]);
    return 0;
}
int insertsort(int *L){
    int i,j;
    int K;  //用于保存待插入的数据 
    for(i = 1;i < 10;i++){  //  10个数据比较9次就行了 
        K = L[i];   //将待插入数据备份到 K 中 
        for(j = i-1;L[j] > K;j--)   //从后往前比较 
            L[j+1] = L[j];  //把大于 K 的都向后移一个位置 
        L[j+1] = K; //将 K 放到正确位置 
    }
    return *L;
}

可将待排序的数组分为两部分
[已排序区][待排序区]
[2][99,88,54,23,76,34,13,4,5]
然后依次在待排序区取出一个数据,与排序区比较,比待排数据大的,依次往后移一位,否则,这个位置就是要插入数据的位置。

  • 希尔排序
package insertsort;

/**
 * 
 * @author Administrator
 * (Shell's Sort)是插入排序的一种又称“缩小增量排序”(Diminshing Increment Sort)
 * 是直接插入排序算法的一种更高效的改进版本。
 * 希尔排序是非稳定排序算法。时间复杂度为
 * 该方法因D.L.Shell于1959年提出而得名。
 * 基本思想:
 * 先取一个小于n的整数d1作为第一个增量,把文件的全部记录分组。所有距离为d1的倍数的记录放在同一个组中。
 * 先在各组内进行直接插入排序;然后,取第二个增量d2<d1重复上述的分组和排序,直至所取的增量  =1(  <  …<d2<d1),
 * 即所有记录放在同一组中进行直接插入排序为止。
 * 时间复杂度:O(n2)
 */
public class sortShell {

    public static int[] shellSort(int[] array) {

            int d=array.length;  //增量
            while(true){
                d=d/2;          
                for(int x=0;x < d;x++){
                    for(int i=x+d;i<array.length;i=i+d){    //各组内进行直接插入排序
                        int temp=array[i];
                        int j;
                        for(j=i-d;j>=0&&array[j]>temp;j=j-d){
                            array[j+d]=array[j];
                        }
                        array[j+d]=temp;
                    }
                }
                if(d==1){
                    break;
                }
            }
        return array;
    }


}

交换类排序

  • 冒泡排序
package swapsort;

public class sortBubble {
    /**
     * 冒泡排序
     * 比较相邻的元素。如果第一个比第二个大,就交换他们两个。  
     * 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。  
     * 针对所有的元素重复以上的步骤,除了最后一个。
     * 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。 
     * @param numbers 需要排序的整型数组
     * 时间复杂度:O(n2)
     */
    public static int[] bubbleSort(int[] array2) {
        int temp = 0;
        for(int i = 0;i < array2.length;i++){
            for(int j = 0;j < array2.length- i -1;j++){
                if(array2[j] > array2[j+1]){
                    temp = array2[j];
                    array2[j] = array2[j+1];
                    array2[j+1] = temp;
                }
            }
        }
        return array2;

    }
}
  • 快速排序
package swapsort;
/**
 * 尽管快速排序的最坏时间为O(n2)
 * 但就平均性能而言,它是基于关键字比较的内部排序算法中速度最快者,快速排序亦因此而得名。
 * 它的平均时间复杂度为O(nlgn)
 * @author Administrator
 * 时间复杂度:O(nlogn)--O(n2)
 */
public class sortQuit {

    public static int[] quitSort(int[] array) {

        if(array.length > 0){
            quitRecursion(array,0,array.length-1);
        }
        return array;
    }
    //分治思想的体现,递归实现  Recursion:递归
    private static void quitRecursion(int[] array, int low, int high) {
        if(low < high){                     
            int middle = getMiddle(array,low,high);
            quitRecursion(array,low,middle-1);
            quitRecursion(array,middle+1,high);
        }
    }

    private static int getMiddle(int[] array, int low, int high) {
        int middle = array[low];            //选一个数作为中轴
        while(low < high){
            while(low < high && array[high] >= middle){
                high--;
            }
            array[low] = array[high];       //比中轴小的,放到低端
            while(low < high && array[low] <=  middle){
                low++;
            }
            array[high] = array[low];       //比中轴大的,放到高端
        }
        array[low] = middle;                //  中轴记录到位

        return low;                         //返回中轴位置
    }

}

选择类排序

  • 简单选择排序
package selectsort;

/**
 * 
 * @author Administrator
 * 算法思想:遍历整个数组找出最小的数,如果不是第一个数则与第一个数交换,否则不变。
 *        第二趟从剩余的数中,找出最小的数,重复上述操作,依次完成整个数组的排序       
 * 时间复杂度O(n2)   
 */
public class sortSimple {

    public static int[] simpleSort(int[] array) {

        int temp = 0;
        int index = 0;
        int minNumIndex = 0;//存最小的数的下标
        for(int i = 0;i < array.length-1;i++){
            minNumIndex = i;
            temp = 0;
            for(int j = i+1;j < array.length;j++){
                if( array[minNumIndex] > array[j] ){
                    minNumIndex = j;
                }
            }
            temp = array[minNumIndex];
            array[minNumIndex] = array[i];
            array[i] = temp;
        }
        return array;
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值