.net strengthen the foundation

1. Bubble Sort

Please array of integers {1, 3, 5, 7, 90, 2, 4, 6, 8, 10} achieved by a bubble sort in ascending order

int[] num = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
BubbleSort(num);
Console.ReadLine();

 

private static void BubbleSort(int[] num)
{
for (int i = 0; i < num.Length - 1; i++)
{
for (int j = num.Length - 1; j > i; j--)
{
if(num[j] < num[j - 1])
{
int tmp = num[j];
num[j] = num[j - 1];
num[j - 1] = tmp;
}
}
}
for (int n = 0; n < num.Length; n++)
{
Console.WriteLine(num[n]);
}
}

 

Guess you like

Origin www.cnblogs.com/wlj-blog/p/11440486.html