用c#编写一个冒泡法排序的程序
发布网友
发布时间:2022-04-20 00:03
我来回答
共10个回答
热心网友
时间:2022-05-22 02:56
using System;
using System.Collections.Generic;
using System.Text;
namespace GanggangApplication
{
class Program
{
static void Main(string[] args)
{
SortedNumbers();
}
/// <summary>
/// 该方法获得需要排序的数组,表调用排序方法进行排序
/// </summary>
public static void SortedNumbers()
{
int numberCount;
int[] numbers;
Console.WriteLine("请问您要对多少个数字进行排序?");
numberCount = Convert.ToInt32(Console.ReadLine());
numbers = new int[numberCount];
Console.WriteLine("请输入您要进行排序的这{0}个数字:", numberCount);
for (int i = 0; i < numberCount; i++)
{
numbers[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("\n您要进行排序的{0}个数字分别为:", numberCount);
for (int i = 0; i < numberCount; i++)
{
Console.Write(numbers[i].ToString() + "\t");
}
Console.WriteLine("\n您要对这{0}个数字进行什么排序?(1表示冒泡排序,2表示选择排序)", numberCount);
int method = Convert.ToInt32(Console.ReadLine());
while (method != 1 && method != 2)
{
Console.WriteLine("只能输入1或者2,请您重新输入!");
method = Convert.ToInt32(Console.ReadLine());
}
//调用排序方法
ExecuteSortedMethod(numbers, method);
Console.WriteLine("排序后的结果为:");
for (int i = 0; i < numberCount; i++)
{
Console.Write(numbers[i].ToString() + "\t");
}
Console.ReadKey();
}
/// <summary>
/// 接受数字参数和排序方法参数,进行排序
/// </summary>
/// <param name="num">要进行排序的整数数组</param>
/// <param name="sortedMethod">排序方法标识:1为冒泡,2为选择</param>
public static void ExecuteSortedMethod(int[] num, int sortedMethod)
{
if (sortedMethod == 1) //冒泡排列
{
for (int i = 0; i < num.Length - 1; i++)
{
for (int j = 0; j < num.Length - 1 - i; j++)
{
if (num[j] > num[j + 1])
{
int temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
}
}
if (sortedMethod == 2) //选择排列
{
int min;
for(int i=0;i<num.Length-1;i++)
{
min=i;
for(int j=i+1;j<num.Length;j++){
if(num[j]<num[min])
min=j;
}
int t=num[min];
num[min]=num[i];
num[i]=t;
}
}
}
}
}
热心网友
时间:2022-05-22 04:14
楼上的那家伙写的是差不多的.main函数,把给定数字,变成readline方法即可接受自己定义的数组.
热心网友
时间:2022-05-22 05:49
int[] b={34,43,656,57,4,4,454,545,9};
for (int i=0;i<b.length;i++)
{
for(int j=0;j<b.length;j++)
{
int temp;
if(b[i]>b[j])
{
temp= b[i]
}
}
}
............自己把它填完就好。
热心网友
时间:2022-05-22 07:40
int[]
b={34,43,656,57,4,4,454,545,9};
for
(int
i=0;i<b.length;i++)
{
for(int
j=0;j<b.length;j++)
{
int
temp;
if(b[i]>b[j])
{
temp=
b[i]
}
}
}
............自己把它填完就好。
热心网友
时间:2022-05-22 09:48
一、冒泡排序(Bubble)
using
System;
namespace
BubbleSorter
{
public
class
BubbleSorter
{
public
void
Sort(int[]
list)
{
int
i,j,temp;
bool
done=false;
j=1;
while((j<list.Length)&&(!done))
{
done=true;
for(i=0;i<list.Length-j;i++)
{
if(list[i]>list[i+1])
{
done=false;
temp=list[i];
list[i]=list[i+1];
list[i+1]=temp;
}
}
j++;
}
}
}
public
class
MainClass
{
public
static
void
Main()
{
int[]
iArrary=new
int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
BubbleSorter
sh=new
BubbleSorter();
sh.Sort(iArrary);
for(int
m=0;m<iArrary.Length;m++)
Console.Write("{0}
",iArrary[m]);
Console.WriteLine();
}
}
}
二、选择排序(Selection)
using
System;
namespace
SelectionSorter
{
public
class
SelectionSorter
{
private
int
min;
public
void
Sort(int
[]
list)
{
for(int
i=0;i<list.Length-1;i++)
{
min=i;
for(int
j=i+1;j<list.Length;j++)
{
if(list[j]<list[min])
min=j;
}
int
t=list[min];
list[min]=list[i];
list[i]=t;
}
}
}
public
class
MainClass
{
public
static
void
Main()
{
int[]
iArrary
=
new
int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
SelectionSorter
ss=new
SelectionSorter();
ss.Sort(iArrary);
for
(int
m=0;m<iArrary.Length;m++)
Console.Write("{0}
",iArrary[m]);
Console.WriteLine();
}
}
}
热心网友
时间:2022-05-22 12:13
class
EbullitionSorter
{
public
void
Sort(int[]
arr)
{
int
i,
j,
temp;
bool
done
=
false;
j
=
1;
while
((j
<
arr.Length)
&&
(!done))//判断长度
{
done
=
true;
for
(i
=
0;
i
<
arr.Length
-
j;
i++)
{
if
(arr[i]
>
arr[i
+
1])
{
done
=
false;
temp
=
arr[i];
arr[i]
=
arr[i
+
1];//交换数据
arr[i
+
1]
=
temp;
}
}
j++;
}
}
static
void
Main(string[]
args)
{
int[]
array
=
new
int[]
{
1,
5,
3,
6,
10,
55,
9,
2,
87,
12,
34,
75,
33,
47
};
EbullitionSorter
e
=
new
EbullitionSorter
();
e.Sort(array);
foreach
(int
m
in
array)
Console.WriteLine("{0}",
m);
}
}
热心网友
时间:2022-05-22 14:54
一、冒泡排序(Bubble)
using System;
namespace BubbleSorter
{
public class BubbleSorter
{
public void Sort(int[] list)
{
int i,j,temp;
bool done=false;
j=1;
while((j<list.Length)&&(!done))
{
done=true;
for(i=0;i<list.Length-j;i++)
{
if(list[i]>list[i+1])
{
done=false;
temp=list[i];
list[i]=list[i+1];
list[i+1]=temp;
}
}
j++;
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
BubbleSorter sh=new BubbleSorter();
sh.Sort(iArrary);
for(int m=0;m<iArrary.Length;m++)
Console.Write("{0} ",iArrary[m]);
Console.WriteLine();
}
}
}
二、选择排序(Selection)
using System;
namespace SelectionSorter
{
public class SelectionSorter
{
private int min;
public void Sort(int [] list)
{
for(int i=0;i<list.Length-1;i++)
{
min=i;
for(int j=i+1;j<list.Length;j++)
{
if(list[j]<list[min])
min=j;
}
int t=list[min];
list[min]=list[i];
list[i]=t;
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary = new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
SelectionSorter ss=new SelectionSorter();
ss.Sort(iArrary);
for (int m=0;m<iArrary.Length;m++)
Console.Write("{0} ",iArrary[m]);
Console.WriteLine();
}
}
}
热心网友
时间:2022-05-22 17:52
int[] arr = new int[] {1,62,2,3,65,1212,12};
int n = 7;
for (int i = 0; i < n; i++)
for (int j = 0; j < arr.Length-i-1; j++)
{
int tt = 0;
if (arr[j+1]<arr[j])
{
tt = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = tt;
}
}
for (int i = 0; i < arr.Length; i++)Console.WriteLine(arr[i]);
热心网友
时间:2022-05-22 21:07
class EbullitionSorter { public void Sort(int[] arr) { int i, j, temp; bool done = false; j = 1; while ((j < arr.Length) && (!done))//判断长度 { done = true; for (i = 0; i < arr.Length - j; i++) { if (arr[i] > arr[i + 1]) { done = false; temp = arr[i]; arr[i] = arr[i + 1];//交换数据 arr[i + 1] = temp; } } j++; } } static void Main(string[] args) { int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 }; EbullitionSorter e = new EbullitionSorter (); e.Sort(array); foreach (int m in array) Console.WriteLine("{0}", m); } }
热心网友
时间:2022-05-23 00:38
int[]
arr
=
new
int[]
{1,62,2,3,65,1212,12};
int
n
=
7;
for
(int
i
=
0;
i
<
n;
i++)
for
(int
j
=
0;
j
<
arr.Length-i-1;
j++)
{
int
tt
=
0;
if
(arr[j+1]<arr[j])
{
tt
=
arr[j
+
1];
arr[j
+
1]
=
arr[j];
arr[j]
=
tt;
}
}
for
(int
i
=
0;
i
<
arr.Length;
i++)Console.WriteLine(arr[i]);