首页 > 代码库 > 冒泡排序
冒泡排序
- 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;
- }
- }
- }
冒泡排序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。