标签:
问题描述:
BubbleSort
InsertionSort
ShellSort
MergeSort
HeapSort
QuickSort
问题分析:
时间复杂度?
空间复杂度?
代码实现:
public class BubbleSort { public static <AnyType extends Comparable<? super AnyType>> void bubblesort(AnyType[] a){ boolean done = false; int size = a.length; while(!done){ done = true; for(int i = 0; i < size - 1; i++){ if(a[i].compareTo(a[i+1]) > 0){ done = false; AnyType tmp = a[i+1]; a[i+1] = a[i]; a[i] = tmp; } } size--; } } }
class InsertionSort { public static <AnyType extends Comparable<? super AnyType>> void insertionSort(AnyType[] a) { for (int p = 1; p < a.length; p++) { AnyType tmp = a[p]; //需找合适的插入位置 int j; for (j = p; j > 0 && tmp.compareTo(a[j-1]) < 0; j--) a[j] = a[j - 1]; a[j] = tmp; } } }
public class ShellSort { public static <AnyType extends Comparable<? super AnyType>> void shellsort(AnyType[] a) { for(int gap = a.length/2; gap > 0; gap /= 2) for(int i = gap; i < a.length; i++){ AnyType tmp = a[i]; int j = i; for(; j >= gap && tmp.compareTo(a[j-gap]) < 0; j -= gap) a[j] = a[j - gap]; a[j] = tmp; } } }
public class MergeSort { public static <AnyType extends Comparable<? super AnyType>> void mergesort(AnyType[] a){ AnyType[] tmp = (AnyType[])new Comparable[a.length]; mergesort(a, tmp, 0, a.length - 1); } public static <AnyType extends Comparable<? super AnyType>> void mergesort(AnyType[] a, AnyType[] tmp, int left, int right){ if(left < right){ int center = (left+right)/2; mergesort(a, tmp, left, center); mergesort(a, tmp, center + 1,right); merge(a, tmp, left, center + 1, right); } } public static <AnyType extends Comparable<? super AnyType>> void merge(AnyType[] a, AnyType[] tmp, int leftPos, int rightPos, int rightEnd){ int leftEnd = rightPos - 1; int tmpPos = leftPos; int numElements = rightEnd - leftPos + 1; while(leftPos <= leftEnd && rightPos <= rightEnd){ if(a[leftPos].compareTo(a[rightPos]) < 0) tmp[tmpPos++] = a[leftPos++]; else tmp[tmpPos++] = a[rightPos++]; } while(leftPos <= leftEnd) tmp[tmpPos++] = a[leftPos++]; while(rightPos <= rightEnd) tmp[tmpPos++] = a[rightPos++]; for(int i = 0; i < numElements; i++, rightEnd--) a[rightEnd] = tmp[rightEnd]; } }
public class HeapSort { static int leftChild(int i){ return 2*i + 1; } public static <AnyType extends Comparable<? super AnyType>> void heapsort(AnyType[] a){ for(int i = a.length/2; i >= 0; i--) percDown(a, i, a.length); for(int j = a.length - 1; j > 0; j--){ AnyType tmp = a[0]; a[0] = a[j]; a[j] = tmp; percDown(a, 0, j); } } public static <AnyType extends Comparable<? super AnyType>> void percDown(AnyType[] a, int i, int n){ int child; AnyType tmp; for(tmp = a[i]; leftChild(i) < n; i = child){ child = leftChild(i); if(child != n -1 && a[child].compareTo(a[child + 1]) < 0) child++; if(tmp.compareTo(a[child]) < 0) a[i] = a[child]; else break; } a[i] = tmp; } }
public class QuickSort { public static <AnyType extends Comparable<? super AnyType>> void quicksort(AnyType[] a){ quicksort(a, 0, a.length - 1); } private static <AnyType extends Comparable<? super AnyType>> AnyType median3(AnyType[]a , int left, int right){ int center = (left + right)/2; if(a[center].compareTo(a[left]) < 0){ AnyType tmp = a[center]; a[left] = a[center]; a[center] = tmp; } if(a[right].compareTo(a[left]) < 0){ AnyType tmp = a[right]; a[right] = a[left]; a[left] = tmp; } if(a[right].compareTo(a[center]) < 0){ AnyType tmp = a[right]; a[right] = a[center]; a[center] = tmp; } AnyType temp = a[center]; a[center] = a[right - 1]; a[right - 1] = temp; return a[right - 1]; } static <AnyType extends Comparable<? super AnyType>> void quicksort(AnyType[] a, int left, int right){ if(left + 10 < right){ AnyType pivot = median3(a, left, right); int i = left, j = right - 1; for(;;){ while(a[++i].compareTo(pivot) < 0 ){} while(pivot.compareTo(a[--j]) < 0){} if(i < j){ AnyType tmp = a[i]; a[i] = a[j]; a[j] = tmp; } else break; } AnyType tmp = a[i]; a[i] = a[right - 1]; a[right - 1] = tmp; quicksort(a, left, i - 1); quicksort(a, i + 1, right); } else insertionSort(a); } }
public class SortTest { public static void main(String[] args) { Integer[] v = {9,3,4,5,1,6,8,2,12,14,22,18,3,11}; InsertionSort.insertionSort(v); //BubbleSort.bubblesort(v); //ShellSort.shellsort(v); //HeapSort.heapsort(v); //MergeSort.mergesort(v); //QuickSort.quicksort(v); for (int i = 0; i < v.length; i++) { Integer integer = v[i]; System.out.print(integer + " "); } } }
标签:
原文地址:http://my.oschina.net/jimmyhan/blog/521870