C#冒泡排序法

beginners 2012-03-21 01:51:50
求各种写法的冒泡排序法。。
...全文
1277 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
何大哥 2012-12-24
  • 打赏
  • 举报
回复
冒泡排序,也不懂
Smile_大笑 2012-12-24
  • 打赏
  • 举报
回复
我也是初学者,请大家多多指教!亲! static void Main(string[] args) { Random RD = new Random(); int [] nums=new int[10]; Console.WriteLine("排序前:"); for (int i = 0; i < nums.Length; i++) { nums[i] = RD.Next(-10,10); Console.Write(nums [i]+","); } //冒泡排序 for (int i = 0; i < nums.Length; i++) { for (int j = 0; j < nums.Length - 1 - i; j++) { if (nums[j] < nums[j + 1]) { int temp = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = temp; } } } Console.WriteLine("排序后:"); foreach (int n in nums) { Console.Write(n+","); } Console.ReadKey(); }
lzw8036 2012-03-22
  • 打赏
  • 举报
回复
将数组从小到大排列~

public void Sort(string[] list)
{
int i,j;
string temp = "";
bool done = flase;
j = 1;
while(j<list.Length)&&(!done))
{
for(i =0;i<list.Length-j;i++)
{
if(Convert.Tpnt32(list[i])>Convert.TpInt32(list[i+1])
{
done = false;
temp = list[i];
list[i] = list[i+1];
list[i+1] = temp;
}
}
j++;
}
}
console.writeLine(list);
tangxu12 2012-03-22
  • 打赏
  • 举报
回复
如果你是C用冒泡可以理解

都C# 还不泛型啊
showlie 2012-03-22
  • 打赏
  • 举报
回复
冒啥泡啊,直接List<int>.Sort()
屌絲來襲 2012-03-22
  • 打赏
  • 举报
回复
建议LZ还是自己理解冒泡排序法的原理,这样不管用到哪种编程语言都能写好

拿分走人...
贾志轩 2012-03-22
  • 打赏
  • 举报
回复
第一种
int[] Num = { 3, 4, 7, 10, 5, 9 };
int TP;
for (int i = 0; Num.Length > i; i++)//从最小开始
{
for (int p = Num.Length - 1; p > i; p--)//从最大开始,外层循环过的跳过。
{
TP = Num[i];
if (Num[i] < Num[p])//这里决定升序或降序
{
Num[i] = Num[p];
Num[p] = TP;
}
}
}
Response.Write("<br/><br/>");
for (int i = 0; Num.Length > i; i++)
{
Response.Write(Num[i] + "<br/>");
}

第二种
int[] Num = { 3, 4, 7, 10, 5, 9 };
int TP;
for (int i = 0; Num.Length > i; i++)//从最小开始
{
for (int p = i + 1; Num.Length > p; p++)//外层循环过的跳过。
{
TP = Num[i];
if (Num[i] < Num[p])//这里决定升序或降序
{
Num[i] = Num[p];
Num[p] = TP;
}
}
}
Response.Write("<br/><br/>");
for (int i = 0; Num.Length > i; i++)
{
Response.Write(Num[i] + "<br/>");
}
水中钓鱼的猫 2012-03-21
  • 打赏
  • 举报
回复
冒泡可以优化一下,就是在交换那里

if(arr[i]>arr[i+1]){
arr[i]=arr[i]+arr[i+1];
arr[i+1]=arr[i]-arr[i+1];
arr[i]=arr[i]-arr[i+1];
}
全局变量 2012-03-21
  • 打赏
  • 举报
回复
- -. 百度一下你就知道....
mr_cheung 2012-03-21
  • 打赏
  • 举报
回复
static void Main(string[] args)
  {
  int[] array = { 23,45,16,7,42 };
  int length = array.Length - 1;
  bool isExchanged = false;
  for (int i = 0; i < length; i++)
  {
  isExchanged = false;
  for (int j = length; j > i; j--)
  {
  if (array[j] > array[j - 1])
  {
  int temp = array[j];
  array[j] = array[j - 1];
  array[j - 1] = temp;
  isExchanged = true;
  }
  }
  if (!isExchanged)//一遍比较过后如果没有进行交换则退出循环
  break;
  }
  foreach (int i in array)
  {
  Console.WriteLine(i);
  }
  Console.Read();
  }
Agenric 2012-03-21
  • 打赏
  • 举报
回复
using System;

public class BubbleSortTest
{
public static void Main( string[] args )
{
BubbleSort sortArray = new BubbleSort( 10 );

Console.WriteLine( "Before:" );
Console.WriteLine( sortArray );

sortArray.Sort();

Console.WriteLine( "After:" );
Console.WriteLine( sortArray );
}
}




using System;

public class BubbleSort
{
private int[] data;
private static Random generator = new Random();

public BubbleSort( int size )
{
data = new int[ size ];

for ( int i = 0; i < size; i++ )
data[ i ] = generator.Next( 10, 100 );
}

public void Sort()
{
for ( int pass = 1; pass < data.Length; pass++ )
{
for ( int index = 0; index < data.Length - 1; index++ )
{
if ( data[ index ] > data[ index + 1 ] )
Swap( index, index + 1 );
}
}
}

public void Swap( int first, int second )
{
int temporary = data[ first ];
data[ first ] = data[ second ];
data[ second ] = temporary;
}

public override string ToString()
{
string temporary = string.Empty;

foreach ( var element in data )
temporary += element + " ";

temporary += "\n";
return temporary;
}
}
Agenric 2012-03-21
  • 打赏
  • 举报
回复
using System;

public class BubbleSortTest
{
public static void Main( string[] args )
{
BubbleSort sortArray = new BubbleSort( 10 );

Console.WriteLine( "Before:" );
Console.WriteLine( sortArray );

sortArray.Sort();

Console.WriteLine( "After:" );
Console.WriteLine( sortArray );
}
}




using System;

public class BubbleSort
{
private int[] data;
private static Random generator = new Random();

public BubbleSort( int size )
{
data = new int[ size ];

for ( int i = 0; i < size; i++ )
data[ i ] = generator.Next( 10, 100 );
}

public void Sort()
{
for ( int pass = 1; pass < data.Length; pass++ )
{
for ( int index = 0; index < data.Length - 1; index++ )
{
if ( data[ index ] > data[ index + 1 ] )
Swap( index, index + 1 );
}
}
}

public void Swap( int first, int second )
{
int temporary = data[ first ];
data[ first ] = data[ second ];
data[ second ] = temporary;
}

public override string ToString()
{
string temporary = string.Empty;

foreach ( var element in data )
temporary += element + " ";

temporary += "\n";
return temporary;
}
}
Agenric 2012-03-21
  • 打赏
  • 举报
回复
using System;

public class BubbleSortTest
{
public static void Main( string[] args )
{
BubbleSort sortArray = new BubbleSort( 10 );

Console.WriteLine( "Before:" );
Console.WriteLine( sortArray );

sortArray.Sort();

Console.WriteLine( "After:" );
Console.WriteLine( sortArray );
}
}




using System;

public class BubbleSort
{
private int[] data;
private static Random generator = new Random();

public BubbleSort( int size )
{
data = new int[ size ];

for ( int i = 0; i < size; i++ )
data[ i ] = generator.Next( 10, 100 );
}

public void Sort()
{
for ( int pass = 1; pass < data.Length; pass++ )
{
for ( int index = 0; index < data.Length - 1; index++ )
{
if ( data[ index ] > data[ index + 1 ] )
Swap( index, index + 1 );
}
}
}

public void Swap( int first, int second )
{
int temporary = data[ first ];
data[ first ] = data[ second ];
data[ second ] = temporary;
}

public override string ToString()
{
string temporary = string.Empty;

foreach ( var element in data )
temporary += element + " ";

temporary += "\n";
return temporary;
}
}
whj11 2012-03-21
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
int main()
{
int a[5];
int i,j,t;
for(i=0;i<5;i++)
cin>>a[i];


for(j=0;j<4;j++)
for(i=0;i<4-j;i++)
if(a[i]>a[i+1])
{
t=a[i];a[i]=a[i+1];a[i+1]=t;
}

for(i=0;i<=4;i++)
cout<<a[i]<<" ";



return 0;
}
  • 打赏
  • 举报
回复
[Quote=引用楼主 hzw781189684 的回复:]
求各种写法的冒泡排序法。。
[/Quote]

static void Main(string[] args)   {   int[] array = { 23,45,16,7,42 };   int length = array.Length - 1;   bool isExchanged = false;   for (int i = 0; i < length; i++)   {   isExchanged = false;   for (int j = length; j > i; j--)   {   if (array[j] > array[j - 1])   {   int temp = array[j];   array[j] = array[j - 1];   array[j - 1] = temp;   isExchanged = true;   }   }   if (!isExchanged)//一遍比较过后如果没有进行交换则退出循环   break;   }   foreach (int i in array)   {   Console.WriteLine(i);   }   Console.Read();   }
  • 打赏
  • 举报
回复
[Quote=引用楼主 hzw781189684 的回复:]
求各种写法的冒泡排序法。。
[/Quote]

LZ,上面给的网址应有尽有,不管是哪门语言。
  • 打赏
  • 举报
回复
[Quote=引用楼主 hzw781189684 的回复:]
求各种写法的冒泡排序法。。
[/Quote]

http://baike.baidu.com/view/1313793.htm

110,533

社区成员

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

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

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