快速排序 C语言 递归调用

递归:要满足两个条件 1.循环部分,2.基准条件。

int partition( int a[], int left, int right )    a []是要从左到右排序的数组,其中left是第一个元素的索引,right是最后一个元素的索引。此函数确定数组中的枢轴,并将所有比pilov少的元素向左移动,而将所有所有元素向右移动。重新定位所有元素后,它将返回枢轴的索引。

其中先比较左边,中间,右边三数的大小关系,在将pivot和right-1两个位置进行交换,从而只需比较left-(righth-1)之间的数,

  while (1) { /*将序列中比基准小的移到基准左边,大的移到右边*/
               while ( a[++Low] < Pivot ) ;
               while ( a[--High] > Pivot ) ;
               if ( Low < High ) {
                   temp = a[Low];
                   a[Low] = a[High];
                   a[High] = temp;
                   }//Swap( &A[Low], &A[High] );
               else break;

这部分代码是关键,主要是实现让两数进行交换,当不满足条件时推出循环,最重要的是要实现返回递归可以实现下去的条件,

          //Swap( &A[Low], &A[Right-1] );   /* 将基准换到正确的位置 */
          if(Low < right-1){
               temp = a[Low];
               a[Low] = a[right-1];
               a[right-1] = temp;
               return  Low;
          }
          else return Low-1;

这部分做到使递归执行下去的条件。

#include <stdio.h>

int partition( int a[], int left, int right );

void sort ( int a[], int left, int right )//设置统一接口
{
    if ( left < right ) {
        int p = partition(a, left, right);
        sort(a, left, p);
        sort(a, p+1, right);
    }
}

int main(void)
{
	int n;
	scanf("%d", &n);
	int a[n];
	int i;
	for (  i=0; i<n; i++ ) {
		scanf("%d", &a[i]);
	}

	sort(a, 0, n-1);

	for ( i=0; i<n; i++ ) {
		printf("%d\n", a[i]);
	}
}

int partition( int a[], int left, int right ){
/* 核心递归函数 */
         int Pivot, Low, High,temp;
         int Center = (left+right) / 2;
         if ( a[left] > a[Center] ){
              temp = a[left];
              a[left] = a[Center];
              a[Center] = temp;
             }
            //Swap( &A[Left], &A[Center] );
         if ( a[left] > a[right] ){
              temp = a[left];
              a[left] = a[right];
              a[right] = temp;
             }
            //Swap( &A[Left], &A[Right] );
         if ( a[Center] > a[right] ){
              temp = a[Center];
              a[Center] = a[right];
              a[right] = temp;
             }
            //Swap( &A[Center], &A[Right] );
         /* 此时A[Left] <= A[Center] <= A[Right] */
         //Swap( &A[Center], &A[Right-1] ); /* 将基准Pivot藏到右边*/
         //right = right -1;
         if(Center != right-1){
              temp = a[Center];
              a[Center] = a[right-1];
              a[right-1] = temp;
              Pivot = a[right-1];  /* 选基准 */
         }
         else  Pivot = a[Center];  /* 选基准 */
          //right = right -1;
          Low = left; High = right-1;
          if(Low == High) return Low;
          while (1) { /*将序列中比基准小的移到基准左边,大的移到右边*/
               while ( a[++Low] < Pivot ) ;
               while ( a[--High] > Pivot ) ;
               if ( Low < High ) {
                   temp = a[Low];
                   a[Low] = a[High];
                   a[High] = temp;
                   }//Swap( &A[Low], &A[High] );
               else break;
          }
          //Swap( &A[Low], &A[Right-1] );   /* 将基准换到正确的位置 */
          if(Low < right-1){
               temp = a[Low];
               a[Low] = a[right-1];
               a[right-1] = temp;
               return  Low;
          }
          else return Low-1;
     }




 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值