• 几种常见的排序方法(C语言实现)


    #include <stdio.h>
    #include <stdlib.h>
    #include <Windows.h>
    
    //直接插入排序
    void InsertSort(int arry[], int n)
    {
        int i, j;
        int temp;//临时变量
        for (i = 1; i < n; i++)
        {
            temp = arry[i];
            for (j = i - 1; j >= 0; j--)
            {
                if (temp > arry[j])
                    break;
                else
                    arry[j + 1] = arry[j];
            }
            arry[j+1] = temp;
        }
    }
    
    //直接选择排序
    void SelectSort(int arry[], int n)
    {
        int i, j;
        int temp;
        for (i = 0; i < n-1; i++)
        {
            temp = i;
            for (j = i + 1; j < n; j++)
            {
                if (arry[j] < arry[temp])
                    temp = j;
            }
            if(temp!=i)
                arry[temp] ^= arry[i] ^= arry[temp] ^= arry[i];//实现arry[temp]和arry[i]值交换
        }
    }
    
    //冒泡排序
    void BubbleSort(int arry[], int n)
    {
        int i, j, k;
        for (i = 0; i < n-1; i++)
        {
            for (j = i; j < n; j++)
            {
                if (arry[j - 1]>arry[j])
                    arry[j - 1] ^= arry[j] ^= arry[j - 1] ^= arry[j];//实现arry[temp]和arry[i]值交换
            }
        }
    }
    
    //希尔排序第一类,
    void ShellSort1(int arry[], int n)
    {
        int i, j, k, gap,temp;
        for (gap = n / 2; gap > 0; gap /= 2)
        {
            for (k = 0; k < gap; k++)//同组先排
            {
                for (i = k+gap; i < n; i += gap)
                {
                    temp = arry[i];
                    for (j = i - gap; j >= 0; j -= gap)
                    {
                        if (temp > arry[j])
                            break;
                        else
                            arry[j + gap] = arry[j];
                    }
                    arry[j + gap] = temp;
                }
            }
        }
    }
    
    //希尔排序第二类
    void ShellSort2(int arry[], int n)
    {
        int i, j, k, gap, temp;
        for (gap = n / 2; gap > 0; gap /= 2)
        {
            for (i = gap; i < n; i++)//所有组一起排序
                {
                    temp = arry[i];
                    for (j = i - gap; j >= 0; j -= gap)
                    {
                        if (temp > arry[j])
                            break;
                        else
                            arry[j + gap] = arry[j];
                    }
                    arry[j + gap] = temp;
                }        
        }
    }
    
    //希尔排序,利用gap分组后采用冒泡方式排序
    void ShellSort3(int arry[], int n)
    {
        int i, j, k, gap, temp;
        for (gap = n / 2; gap > 0; gap /= 2)
        {
            for (k = 0; k < gap; k++)
            {
                for (i = k; i < n-gap; i += gap)
                {
                    for (j = k+gap; j <n ; j += gap)
                    {
                        if (arry[j - gap]>arry[j])
                            arry[j - gap] ^= arry[j] ^= arry[j - gap] ^= arry[j];
                    }
                    
                }
            }
        }
    }
    
    //快速排序,递归调用
    void QuickSort(int arry[], int s,int t)
    {
        int i, j;
    
        if (s < t)
        {
            i = s;
            j = t+1;
            while (1)
            {
                do
                {
                    i++;
                }while (!(arry[s] <= arry[i] || i >= t));
                do
                {
                    j--;
                } while (!(arry[s] >= arry[j] || j <= s));
                
                if (i < j)
                    arry[i] ^= arry[j] ^= arry[i] ^= arry[j];
                    
                else
                    break;
            }
            if (arry[j]!= arry[s])
                arry[j] ^= arry[s] ^= arry[j] ^= arry[s];
            QuickSort(arry, s, j-1);
            QuickSort(arry, j + 1, t);
        }
    }
    
    int main()
    {
        int arry1[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41};
        int arry2[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };
        int arry3[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };
        int arry4[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };
        int arry5[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };
        int arry6[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };
        int arry7[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };
        
        //七种方法任选一种,但效率有别
        //一般情况下:快速排序>直接插入>直接选择==冒泡
        //希尔排序看情况而定。一般优于直接选择排序
    
        //InsertSort(arry1, 15);
        
        //SelectSort(arry2, 15);
        
        //BubbleSort(arry3, 15);
        
        //ShellSort1(arry4, 15);
        //ShellSort2(arry5, 15);    
        //ShellSort2(arry6, 15);
        
        //QuickSort(arry7,0,14);
        
    
    
        int i = 0;
        for (i = 0; i < 15; i++)
        {
            printf("%d  ", arry7[i]);
        }
    
        system("pause");
    }
  • 相关阅读:
    服务器数据库不用开通远程连接通过工具在本地连接操作的方法
    怎么搜索同类网站
    Java三行代码搞定MD5加密,测试5c短信网关的demo
    iOS检测用户截屏并获取所截图片
    tomcat输出servlet-api.jar
    从svn资源库目录checkout出maven项目方法
    Maven打包pom里面配置exclude 排除掉环境相关的配置文件
    PHP获取毫秒时间戳,利用microtime()函数
    阿里云OneinStack,Linux下tomcat命令
    阿里云OneinStack数据库相关
  • 原文地址:https://www.cnblogs.com/cjw1115/p/4614388.html
Copyright © 2020-2023  润新知