Bubble Sort (排序详解 之 冒泡排序)
2013-08-12 18:36
337 查看
Bubble Sort
This article,we will go through one easier sort algorithm --bubble sort .Let's Startwith :
5 , 1, 9 ,3, 7 , 4 , 8 ,6 , 2
point :
1.from 1-n ,each time take 2 numbers ,number[k] and number[k+1]
2.comparethem ,if number[k]
<(or > , depends on ascendingor descending) number[k+1] then swap them
now thissample is descending sort one array usingbubble sort
.
Step 1,takefirst 2 numbers :
Step 2: Take1,9
Step 3: Take1,3
Repeat doingthe same thing ......
At last ,1 willbe the smallest one in thelast position .
so ,now gotit ? if we doing the same steps for 2nd round, we will get:
It is ,2 will be the 2nd one from the end just before 1.
so keep dong,next is 3:
Next is 4:
…… doing the same thing for n times .
finally , wewill get :
Hope now you are quite clear with bubble sort . :)
ReferenceCode :
public List<int> BubbleSort(List<int> arr)
{
var sortedArr = newList<int>();
arr.ForEach(sortedArr.Add);
if (arr.Count < 2) return arr;
for (var i = sortedArr.Count; i> 0; i--)
{
for (var j = 1; j < i; j++)
{
if (sortedArr[j - 1] >sortedArr[j])
{
int tmp = sortedArr[j -1];
sortedArr[j - 1] =sortedArr[j];
sortedArr[j] = tmp;
}
}
}
return sortedArr;
}
Finished .
Thanks forreading . :)
相关文章推荐
- 数据结构 - 冒泡排序(Bubble Sort) 详解 及 代码(C++)
- 冒泡排序和希尔排序案列详解(要求输入10个整数,输出排序结果)
- 排序——冒泡排序(Bubble Sort)
- 算法:冒泡排序(Bubble Sort)、插入排序(Insertion Sort)和选择排序(Selection Sort)总结
- 蛮力算法: 选择排序 冒泡排序(详解)
- 【算法】排序之冒泡排序详解
- 冒泡排序(bubble sort)、直接插入排序(straight insertion sort)、简单选择排序(simple selection sort)
- 新手必看——JAVA排序详解(冒泡排序和插入排序)
- 【排序】插入排序,希尔排序,选择排序,冒泡排序,堆排序详解及稳定性分析
- 冒泡排序 与选择排序的逻辑实现 详解-----------博
- 【排序算法】冒泡排序 bubble sort(交换类排序)
- 排序之冒泡排序(Bubble Sort)
- 交换排序之冒泡排序 bubble sort
- 笔试面试最常涉及到的12种排序算法(包括插入排序、二分插入排序、希尔排序、选择排序、冒泡排序、鸡尾酒排序、快速排序、堆排序、归并排序、桶排序、计数排序和基数排序)进行了详解。每一种算法都有基本介绍、算
- Java原来如此-几种常见的排序--冒泡排序(Bubble Sort)
- 【DS】排序算法之冒泡排序(Bubble Sort)
- 冒泡排序 Bubble Sort 和 选择排序 Selection Sort
- 排序---交换排序---冒泡排序 Bubble Sort
- PHP排序算法之冒泡排序(Bubble Sort)实现方法详解
- 详解冒泡排序和对其优化的Shaker 排序