冒泡排序
Posted 孔琳桂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了冒泡排序相关的知识,希望对你有一定的参考价值。
- 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;
- }
- }
- }
以上是关于冒泡排序的主要内容,如果未能解决你的问题,请参考以下文章