注册 登录
编程论坛 数据结构与算法

快速排序的求高手帮忙改成数组的,或者可以其他的排序是O(n㏒2n),

h2363752280 发布于 2012-12-24 11:50, 490 次点击
顺序表的排序,要求该算法的时间复杂度为O(n㏒2n),然后调用函数输出处理之后的顺序表.
/* QuickSort related function */快速排序的求高手帮忙改成数组的,或者可以其他的排序是O(n㏒2n),急急急
int Partition(SqList *L,int low,int high)
{
  int pivotkey;
  L->r[0]=L->r[low];
  pivotkey=L->r[low].key;
  while(low<high){
    while(low<high&&L->r[high].key>=pivotkey) --high;
    L->r[low]=L->r[high];
    while(low<high&&L->r[low].key<=pivotkey) ++low;
    L->r[high]=L->r[low];
  }
  L->r[low]=L->r[0];
  return low;
}
void QSort(SqList *L,int low,int high)
{
  int pivotloc;
  if(low<high){
    pivotloc=Partition(L,low,high);
    QSort(L,low,pivotloc-1);
    QSort(L,pivotloc+1,high);
  }
}
void QuickSort(SqList *L)
{
  QSort(L,1,L->length);
}        
2 回复
#2
qunxingw2012-12-24 12:31
坛子里有
#3
h23637522802012-12-24 13:24
#include <stdio.h>
#define MAX 99900
#include <time.h>

void creat1(int *p,int n)  //随机生成
{ int temp,i;   //定义两个整型变量
  srand((unsigned)time(NULL));
   for(i=0;i<n;i++)          //用随机函数生成一个顺序表
     {    temp=(int)rand()%MAX+100;  p[i]=temp;}
     
}
void creat2(int *p,int n)//输出
{    int i;
    for(i=0;i<n;i++)
   printf("%d\t",p[i]);

}
int partion(int low, int high)
{int a[MAX];
    a[0] = a[low];
    while( low < high )
    {
        while( low<high && a[0]<=a[high] )
            --high;
        a[low] = a[high];
        while( low<high && a[0]>=a[low] )
            ++low;
        a[high] = a[low];
    }
    a[low] = a[0];
    return low;
}
void sort(int a[],int low, int high)
{
    int i;
    if( low < high )
    {
        i = partion(low, high);
        sort(a,low, i-1);
        sort(a,i+1, high);
    }
}

void show(int n)
{int i,a[MAX];
    for(i=1; i<=n ; ++i )
        printf(" %d", a[i]);
}

int main()                  //主函数
{
int a[MAX],n;
int i,j=0;

while(1)
{
printf("\n 1随机生成顺序表       \n");
printf(" 2输出生成元素           \n");
printf(" 3paixu          \n");

scanf("%d",&i);

while(j==0&&i<0&&i>3)           //确认数组建立与否
{
printf("数组还没有建立!请重新选择功能号:");
scanf("%d",&i);}
switch(i)                    //功能选择
{

case 1:
    printf("键盘输入元素个数n\n");
    scanf("%d",&n);
    creat1(a,n);
    printf("随机生成数组元素成功!!\n\n");  
    break;
      
case 2: creat2(a,n);break;
        
case 3: sort(a,1,n);
    show(n);
      break;  
   

}}}快速排序的不知道哪里有错了求指导,急急急急
1