4个回答
展开全部
用冒泡排序
private void bubble_sort(int[] a, int size)
{
bool flag = true;
int temp = 0;
for (int i = 0; i < size-1; i++)
{
flag = true;
for (int j = i+1; j < size ; j ++)
{
if (a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
flag = false;
} // end if
} // end for j = ...
if (flag == true)
break;
}
}
private void bubble_sort(int[] a, int size)
{
bool flag = true;
int temp = 0;
for (int i = 0; i < size-1; i++)
{
flag = true;
for (int j = i+1; j < size ; j ++)
{
if (a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
flag = false;
} // end if
} // end for j = ...
if (flag == true)
break;
}
}
展开全部
写个排序的算法就好,给你段代码参考下吧:
private static void Main(string[] args)
{
int[] iArrary = new int[] { 87, 12, 34, 75 };
Sort(iArrary);
for (int m = 0; m < iArrary.Length; m++)
Console.Write("{0} ", iArrary[m]);
Console.ReadLine();
}
//冒泡排序
public static 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++;
}
}
private static void Main(string[] args)
{
int[] iArrary = new int[] { 87, 12, 34, 75 };
Sort(iArrary);
for (int m = 0; m < iArrary.Length; m++)
Console.Write("{0} ", iArrary[m]);
Console.ReadLine();
}
//冒泡排序
public static 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++;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[6];
{
//输入数组
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("请输入第{0}个同学的分数", i);
array[i] = int.Parse(Console.ReadLine());
}
Sort(array);//排序
//输出
for (int i = 0; i<array.Length;i++)
{
Console.WriteLine(array[i].ToString());
}
Console.ReadLine();
}
}
///
/// 排序
///
///
public static void Sort(int[] array)
{
int i, j; // 循环变量
int temp; // 临时变量
for (i = 0; i < array.Length - 1; i++)
{
for (j = 0; j < array.Length - 1 - i; j++)
{
if (array[j] > array[j + 1])
{
// 交换元素
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
}
}
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[6];
{
//输入数组
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("请输入第{0}个同学的分数", i);
array[i] = int.Parse(Console.ReadLine());
}
Sort(array);//排序
//输出
for (int i = 0; i<array.Length;i++)
{
Console.WriteLine(array[i].ToString());
}
Console.ReadLine();
}
}
///
/// 排序
///
///
public static void Sort(int[] array)
{
int i, j; // 循环变量
int temp; // 临时变量
for (i = 0; i < array.Length - 1; i++)
{
for (j = 0; j < array.Length - 1 - i; j++)
{
if (array[j] > array[j + 1])
{
// 交换元素
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/// <summary>
/// 从小到大排列数组,并输出,用的是LINQ
/// </summary>
/// <param name="ar1">要排列的数组</param>
public static void Sort(int[] ar1)
{
var result = from i in ar1 orderby i descending select i ;
foreach (int i in result)
{
Console.WriteLine(i);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询