主题:[讨论]快速排序问题!
大家好!这个是我编写的快速排序程序!
好像在qsort函数处有点问题!
大家帮帮看看!
谢谢!
#define TRUE 1
#define FALES 0
#define OK 1
#define ERROR 0
#define OVERFLLOW 0
#define INFEASIBLE -1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int status;
typedef int ELEMTYPE;
typedef struct{
ELEMTYPE *elem;
int length;
int listsize;
}sqlist;
status initlist_sq(sqlist *L)
{L->elem=(ELEMTYPE*)malloc(LIST_INIT_SIZE*sizeof(ELEMTYPE));
if(!L->elem)exit(OVERFLLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
void change(int a,int b)
{int temp;
temp=a;
a=b;
b=temp;
}
status partition(sqlist *L,int low,int high)
{int pivotkey;
L->elem[0]=L->elem[low];
pivotkey=L->elem[low];
while(low<high)
{while(low<high&&L->elem[high]>pivotkey)--high;
change(L->elem[low],L->elem[high]);
while(low<high&&L->elem[low]<=pivotkey)++low;
change(L->elem[high],L->elem[low]);
}
L->elem[low]=L->elem[0];
return low;
}
void qsort(sqlist *L,int low,int high)
{int pivot;
if(low<high)
{pivot=partition(L,low,high);
qsort(L,low,pivot-1);
qsort(L,pivot+1,high);
}
}
status scan_sqlist(sqlist *L)
{int i=0;
printf("please input nums:\n");
while(i!='0')
{scanf("%d",L->elem[i]);
i++;
}
L->length=i;
}
void print_sq(sqlist *L)
{int i;
printf("the change num is:\n");
for(i=0;i<L->length-1;i++)
printf("%5d",L->elem[i]);
printf("\n");
}
void main()
{sqlist *L;
initlist_sq(&L);
scan_sq(L);
qsort(L,0,L->length-1);
print_sq(L);
}
好像在qsort函数处有点问题!
大家帮帮看看!
谢谢!
#define TRUE 1
#define FALES 0
#define OK 1
#define ERROR 0
#define OVERFLLOW 0
#define INFEASIBLE -1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int status;
typedef int ELEMTYPE;
typedef struct{
ELEMTYPE *elem;
int length;
int listsize;
}sqlist;
status initlist_sq(sqlist *L)
{L->elem=(ELEMTYPE*)malloc(LIST_INIT_SIZE*sizeof(ELEMTYPE));
if(!L->elem)exit(OVERFLLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
void change(int a,int b)
{int temp;
temp=a;
a=b;
b=temp;
}
status partition(sqlist *L,int low,int high)
{int pivotkey;
L->elem[0]=L->elem[low];
pivotkey=L->elem[low];
while(low<high)
{while(low<high&&L->elem[high]>pivotkey)--high;
change(L->elem[low],L->elem[high]);
while(low<high&&L->elem[low]<=pivotkey)++low;
change(L->elem[high],L->elem[low]);
}
L->elem[low]=L->elem[0];
return low;
}
void qsort(sqlist *L,int low,int high)
{int pivot;
if(low<high)
{pivot=partition(L,low,high);
qsort(L,low,pivot-1);
qsort(L,pivot+1,high);
}
}
status scan_sqlist(sqlist *L)
{int i=0;
printf("please input nums:\n");
while(i!='0')
{scanf("%d",L->elem[i]);
i++;
}
L->length=i;
}
void print_sq(sqlist *L)
{int i;
printf("the change num is:\n");
for(i=0;i<L->length-1;i++)
printf("%5d",L->elem[i]);
printf("\n");
}
void main()
{sqlist *L;
initlist_sq(&L);
scan_sq(L);
qsort(L,0,L->length-1);
print_sq(L);
}