国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

純C語言:分治快速排序源碼分享

2020-01-26 15:38:41
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

#include<stdio.h>
void fun(int array[],int low,int high)
{
    int i = low;
    int j = high; 
    int temp = array[i];         
    while(i < j)
    {
  while((array[j] >= temp) && (i < j))
  {
   j--;
   array[i] = array[j];
  }
        while((array[i] <= temp) && (i < j))
        {
            i++;
   array[j]= array[i];
  }
    }
    array[i] = temp;
 if(i-1>low)
 {
        fun(array,low,i-1);
 }
 if(high>i+1)
 {
        fun(array,j+1,high);
 }
    else
    {
        return;
    }
}
void main()
{
    int array[10];
 printf("輸入十個(gè)數(shù)字進(jìn)行快速排序:/n");
 for(int i=0;i<10;i++)
 {
  printf("請(qǐng)輸入第%d個(gè)數(shù):",i+1);
  scanf("%d",&array[i]);
 }
 fun(array,0,9);
 printf("對(duì)這十個(gè)數(shù)字從小到大快速排序得:");
    for(i=0;i<10;i++)
    {
        printf("%d ",array[i]);
    }
    printf("/n");
}

 

 

2
#include<iostream.h>
#include<malloc.h>
void interchange(int* m,int* n)
{
 int temp=*m;
 *m=*n;
 *n=temp;

}
int partition(int array[],int p,int q)
{
 int i,j;
 i=p;
 j=q+1;
 while(1)
 {
  do i++;
  while((array[i]<array[p])&&(i!=q));
  do j--;
        while((array[j]>array[p])&&(j!=p));
  if(i<j)
   interchange(&array[i],&array[j]);
  else
   break;
 }
 interchange(&array[p],&array[j]);
 return j;

}
void quicksort(int array[],int p,int q)
{
 int j;
 if (p<q)
 {
  j=partition(array,p,q);
  quicksort(array,p,j-1);
  quicksort(array,j+1,q);
 }
}
void main()
{
 int n,i;
 cout<<"please input the number of array:";
 cin>>n;
 int* a=(int*)malloc(n*sizeof(int));

 for(i=0;i<n;i++)
 {
  cout<<"please the "<<i+1<<"th element :";
  cin>>a[i];
 }
 cout<<"before sort:";
    for(i=0;i<n;i++)
  cout<<a[i]<<"  ";
 cout<<endl;
 quicksort(a,0,n-1);
    cout<<"after sort:";
    for(i=0;i<n;i++)
  cout<<a[i]<<"  ";
 cout<<endl;

}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 仁怀市| 东台市| 陇川县| 永兴县| 噶尔县| 唐河县| 定南县| 灌阳县| 噶尔县| 博野县| 大石桥市| 凤庆县| 高安市| 余江县| 广德县| 延吉市| 绍兴县| 富裕县| 新化县| 安徽省| 江陵县| 河津市| 乌拉特中旗| 孟州市| 扎赉特旗| 弥勒县| 昌黎县| 南汇区| 襄城县| 洞头县| 利津县| 廊坊市| 图片| 桑日县| 布拖县| 环江| 渝北区| 磐石市| 电白县| 广德县| 宜丰县|