“快速排序算法”问题的分而治之算法

news/2024/7/7 20:16:31
/*
    标题:< <系统设计师> >应试编程实例-[分治法程序设计]
    作者:成晓旭
    时间:2002年09月18日(21:43:00-22:03:00)
          实现“快速排序算法”问题的分而治之算法函数
*/

#include    
" stdio.h "
#include    
" stdlib.h "

// :============================“快速排序算法”问题的分而治之算法===========================
/*
    时间:2002年09月18日(21:43:00-22:03:00)
          实现“快速排序算法”问题的分而治之算法函数
    问题描述:
            用分治法的思想实现快速排序算法。
    编程思想:
            快速排序算法的基本思想本身就是分治法。通过分割,将无序序列分成两部分,
        其中前一部分的元素值都小于后一部分的元素值。然后每一部分再各自递归进行上
        述过程,直到每一部分的长度为1为止。
            首先,在序列的第一个,中间一个,最后一个元素中选取中项,设为p[middle],
        并作temp = p[middle](保存中项);
            其次,将序列中的第一个元素移到p[middle]的位置上;
            然后,设两个指针i,j分别指向将排序序列的第一个元素和最后一个元素;
            重复以上两步,直到i = j为止;
            最后,将array[i] = temp(将tmep移到array[i])。
*/

#define     MAXN    20

void     Carve_up( int  array[], int  number, int   * m)
{
    
int i,j,k,middle,temp;
    i 
= 0;
    j 
= number - 1;
    k 
= (i + j) / 2;
    
//在下标i,j,k的数组元素中选取中项
    if(array[i] >= array[j] && array[j] >= array[k])
        middle 
= j;        //Array[j]是中项
    else if(array[i] >= array[k] && array[k] >= array[j])
        middle 
= k;        //Array[k]是中项
    else
        middle 
= i;        //Array[i]是中项
    temp = array[middle];
    array[middle] 
= array[i];
    
while(i != j)
    
{
        
while(i < j && array[j] >= temp)
            j 
--;    //j逐步减小,直到发现一个array[j] < temp为止
        if(i < j)
        
{
            array[i] 
= array[j];
            i 
++ ;
            
while(i < j && array[i] <= temp)
                i 
++ ;    //i逐步减小,直到发现一个array[i] > temp为止
            if(i < j)
            
{
                array[j] 
= array[i];
                j 
--;
            }

        }

    }

    array[i] 
= temp;
    
*= i;
    
return;
}

void     Quick_Sort( int  array[], int  number)
{
    
int i;
    
if(number > 1)
    
{
        Carve_up(array,number,
&i);
        Quick_Sort(array,i);
        Quick_Sort(
&array[i + 1],number - i - 1);
    }

    
return;
}

void     Run_Quick_Sort()
{
    
int i,array[MAXN] = {1,9,3,7,18,2,20,4,16,5,15,6,14,7,13,6,12,8,11,10};
    Quick_Sort(array,MAXN);
    
for(i = 0;i < MAXN;i++)
        printf(
"%3d",array[i]);
}

// :============================“快速排序算法”问题的分而治之算法===========================

int  main( int  argc,  char *  argv[])
{
    Run_Quick_Sort();

    printf(
" 应用程序运行结束! ");
    
return 0;
}






http://www.niftyadmin.cn/n/3649088.html

相关文章

如何在android桌面添加几个app图标做快捷方式

如最下方图所示&#xff0c;在桌面添加6个app应用的快捷方式&#xff0c; 在路径packages\apps\Launcher2\res\xml\default_workspace.xml文件里 1. 在桌面第二屏添加所用app的包名和类名。 <favorites xmlns:launcher"http://schemas.android.com/apk/res/com.android…

node vue 实时推送_如何使用Node,Vue和ElasticSearch构建实时搜索引擎

node vue 实时推送介绍 (Introduction) Elasticsearch is a distributed, RESTful search and analytics engine capable of solving a growing number of use cases. Elasticsearch is built on top of Apache Lucene, which is a high-performance text search engine librar…

“装箱”问题的贪婪法解决算法

/**//* 标题&#xff1a;<<系统设计师>>应试编程实例-[递推算法程序设计]作者&#xff1a;成晓旭时间&#xff1a;2002年09月14日(18:20:00-20:18:00)实现“装箱”问题的贪婪算法实现函数时间&#xff1a;2002年09月14日(22:00:00-23:18:00)实现“装箱”问题的贪…

Android快捷方式解密

Android快捷方式解密 Android快捷方式作为Android设备的杀手锏技能&#xff0c;一直都是非常重要的一个功能&#xff0c;也正是如此&#xff0c;各种流氓App也不断通过快捷方式霸占着这样一个用户入口。 同时&#xff0c;各大国产ROM和Luncher的崛起&#xff0c;让这个桌面之争…

如何使用React Router v4设置条件和响应式路由

介绍 (Introduction) Responsive routing in React involves serving different routes to users based on the viewport of their device. CSS media queries are usually used to achieve this, but this means you’re restricted to either showing or not showing differe…

[收藏]影响员工执行力的16种因素及主管对策

问题或现象Q01、员工不知道该做什么Q02、员工不知道为什么该做这件事Q03、员工不知道该如何做Q04、员工认为你的方法不行Q05、员工认为自己的方法更好Q06、员工认为其他事情更重要Q07、员工认为他们正在按照你的要求做事Q08、积极工作的员工没有得到正面的结果Q09、无功的员工却…

专门提供为处理一些UI相关的问题而创建的工具类

/*** 专门提供为处理一些UI相关的问题而创建的工具类&#xff0c;* 提供资源获取的通用方法&#xff0c;避免每次都写重复的代码获取结果。*/ public class UIUtils {public static Context getContext() {return MyApplication.context; }public static Handler getHandler() …

如何在Ubuntu 16.04上使用ProxySQL缓存优化MySQL查询

The author selected the Free Software Foundation to receive a donation as part of the Write for DOnations program. 作者选择了自由软件基金会作为Write for DOnations计划的一部分接受捐赠。 介绍 (Introduction) ProxySQL is a SQL-aware proxy server that can be p…