# 冒泡排序

659人阅读 评论(1)

＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊普通冒泡排序法BEGIN＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊

void main(){
int num[]={8,6,7,5,3,4,2,1,0,9},state=1;
for(int i=0;(i<=9)&&state;i++){
state=0;
for(int j=0;j<=9-i;j++)
if(num[j]>num[j+1]){
int temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
state=1;
}
}
for(i=0;i<=9;i++)
cout<<num[i]<<"   ";
}

＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊普通冒泡排序法OVER＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊

＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊冒泡排序法（链表实现）BEGIN＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊

typedef struct Node{
int v;
struct Node *next;
}Node;
void Insert(Node *L,int e){
Node *temp=new Node;
temp->v=e;
temp->next=NULL;
L->next=temp;
}
void main(){
Node *H=new Node,*L=new Node,*p0=new Node,*p1=new Node;
L=H;
Insert(H,8);
H=H->next;
Insert(H,6);
H=H->next;
Insert(H,7);
H=H->next;
Insert(H,5);
H=H->next;
Insert(H,3);
H=H->next;
Insert(H,4);
H=H->next;
Insert(H,2);
H=H->next;
Insert(H,1);
H=H->next;
Insert(H,0);
H=H->next;
Insert(H,9);
H=L;
L=L->next;
/*while(L!=NULL){
printf("%d  ",L->v);
L=L->next;
}//构造整个链表
//用链表实现出泡法  */
int state=1,Max=100;　//  将MAX的值设为100，利于第一轮循环
for(p0=H->next;p0!=NULL&&state;p0=p0->next){
state=0;
for(p1=H->next;p1->next!=NULL&&p1->v!=Max;p1=p1->next)
if((p1->v)>(p1->next->v)){
int a;
a=p1->v;
p1->v=p1->next->v;
p1->next->v=a;
state=1;
}
Max=0;
for(p1=H->next;p1->next!=NULL&&p1->v!=Max;p1=p1->next)　//此循环求出这一次范围中的最大值赋给MAX
if(p1->v>Max)
Max=p1->v;
}
while(L!=NULL){
printf("%d  ",L->v);
L=L->next;
}
}

＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊冒泡排序法（链表实现）OVER＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：61755次
• 积分：1128
• 等级：
• 排名：千里之外
• 原创：45篇
• 转载：12篇
• 译文：0篇
• 评论：17条
最新评论