`

排序算法

阅读更多

冒泡  选择 插入 希尔 快速 归并 堆排序

参考
----------------------------------------------------
八大排序算法总结
http://blog.csdn.net/yexinghai/archive/2009/10/10/4649923.aspx

基本排序算法比较与选择
http://blogger.org.cn/blog/more.asp?name=njucs&id=3950

起泡排序-也叫冒泡排序
http://roclinux.cn/?p=661

快速排序-最重要的排序算法
http://roclinux.cn/?p=566

直接插入排序-最简单的排序算法
http://roclinux.cn/?p=579

堆排序-来自锦标赛排序的灵感
http://roclinux.cn/?p=675

简单选择排序-最简单的选择排序
http://roclinux.cn/?p=671


快速排序:

package test;
import java.util.Random;

public class A {
	public static void main(String[] args) throws Exception {

		Random random = new Random();
		int len = random.nextInt(1000);
		System.out.println("长度:" + len);
		int[] arr3 = new int[len];
		System.out.println("原始的数组:");
		for (int i = 0; i < len; i++) {
			arr3[i] = random.nextInt(10000);
			System.out.print(arr3[i]);
			System.out.print(",");
		}
		System.out.println("");
		long t3 = System.currentTimeMillis();
		arr3 = QuickSort2(arr3, 0, arr3.length - 1);
		long t4 = System.currentTimeMillis();
		System.out.println("用时毫秒:" + (t4 - t3));
		for (int a : arr3) {
			System.out.print(a);
			System.out.print(",");
		}
	}

	private static int[] QuickSort2(int[] arr, int start, int end) {
		if (start < end) {
			int i = start - 1;
			int j = end + 1;
			int key = arr[start];
			int temp;
			while (i != j && i + 1 != j) {
				if (key < arr[j - 1]) {
					j--;
				} else if (key > arr[i + 1]) {
					i++;
				} else {
					temp = arr[i + 1];
					arr[i + 1] = arr[j - 1];
					arr[j - 1] = temp;
					j--;
					i++;
				}
			}
			QuickSort2(arr, start, i);
			QuickSort2(arr, i + 1, end);
		}
		return arr;
	}
}





冒泡排序

final List<NsUploadVO> listvo = new ArrayList<NsUploadVO>();
listvo .add(....略.....);
listvo .add(....略.....);
listvo .add(....略.....);

for (int k = 0; k < listvo.size(); k++){
	for (int j = listvo.size() - 1; j > k; j--){
		final NsUploadVO vo_j = listvo.get(j);
		final NsUploadVO vo_k = listvo.get(j - 1);
		if (vo_j.getId() < vo_k.getId()){
			final NsUploadVO temp = vo_k;
			listvo.remove(j - 1);
			listvo.add(j - 1, vo_j);
			listvo.remove(j);
			listvo.add(j, temp);
		}
	}
}

public class NsUploadVO{
	private int		id;
	private int		code;
	private String	title;
	private String	fileName;
	private String	info;
	private String	bot_info;
	private String	content;	//正文
	set get方法略...
}



 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics