冒泡排序大比拼---看看谁的算法最简单

消失的尘芥 2009-04-06 11:50:39
加精
首先声明:此贴的目的是为了提高大家的学习兴趣。希望各位把自己写的冒泡排序方法贴上来。看谁的算法最经典。首先抛砖引玉献丑了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{

int[] a = { 3, 4, 7, 10, 5, 9 };
int[] b = BubbleSort(a);
for (int i = 0; i < b.Length; i++)
{
Console.Write(b[i].ToString() + " ");

}
Console.ReadLine();
}

public static int[] BubbleSort(int[] list)
{
int i, temp;
for (int j = 0; j < list.Length; j++)
{
for (i = list.Length - 1; i > j; i--)
{
if (list[j] < list[i])
{
temp = list[j];
list[j] = list[i];
list[i] = temp;
}
}
}

return list;
}
}
}
...全文
27362 514 打赏 收藏 转发到动态 举报
写回复
用AI写文章
514 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaceyxk 2012-07-22
  • 打赏
  • 举报
回复
两者的思路很简单啊,也很容易分清
kaceyxk 2012-07-22
  • 打赏
  • 举报
回复
冒泡不都一样吗?
  • 打赏
  • 举报
回复
很遗憾,很多人选择和冒泡分不清楚,还一味地经典、支持。。。
wwb562883398 2012-07-17
  • 打赏
  • 举报
回复
支持 学习了
狼_Sky 2012-07-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
算法都定了,
for (int j = 0; j < list.Length; j++)
{
for (i = list.Length - 1; i > j; i--)
{
if (list[j] < list[i])
{
temp = list[j];
list[j] = list[i];
list[i] = temp;
……
[/Quote]

搞错

不用temp
list[j]=list[j]+list[i];
list[i]=list[j]-list[i];
list[j]=list[j]-list[i];
狼_Sky 2012-07-17
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
首先声明:此贴的目的是为了提高大家的学习兴趣。希望各位把自己写的冒泡排序方法贴上来。看谁的算法最经典。首先抛砖引玉献丑了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
clas……
[/Quote]

不用temp
list[j]=list[j]+list[i];
list[i]=list[j]-list[i];
list[j]=list[j]-list[i];
续写经典 2012-07-09
  • 打赏
  • 举报
回复
误会!看错了!不好意思
续写经典 2012-07-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
算法都定了,
for (int j = 0; j < list.Length; j++)
{
for (i = list.Length - 1; i > j; i--)
{
if (list[j] < list[i]) //楼上的这一行代码好像有点问题吧
{
temp = list[j];
list[j] = list[i];
list[i] = temp;
……
[/Quote]
玩命小刀 2012-07-08
  • 打赏
  • 举报
回复
楼主应该说哪个排序算法吧。。。冒泡不就一种吗???
500G的忧伤 2012-07-07
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
int temp = 0;
int n = 0;
bool flag = true;
int[] array = new int[12] { 12, 85, 68, 54, 25, 25, 65, 85, 88, 96, 12, 56 };
for (int i = 0; i < array.Length; i++)
{
while (flag)
{
for (int j = 0; j < array.Length - i - 1; j++)
{

if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
n++;
flag = false;

}
}

}
}

for (int k = 0; k < array.Length; k++)
{
Console.Write(array[k].ToString() + " ");
Console.WriteLine(n.ToString());
}
}
如此怎样
danila0_o 2012-05-02
  • 打赏
  • 举报
回复
#include<iostream.h>
void main()
int i,j,temp, flag;
{
int i,j,temp, flag;
int a[10];
for(i=0;i<10;i++)
cin>>a[i];
for(i=0;i<=9;i++)
{flag=1;
for (j=0;j<10-i;j++)
if (a[j]>a[j+1])
{ temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}

for(i=1;i<11;i++)
cout<<a[i]<<endl;
}
  • 打赏
  • 举报
回复
for (int j = 0; j < list.Length; j++)
{
for (i = list.Length - 1; j<i; i--)
{
if (list[j] < list[i])
{
temp = list[j];
list[j] = list[i];
list[i] = temp;
}
}
guyue2043 2012-04-16
  • 打赏
  • 举报
回复
不错哦,值得学习
「已注销」 2012-04-10
  • 打赏
  • 举报
回复

int[] aa = {1,2,44,34,7,5,3};
for (int i = 0; i<aa.Length-1; i++)
{
for (int j = 0; j <aa.Length-1-i; j++)
{
if (aa[j] > aa[j+1])
{
int t = aa[j];
aa[j] = aa[j+1];
aa[j + 1] = t;
}
}
}
foreach (int b in aa)
{
Console.WriteLine(b);
}
Console.Read();

用那么麻烦吗? 两个for循环搞定..
在路上_等你 2012-03-24
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 madodo 的回复:]

我也纳闷了,冒泡法还有不同的泡吗?
[/Quote]
up
hk_qtxy 2012-03-23
  • 打赏
  • 举报
回复
冒泡排序法就那么一种最经典的
伽布里克 2012-03-23
  • 打赏
  • 举报
回复
从第一楼开始就不是冒泡吧???
冒泡不是比较a[i] a[i+1]然后交换么?
我写个自己的吧。
for(int i=1;i<=n-1;i++){
int x=a[i];
for(int j=i+1;j<=n;j++)
if(x<a[j])
a[i]=a[j],a[j]=x;
}
落魄骚年 2012-03-14
  • 打赏
  • 举报
回复
看得我手都痒痒了,写一个吧


int[] needSort = { 1010, 193, 2032, 1, 6, 7, 10, 4, 3, 21 };
int swap;

for (int j = needSort.Length - 1; j > 0; j--)
{
for (int i = 0; i < j; i++)
{
if (needSort[i] > needSort[i + 1])
{
swap = needSort[i + 1];
needSort[i + 1] = needSort[i];
needSort[i] = swap;
}

}
}

RoseOiO 2012-03-12
  • 打赏
  • 举报
回复
来学习了,万变不离其宗。。
wangpc607 2012-03-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lifetimeus 的回复:]

顶者都有分哦
[/Quote]
新人为了这句话来的 不过冒泡排序基本上就这几种算法吧 还有更高效的么 怀疑
加载更多回复(479)

110,577

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧