#include <stdio.h>
#include "算法排序.cpp"
void main ()
{
SqList L;
InitList_L (L);
int n,i;
printf ("输入待排序记录的个数n: ");
scanf ("%d",&n);
for (i=1;i<=n;i++)
{
printf ("输入第%d个记录的关键字以及其他信息: ",i);
scanf ("%d",&L.r[i].key);
scanf ("%d",&L.r[i].otherinfo);
L.length++;
}
//SelectSort (L);
//InsertSort (L);
//BubbleSort (L);
QuickSort (L);
for (i=1;i<=n;i++)
{
printf ("%d %d",L.r[i].key,L.r[i].otherinfo);
printf ("\n");
}
//DestroyList_L (L);
}
1-2.cpp如下所示
#include <iostream>
#include <stdio.h>
#define MAXSIZE 10
typedef int KEYTYPE;
typedef int INFOTYPE;
typedef struct
{
KEYTYPE key;
INFOTYPE otherinfo;
}RcdType;
typedef struct
{
RcdType *r;
//RcdType r[MAXSIZE+1];
int length;
}SqList;
void InitList_L (SqList &L)
{
L.r=new RcdType [MAXSIZE+1];
L.length=0;
}
void DestroyList_L (SqList &L)
{
delete [] L.r;
L.length=0;
}
/********************************************/
/****** 选择排序 *************/
/********************************************/
void SelectSort (SqList &L)
{
RcdType w;
int i,j,k;
for (i=1;i<L.length;i++)
{
j=i;
for (k=i+1;k<=L.length;k++)
if (L.r[k].key<L.r[j].key)
j=k;
if (j!=i)
{
w=L.r[i];
L.r[i]=L.r[j];
L.r[j]=w;
}
}
}
/********************************************/
/****** 插入排序 *************/
/********************************************/
void InsertSort (SqList &L)
{
int i,j;
for (i=2;i<=L.length;i++)
{
if (L.r[i].key<L.r[i-1].key)
{
L.r[0]=L.r[i];
for (j=i-1;L.r[0].key<L.r[j].key;j--)
L.r[j+1]=L.r[j];
L.r[j+1]=L.r[0];
}
}
}
/********************************************/
/****** 起泡排序 *************/
/********************************************/
void BubbleSort (SqList &L)
{
RcdType w;
int i,j;
i=L.length;
int lastExchangeIndex;
while (i>1)
{
lastExchangeIndex=1;
for (j=1;j<i;j++)
{
if (L.r[j].key>L.r[j+1].key)
{
w=L.r[j];
L.r[j]=L.r[j+1];
L.r[j+1]=w;
}
lastExchangeIndex=j;
}
i=lastExchangeIndex;
}
}
/********************************************/
/****** 快速排序 *************/
/********************************************/
int Partition (RcdType R[],int low,int high)
{
R[0]=R[low];
while (low<high)
{
while (low<high && R[high].key<R[0].key)
high--;
R[low++]=R[high];
while (low<high && R[low].key>R[0].key)
low++;
R[high--]=R[low];
}
R[low]=R[0];
return low;
}
void QSort (RcdType R[],int s,int t)
{
int pivotloc;
if (s<t)
{
pivotloc=Partition(R,s,t);
QSort (R,s,pivotloc-1);
QSort (R,pivotloc+1,t);
}
}
void QuickSort (SqList &L)
{
QSort (L.r,1,L.length);
}
为什么得不到预期的结果?