为数组排序算法 [英] Sorting algo for an array
本文介绍了为数组排序算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!
我正在考虑排序算法,我为此编写了一个小算法...它可以更加精致......
Hi Guys!
I was thinking for sorting algo,i have written a small algo for this...can it be more refined...
int[] unSortedArray = { 9, 4, 5, 1,7, 3, 8, 2, 6 };
int j = 0;
for (int i = 0; i < (unSortedArray.Length - 1) * (unSortedArray.Length - j); i++)
{
if (i == unSortedArray.Length - j -1)
{
i = 0;
j++;
}
if (unSortedArray[i] > unSortedArray[i + 1])
{
int temp = unSortedArray[i];
unSortedArray[i] = unSortedArray[i + 1];
unSortedArray[i + 1] = temp;
}
}
任何想法都表示赞赏!!!
问候,
Vikas
Any thought is appreciated!!!
Regards,
Vikas
推荐答案
你可以在这里找到解决方法:
C#中排序算法的可视化和比较/ a> [ ^ ]
虽然你想要做的事似乎有点矫枉过正,因为你可以写一个谓词来进行数组排序。像这样:
http://www.csharp-examples.net/sort-array/ [ ^ ]
You could proberbly find the algorithm here:
Visualization and comparison of sorting algorithms in C#[^]
Although what you are trying to do seems to be a bit overkill, as you could write a predicate to go with array sorting. Like this:
http://www.csharp-examples.net/sort-array/[^]
你可以在c#中使用build in function。
请看下面的例子
You can used build in function present in the c#.
Please see the below example
using System;
class Program
{
static void Main()
{
// Simple sort call.
int[] values = {1,5,3,0,3,5,7 };
Array.Sort(values);
foreach (int value in values)
{
Console.Write(value);
Console.Write(' ');
}
Console.WriteLine();
}
}
这篇关于为数组排序算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文