elvalad

修仙
排序

计算机领域最基础的操作:排序 http://en.wikipedia.org/wiki/Sorting_algorithm。

  • 冒泡排序:http://en.wikipedia.org/wiki/Bubble_sort
  • 选择排序:http://en.wikipedia.org/wiki/Selection_sort
  • 插入排序:http://en.wikipedia.org/wiki/Insertion_sort
  • 希尔排序:http://en.wikipedia.org/wiki/Shellsort
  • 归并排序:http://en.wikipedia.org/wiki/Merge_sort
  • 快速排序:http://en.wikipedia.org/wiki/Quicksort
import java.util.Scanner;

public class Sort {
	public static void bubbleSort(Comparable[] a) {
		for (int i = 0; i < a.length; i++) {
			for (int j = i + 1; j < a.length; j++) {
				if (less(a[j], a[i])) {
					exch(a, i, j);
				}
			}
		}
	}
	
	public static void selectSort(Comparable[] a) {
		for (int i = 0; i < a.length; i++) {
			int min = i;
			for (int j = i + 1; j < a.length; j++) {
				if (less(a[j], a[min])) {
					min = j;
				}
			}
			exch(a, i, min);
		}
	}
	
	public static void insertionSort(Comparable[] a) {
		for (int i = 1; i < a.length; i++) {
			for (int j = i; (j > 0) && (less(a[j], a[j - 1])); j--) {
				exch(a, j, j - 1);
			}
		}
	}
	
	public static void insertionSort2(Comparable[] a) {
		Comparable key;
		for (int i = 1; i < a.length; i++) {
			int j = i - 1;
			key = a[i];
			while((j > 0) && less(key, a[j])) {
				a[j + 1] = a[j];
				j--;
			}
			a[j + 1] = key;
		}
	}
	
	public static void shellSort(Comparable[] a) {
		int h = 1;
		while (h < a.length/3) {
			h = 3*h + 1;
		}
		
		while (h >= 1) {
			for (int i = h; i < a.length; i++) {
				for (int j = i; (j >= h) && (less(a[j], a[j - h])); j -= h) {
					exch(a, j, j - h);
				}
			}
			h = h/3;
		}
	}
	
	private static void merge(Comparable[] a, int lo, int mid, int hi) {
		Comparable[] aux = new Comparable[a.length];
		for (int k = lo; k <= hi; k++) {
			aux[k] = a[k];
		}
		int i = lo, j = mid + 1;
		for (int k = lo; k <= hi; k++) {
			 if (i > mid) {
				a[k] = aux[j++];
			} else if (j > hi) {
				a[k] = aux[i++];
			} else if (less(aux[j], aux[i])) {
				a[k] = aux[j++];
			} else {
				a[k] = aux[i++];
			}
		}
	}
	
	private static void mergesort(Comparable[] a, int lo, int hi) {
		if (hi <= lo) {
			return;
		}
		int mid = lo + (hi - lo)/2;
		mergesort(a, lo, mid);
		mergesort(a, mid + 1, hi);
		merge(a, lo, mid, hi);
	} 
	
	public static void mergeSort(Comparable[] a) {
		mergesort(a, 0, a.length - 1);
	}
	
	private static void quicksort(Comparable[] a, int lo, int hi) {
		if (hi <= lo)
			return;
		int j = partition(a, lo, hi);
		quicksort(a, lo, j - 1);
		quicksort(a, j + 1, hi);
	}
	
	private static int partition(Comparable[] a, int lo, int hi) {
		int i = lo, j = hi + 1;
		Comparable v= a[lo];
		while (true) {
			while (less(a[++i], v)) {
				if (i == hi)
					break;
			}
			while (less(v, a[--j])) {
				if (j == lo)
					break;
			}
			if (i >= j)
				break;
			exch(a, i, j);
		}
		exch(a, lo, j);
		return j;
	}
	
	public static void quickSort(Comparable[] a) {
		quicksort(a, 0, a.length - 1);
	}
	
	private static boolean less(Comparable m, Comparable n) {
		return m.compareTo(n) < 0; 
	}
	
	private static void exch(Comparable[] a, int i, int j) {
		Comparable tmp = a[i];
		a[i] = a[j];
		a[j] = tmp;
	}
	
	private static void show(Comparable[] a) {
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i] + " ");
		}
		System.out.println();
	}
	
	public static boolean isSorted(Comparable[] a) {
		for (int i = 1; i < a.length; i++) {
			if (less(a[i], a[i - 1])) {
				return false;
			}
		}
		return true;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Integer[] a = {1,4,3,2,66,333,56,3,112,33333,43};	
		quickSort(a);		
		assert isSorted(a);
		show(a);
	}
}

  

posted on 2014-11-05 22:21  elvalad  阅读(109)  评论(0编辑  收藏  举报