你是搞研究还是做项目?如果是搞研究,不可能。如果是做项目,这样最方便了:
int[] temps = { 10, 22, 4, 50, 100, 65, 56, 98, 105, 200, 900, 1 };
List<int> l = new List<int>(temps);
l.Sort();
temps = l.ToArray();
int[] temps = { 10, 22, 4, 50, 100, 65, 56, 98, 105, 200, 900, 1 };
List<int> l = new List<int>(temps);
l.Sort();
temps = l.ToArray();
int[] temps = { 10, 22, 4, 50, 100, 65, 56, 98, 105, 200, 900, 1 };
for (int i = 1; i < temps.Length; i++)
{
int a = temps[i-1];
if (temps[i] < temps[i - 1])
{
temps[i - 1] = temps[i];
temps[i] = a;
i = 0;
}
}
其實與兩層迴圈意思是一樣的,只是用一層來重覆比較。可以嗎?
如果LZ是要找排序算法我感觉快速排序算法不算递归看上去符合LZ的描述点
这里的一次循环并不是说时间复杂度就是O(n),冒泡排序的时间复杂度肯定是O(n2)的。
这道题考的只是一个编程技巧而已。
一直想写篇博客来着,不过没时间写。 //two loops
static void Sort(int[] array)
{
int length = array.Length;
for (int i = 0; i < length - 1; i++)
{
for (int j = 0; j < length - 1 - i; j++)
{
if (array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
} //one loop
static void SortOneLoop(int[] array)
{
int length = array.Length;
int total = length * length - 1; while (total / length > 0)
{
if (total % length >= length - total / length && array[total % length] < array[total % length - 1])
{
int temp = array[total % length];
array[total % length] = array[total % length - 1];
array[total % length - 1] = temp;
} total--;
}
}
博客也写了一下,可以去看看。
int[] result = new int[1000];
for (int i = 0; i < temps.Length; i++)
{
result[temps[i]] = temps[i];
}
for (int i = 0; i < result.Length; i++)
{
if (result[i]!=0) Console.WriteLine(result[i]);
}
( ⊙ o ⊙ )
//var query =
// from vall in strAry
// orderby vall descending
// select vall;
//foreach(int s in query)
//{
// Response.Write(s.ToString() + "-");
//}Linq 也 可以. . .....
不要一味拘泥于“实用性”。如果只知道3楼的写法,充其量只是熟悉api的用法而已。11楼的方法看似无用,却能拓展思维。长远来说,这更重要
for (int j = 0; j < 10; j++)
Console.WriteLine("i = {0}, j = {0}", i, j);
=>
for (int i = 0, j = 0; i < 10; i += (((j = (++j % 10)) == 0) ? 1 : 0))
Console.WriteLine("i = {0}, j = {0}", i, j);
int bubble_sort(char * array, int n)
{
int i=0;
char temp;
int count = n; while(count--)
{
if(*(array+i) > *(array+i+1))
{
temp = *(array+i);
*(array+i) = *(array+i+1);
*(array+i+1) = temp;
} i++; if(i == n-1)
{
count = --n;
i = 0;
}
}
}