? 插入排序(Insert Sort), java版.
简单选择排序(Select Sort),java版。 ?
冒泡排序(Bubble Sort),java版.
版权信息: 可以任意转载, 转载时请务必以超链接形式标明文章原文出处, 即下面的声明.
原文出处:http://blog.chenlb.com/2008/12/bubble-sort-for-java.html
学习的第一个排序方式就是冒泡排序,在是学c语言时候学的。
冒泡排序原理:把最大的(或最小的)冒出来。从底端(即index=0)向上面紧挨着的比较,大的(或小的)冒上来(交换),直到冒到“顶”(顶的解释:没有冒过的,即是冒一趟,顶就矮一层。)。然后继续下一趟冒,直到底端不是“顶”。
时间复杂度:平均O(n2),最坏情况O(n2)。
示例代码:
- package com.chenlb.sort;
- import java.util.Arrays;
- public class BubbleSort {
- public static int[] sort(int[] datas) {
- for(int i=1; i<datas.length; i++) {
- for(int j=0; j<datas.length-i; j++) {
- if(datas[j] > datas[j+1]) {
- SortUtil.swap(datas, j, j+1);
- }
- }
- }
- return datas;
- }
- public static void main(String[] args) {
- int[] datas = { 5,1,3,4,9,2,7,6,5};
- sort(datas);
- System.out.println(Arrays.toString(datas));
- datas = SortUtil.randomDates(10);
- sort(datas);
- System.out.println(Arrays.toString(datas));
- }
- }
运行结果:
- [1, 2, 3, 4, 5, 5, 6, 7, 9]
- [68, 86, 143, 175, 242, 281, 332, 481, 603, 861]
随机日志 ?
- solr sint 字段 hashcode 冲突高达99%,导致 solr-memcached 的 bug
- ictclas4j for lucene analyzer
- 为 httpclient 设置代理, 设置http头.
- 金士顿 Kingston 16G 假冒 U盘
- 关于 Jms Topic 持久订阅
相关日志 ?