public class BubbleSort {
public static void bubbleSort(int[] a) {
for (int j = 1; j < a.length; j++) {
for (int i = 0; i < a.length - j; i++) {
if (a[i] > a[i + 1]) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7,
3, 8, 31 };
bubbleSort(a);
for(int i = 0; i < a.length; i++){
System.out.print(a[i]+" ");
}
}
}
public class SelectSort {
public static void selectSort(int[] a) {
int min;
for (int i = 0; i < a.length - 1; i++) {
min = i;
for (int j = min + 1; j < a.length; j++) {
if (a[min] > a[j]) {
int temp = a[min];
a[min] = a[j];
a[j] = temp;
}
}
}
}
public static void main(String[] args) {
int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7,
2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 };
selectSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
public class InsertSort {
public static void insertSort(int[] a) {
for (int i = 1; i < a.length; i++) {
int key = a[i];
int j = i - 1;
while (j >= 0 && a[j] > key) {
a[j+1] = a[j];
j--;
}
a[j+1] = key;
}
}
public static void main(String[] args) {
int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7,
2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 };
insertSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
package sort;
public class BinarySort {
public static int binarySerch(int[] arr, int start, int end, int value) {
int mid = -1;
while (start <= end) {
mid = (start + end) / 2;
if (arr[mid] < value)
start = mid + 1;
else if (arr[mid] > value)
end = mid - 1;
else
break;
}
if (arr[mid] < value)
return mid + 1;
else if (value < arr[mid])
return mid;
return mid + 1;
}
public static void binarySort(int[] arr, int start, int end) {
for (int i = start + 1; i <= end; i++) {
int value = arr[i];
int insertLoc = binarySerch(arr, start, i - 1, value) ;
for (int j = i; j > insertLoc; j--) {
arr[j] = arr[j - 1];
}
arr[insertLoc] = value;
}
}
public static void main(String[] args) {
int[] arr = { 3, 5, 0, 8, 7, 9, 1, 2, 6, 4 };
binarySort(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
public class ShellSort {
public static void shellSort(int[] a) {
for (int gap = a.length / 2; gap > 0; gap /= 2)
for (int i = gap; i < a.length; i++) {
int key = a[i];
int j = i - gap;
while (j >= 0 && a[j] > key) {
a[j + gap] = a[j];
j -= gap;
}
a[j + gap] = key;
}
}
public static void main(String[] args) {
int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7,
2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 };
shellSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
public class QuickSort {
/*public static int partition(int[] a, int p, int r) {
int x = a[r];
int i = p - 1;
for (int j = p; j < r; j++) {
if (a[j] <= x) {//如果a[j]= x && i
public class MergeSort {
public static void merge(int A[], int p, int q, int r) {
int[] L = new int[q - p + 1];
int[] R = new int[r - q];
for (int i = p, j = 0; i <= q; i++, j++) {
L[j] = A[i];
}
for (int i = q + 1, j = 0; i <= r; i++, j++) {
R[j] = A[i];
}
int pos = p;
int i = 0, j = 0;
for (; i < L.length && j < R.length;) {
if (L[i] <= R[j]) {
A[pos++] = L[i++];
} else {
A[pos++] = R[j++];
}
}
if (i < L.length) {
for (; i < L.length;)
A[pos++] = L[i++];
} else if (j < R.length) {
for (; j < R.length;)
A[pos++] = R[j++];
}
}
public static void mergeSort(int[] A, int p, int r) {
if (p < r) {
int q = (p + r) / 2;
mergeSort(A, p, q);
mergeSort(A, q + 1, r);
merge(A, p, q, r);
}
}
public static void main(String[] args) {
int A[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7,
3, 8, 31 };
mergeSort(A, 0, A.length - 1);
for (int i = 0; i < A.length; i++) {
System.out.print(A[i] + " ");
}
}
}
public class HeapSort {
//求双亲位置
static int parent(int i) {
return i / 2;
}
//求左孩子位置
static int left(int i) {
return 2 * i;
}
//求右孩子位置
static int right(int i) {
return 2 * i + 1;
}
//维持大顶堆的性质
static void maxHelpify(int[] A, int i) {
int l = left(i);
int r = right(i);
int largest = 0;
if (l <= A[0] && A[l] > A[i])
largest = l;
else
largest = i;
if (r <= A[0] && A[r] > A[largest])
largest = r;
if (largest != i) {
int temp = A[largest];
A[largest] = A[i];
A[i] = temp;
maxHelpify(A, largest);
}
}
//建立大顶堆
static void buildMaxHeap(int[] A){
for(int i=(A.length-1)/2; i>0; i--){
maxHelpify(A, i);
}
}
//堆排序
public static void heapSort(int[] A){
buildMaxHeap(A);//建立大顶堆
//每次把堆顶的数放到数组最后,然后把堆的大小减1,再次维持第一个数的大顶堆性质
for(int i = A.length - 1; i>=2; i--){
int temp = A[1];
A[1] = A[i];
A[i] = temp;
A[0]--;
maxHelpify(A, 1);
}
}
public static void main(String[] args) {
int A[] = new int[3];
A[0] = A.length-1;//a[0]存放堆的大小
for(int i = 1; i < A.length; i++){
A[i] = (int) (Math.random()*10);
}
heapSort(A);
for (int i = 1; i < A.length; i++) {
System.out.print(A[i] + " ");
}
}
}
比较简单:就是将数组下标作为元素的value,特殊情况下使用。如排序N个人的年龄就可以用计数排序。将年龄看作数组的下标,定义一个大小为100的数组,将年龄与之比较,如果年龄==下标就将,该下标的value+1.时间复杂度为(N)。