链表的一种排序---冒泡排序
来源:互联网 发布:哪里有唐筛计算软件 编辑:程序博客网 时间:2024/05/18 00:36
#include <iostream>
using namespace std;
struct node
{
int a;
node *next;
{
int n,m;
cin>>n;
m=n;
node *first=new node,*rear=new node;
first->next=NULL;
rear=first;
while(n--)
{
node *x=new node;
cin>>x->a;
rear->next=x;
rear=x;
}
rear->next=NULL;
rear=first;
//冒泡排序
node *n_max,*p,*h;
m-=1;
while(m--)
{
h=rear;
p=rear->next;
while(p&&p->next)
{
n_max=p;
p=p->next;
if(n_max->a>p->a)
{
h->next=n_max->next;
n_max->next=p->next;
p->next=n_max;
p=n_max;
}
h=h->next;
}
{
p=rear;
cout<<rear->next->a<<" ";
rear=rear->next;
delete p;
}
return 0;
using namespace std;
struct node
{
int a;
node *next;
};
{
int n,m;
cin>>n;
m=n;
node *first=new node,*rear=new node;
first->next=NULL;
rear=first;
while(n--)
{
node *x=new node;
cin>>x->a;
rear->next=x;
rear=x;
}
rear->next=NULL;
rear=first;
//冒泡排序
node *n_max,*p,*h;
m-=1;
while(m--)
{
h=rear;
p=rear->next;
while(p&&p->next)
{
n_max=p;
p=p->next;
if(n_max->a>p->a)
{
h->next=n_max->next;
n_max->next=p->next;
p->next=n_max;
p=n_max;
}
h=h->next;
}
}
//输出
while(rear&&rear->next){
p=rear;
cout<<rear->next->a<<" ";
rear=rear->next;
delete p;
}
return 0;
}
此外还有选择排序,插入排序,方法基本一样
0 0
- 链表的一种排序---冒泡排序
- 冒泡排序的一种实现
- 一种冒泡排序
- 链表的冒泡排序
- 链表的冒泡排序
- 链表的冒泡排序
- 链表的冒泡排序
- 链表的冒泡排序
- 链表的冒泡排序
- 链表的冒泡排序。
- 链表的冒泡排序
- 链表的排序(冒泡)
- 链表的冒泡排序
- 链表的冒泡排序
- 链表的冒泡排序
- 链表的冒泡排序
- 排序算法之冒泡排序:一种优化的冒泡排序算法
- 每天一种算法 - 冒泡排序
- C++ Primer 第六章 语句(try 块和异常处理)
- Median of Two Sorted Array
- Nodejs学习路线图
- Combination Sum
- C++ Primer 第七章 函数
- 链表的一种排序---冒泡排序
- how to find the address of git to download linux kernel soure
- 手游业蓝海市场不再,开发者生存困境该如何解围?
- 技术合伙人不是为了节省开发成本忽悠人的噱头
- iOS开发者的资源库必不可少的资源有?
- Longest Substring without repeating Characters
- Max Points on a Line
- 小气是一种病
- Combination Sum II