C#冒泡排序法
求各种写法的冒泡排序法。。 --------------------编程问答--------------------http://baike.baidu.com/view/1313793.htm --------------------编程问答--------------------
LZ,上面给的网址应有尽有,不管是哪门语言。 --------------------编程问答--------------------
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(); } --------------------编程问答-------------------- #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;
} --------------------编程问答-------------------- 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;
}
}
--------------------编程问答-------------------- 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;
}
}
--------------------编程问答-------------------- 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;
}
}
--------------------编程问答--------------------
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();
}
--------------------编程问答-------------------- 第一种
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];
}
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/>");
} --------------------编程问答-------------------- --------------------编程问答-------------------- 建议LZ还是自己理解冒泡排序法的原理,这样不管用到哪种编程语言都能写好
拿分走人... --------------------编程问答-------------------- 冒啥泡啊,直接List<int>.Sort() --------------------编程问答-------------------- 如果你是C用冒泡可以理解
都C# 还不泛型啊 --------------------编程问答-------------------- 将数组从小到大排列~
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); --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 我也是初学者,请大家多多指教!亲!
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();
} --------------------编程问答-------------------- 冒泡排序,也不懂
补充:.NET技术 , C#