这是在网上看到一个高手用链表编的排序
链表应该是最好的
/*---------------------------------------
基于链式存储方式下的排序操作(插入排序)
----------------------------------------*/
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{ int data;
struct node*next;
};
typedef struct node Node;
Node *CreateList();
void PrintList(Node *HL);
Node *SortList(Node *HL);
int main()
{ Node *HL;
HL=CreateList();
PrintList(HL);
HL=SortList(HL);
PrintList(HL);
getch();
return 0;
}
Node *CreateList()
{ Node *HL,*P;
int D;
HL=P=(Node *)malloc(sizeof(Node));
scanf("%d",&D);
while(D!=0)
{ P->next=(Node *)malloc(sizeof(Node));
P->next->data=D;
P=P->next;
scanf("%d",&D);
}
P->next=NULL;
return HL;
}
void PrintList(Node *HL)
{ Node *P=HL;
while(P=P->next)
printf("%d ",P->data);
putchar('\n');
}
Node * SortList(Node *HL)
{ Node *NHL,*NP,*P,*AP,*OP=HL->next;
int NewD;
NHL=NP=(Node *)malloc(sizeof(Node));
NP->next=NULL;
while(OP)
{ NewD=OP->data;
AP=NHL; P=NHL->next;
while(P)
if(NewD<=P->data) break;
else { AP=AP->next; P=P->next; }
NP=(Node *)malloc(sizeof(Node));
NP->data=NewD;
AP->next=NP;
NP->next=P;
OP=OP->next;
}
return NHL;
}
链表应该是最好的
/*---------------------------------------
基于链式存储方式下的排序操作(插入排序)
----------------------------------------*/
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{ int data;
struct node*next;
};
typedef struct node Node;
Node *CreateList();
void PrintList(Node *HL);
Node *SortList(Node *HL);
int main()
{ Node *HL;
HL=CreateList();
PrintList(HL);
HL=SortList(HL);
PrintList(HL);
getch();
return 0;
}
Node *CreateList()
{ Node *HL,*P;
int D;
HL=P=(Node *)malloc(sizeof(Node));
scanf("%d",&D);
while(D!=0)
{ P->next=(Node *)malloc(sizeof(Node));
P->next->data=D;
P=P->next;
scanf("%d",&D);
}
P->next=NULL;
return HL;
}
void PrintList(Node *HL)
{ Node *P=HL;
while(P=P->next)
printf("%d ",P->data);
putchar('\n');
}
Node * SortList(Node *HL)
{ Node *NHL,*NP,*P,*AP,*OP=HL->next;
int NewD;
NHL=NP=(Node *)malloc(sizeof(Node));
NP->next=NULL;
while(OP)
{ NewD=OP->data;
AP=NHL; P=NHL->next;
while(P)
if(NewD<=P->data) break;
else { AP=AP->next; P=P->next; }
NP=(Node *)malloc(sizeof(Node));
NP->data=NewD;
AP->next=NP;
NP->next=P;
OP=OP->next;
}
return NHL;
}