冒泡排序

  • int[] arr = {1,3,6,4,2};
/*
count = 5
i = 0
j = 0
{1,3,6,4,2}
j = 1
{1,3,6,4,2}
j = 2
{1,3,4,6,2}
j = 3
{1,3,4,2,6}
 
i = 1
j = 0
{1,3,4,2,6}
j = 1
{1,3,4,2,6}
j = 2
{1,3,2,4,6}
 
i = 2
j = 0
{1,3,2,4,6}
j = 1
{1,2,3,4,6}
 
i = 3
j = 0
{1,2,3,4,6}
 
结束 !!!
*/
 
  • int count = arr.Length;
  • //冒泡
  • for (int i = 0; i < count - 1; i++)
  • {
  • for (int j = 0; j < count - 1 - i; j++)
  • {
  • if (arr[j] > arr[j + 1])
  • {
  • //交换
  • int temp = arr[j + 1];
  • arr[j + 1] = arr[j];
  • arr[j] = temp;
  • }
  • }
  • }
原文地址:https://www.cnblogs.com/C-9925/p/7357065.html