| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 644 人关注过本帖
标题:快速排序,不知道哪里有问题,请高手指点!!!!!
只看楼主 加入收藏
ice_callous
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2010-10-14
结帖率:60%
收藏
已结贴  问题点数:30 回复次数:4 
快速排序,不知道哪里有问题,请高手指点!!!!!
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#define OK 1
#define NULL 0
#define LIST_INTT_SIZE 100000
#define LISTINCREMENT 10
#define OVERFLOW 0

typedef int Status;
typedef int ElemType;
typedef struct
{
    ElemType *elem;
    int Length;
    int MaxSize;
}SqList;


Status InitList_Sq(SqList &L)
{
    L.elem=(ElemType*)malloc(1000*sizeof(ElemType));
    if(!L.elem)
        exit(OVERFLOW);
    L.Length=0;
    L.MaxSize=LIST_INTT_SIZE;
    return OK;
}


void DataIn_Sq(SqList &L)
{
    int x;
    int i;
    for(i=1;i<10;i++)
    {
        scanf("%d",&x);
        L.elem[i-1]=x;
        L.Length++;
    }
}


void DataOut_Sq(SqList &L)
{
    int i;
    for(i=0;i<L.Length;i++)
        printf("%d ",L.elem[i]);
}



Status Partition(SqList &L, int low, int high)
{
    int temp;
    int pivotkey;
    pivotkey=L.elem[low];
    while(low<high)
    {
        while(low<high && L.elem[high]>=pivotkey)
            --high;
        temp=L.elem[low];
        L.elem[low]=L.elem[high];
        L.elem[high]=temp;
        while(low<high && L.elem[low]<=pivotkey)
            ++low;
        temp=L.elem[low];
        L.elem[low]=L.elem[high];
        L.elem[high]=temp;
    }
        return low;
}




void QuickSort(SqList &L, int low, int high)
{
    int pivotkey;
    if(low<high)
        pivotkey=Partition(L,low,high);
    QuickSort(L,low,pivotkey-1);
    QuickSort(L,pivotkey+1,high);
}



void DestroyList(SqList &L)
{
   free(L.elem);
   L.elem=NULL;
   L.Length=0;
   L.MaxSize=0;
}




void main()
{
    SqList MyList;
    InitList_Sq(MyList);
    DataIn_Sq(MyList);
    DataOut_Sq(MyList);
    QuickSort(MyList,Partition(MyList,0,1000),MyList.MaxSize);
    DataOut_Sq(MyList);
    DestroyList(MyList);
}

搜索更多相关主题的帖子: include return include return 
2010-11-18 16:49
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:30 
原始的问题是数组越界  和 代码copy遗漏
//.cpp文件
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#define OK 1
#define NULL 0
#define LIST_INTT_SIZE 100000
#define LISTINCREMENT 10
#define OVERFLOW 0

typedef int Status;
typedef int ElemType;
typedef struct
{
    ElemType *elem;
    int Length;
    int MaxSize;
}SqList;


Status InitList_Sq(SqList &L)
{
    L.elem=(ElemType*)malloc(1000*sizeof(ElemType));
    if(!L.elem)
        exit(OVERFLOW);
    L.Length=0;
    L.MaxSize = LIST_INTT_SIZE;
    return OK;
}


void DataIn_Sq(SqList &L)
{
    int x;
    int i;
    for(i=1;i<=5;i++)
    {
        scanf("%d",&x);
        L.elem[i-1]=x;
        L.Length++;
    }
}


void DataOut_Sq(SqList &L)
{
    int i;
    for(i=0;i<L.Length;i++)
        printf("%d ",L.elem[i]);
    printf("\n");
}



Status Partition(SqList &L, int low, int high)
{
    int temp;
    int pivotkey;
    pivotkey = L.elem[low];
    while(low<high)
    {
        while(low<high && L.elem[high]>=pivotkey)
            --high;
        temp=L.elem[low];
        L.elem[low]=L.elem[high];
        L.elem[high]=temp;
        while(low<high && L.elem[low]<=pivotkey)
            ++low;
        temp=L.elem[low];
        L.elem[low]=L.elem[high];
        L.elem[high]=temp;
    }
    return low;
}




void QuickSort(SqList &L, int low, int high)
{
    int pivotkey;
    if(low<high)
    {
        pivotkey=Partition(L,low,high);
        QuickSort(L,low,pivotkey-1);
        QuickSort(L,pivotkey+1,high);
    }
}



void DestroyList(SqList &L)
{
   free(L.elem);
   L.elem = NULL;
   L.Length = 0;
   L.MaxSize = 0;
}

void main()
{
    SqList MyList;
    InitList_Sq(MyList);
    DataIn_Sq(MyList);
    DataOut_Sq(MyList);
    QuickSort(MyList,0,MyList.Length-1);
    DataOut_Sq(MyList);
    DestroyList(MyList);
}
2010-11-18 22:18
ice_callous
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2010-10-14
收藏
得分:0 
回复 2楼 寒风中的细雨
多谢指点
2010-11-18 22:26
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
#include <stdio.h>

int a[] = {0, 49, 78, 12, 13, 90, 21, 56, 23, 9, 10 };/*零下标作为哨兵*/

int partion( int low, int high )
{
    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 low, int high)
{
    int i;
    if( low < high )
    {
        i = partion(low, high);
        sort(low, i-1);
        sort(i+1, high);
    }
}

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

int main()
{
    sort(1, 10);
    show();
    printf("\n");
    return 0;
}
2010-11-19 12:20
h2363752280
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2012-12-19
收藏
得分:0 
#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;  
   

}}}
可以帮忙修改一下吗,快速排序排不了,不知道为什么,急急急急急
2012-12-24 13:43
快速回复:快速排序,不知道哪里有问题,请高手指点!!!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024242 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved