1.冒泡排序
package com.sort; public class BubbleSort { public void BubbleSort(int[] a){ for (int i =0; i < a.length - 1;i++){ for (int j = 0; j < a.length-1-i ;j++){ if (a[j] > a[j+1]){ // swap(a[j],a[j+1]); int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } } private void swap(int x, int y) { int temp = x; x = y; y = temp; } public static void println(int[] a){ System.out.print("["); for (int i = 0; i < a.length;i++){ System.out.print(a[i]); } System.out.print("]" + "\n"); } public static void main(String[] args){ int[] a ={2,3,1,5,6,7,4,3,7,}; BubbleSort bs = new BubbleSort(); System.out.println("排序前的数组是:"); println(a); bs.BubbleSort(a); System.out.println("排序后的数组是:"); println(a); } }
2. 选择排序
package com.sort; public class SelectSort { public void SelectSort(int [] array){ for(int i = 0; i < array.length-1;i++){ for(int j = i+1; j< array.length;j++){ if(array[i]>array[j]){ // swap(array[i],array[j]); int temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } private void swap(int i, int j) { int temp = i; i = j; j = temp; } public static void println(int[] a){ System.out.print("["); for (int i = 0; i < a.length;i++){ System.out.print(a[i]); } System.out.print("]" + "\n"); } public static void main(String[] args) { int[] a = {2, 3, 1, 5, 6, 7, 4, 3, 7,}; SelectSort ss = new SelectSort(); System.out.println("排序前的数组是:"); println(a); ss.SelectSort(a); System.out.println("排序后的数组是:"); println(a); } }3.插入排序
package com.sort; public class InsertSort { public void InsertSort(int [] arr){ for(int i= 1; i< arr.length;i++){ if(arr[i]<arr[i-1]){ int temp = arr[i]; int k = i-1; for(int j = k;j>=0 && temp < arr[j];j--){ arr[j+1]= arr[j]; k--; } arr[k+1]=temp; } } } public static void println(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(+array[i]); if (i != array.length - 1) { System.out.print(","); } } System.out.print("\n"); } public static void main(String[] args) { int[] a = {2, 3, 1, 5, 6, 7, 4, 3, 7,}; InsertSort ss = new InsertSort(); System.out.println("排序前的数组是:"); println(a); ss.InsertSort(a); System.out.println("排序后的数组是:"); println(a); } }4.归并排序
package com.sort; public class MergeSort { // private static long sum = 0; /** * * <pre> * * 二路归并 * * 原理:将两个有序表合并和一个有序表 * * </pre> * * * * @param a * * @param s * * 第一个有序表的起始下标 * * @param m * * 第二个有序表的起始下标 * * @param t * * 第二个有序表的结束小标 * * */ private static void merge(int[] a, int s, int m, int t) { int[] tmp = new int[t - s + 1]; int i = s, j = m, k = 0; while (i < m && j <= t) { if (a[i] <= a[j]) { tmp[k] = a[i]; k++; i++; } else { tmp[k] = a[j]; j++; k++; } } while (i < m) { tmp[k] = a[i]; i++; k++; } while (j <= t) { tmp[k] = a[j]; j++; k++; } System.arraycopy(tmp, 0, a, s, tmp.length); } /** * * * * @param a * * @param s * * @param len * * 每次归并的有序集合的长度 */ public static void mergeSort(int[] a, int s, int len) { int size = a.length; int mid = size / (len << 1); int c = size & ((len << 1) - 1); // -------归并到只剩一个有序集合的时候结束算法-------// if (mid == 0) return; // ------进行一趟归并排序-------// for (int i = 0; i < mid; ++i) { s = i * 2 * len; merge(a, s, s + len, (len << 1) + s - 1); } // -------将剩下的数和倒数一个有序集合归并-------// if (c != 0) merge(a, size - c - 2 * len, size - c, size - 1); // -------递归执行下一趟归并排序------// mergeSort(a, 0, 2 * len); } public static void main(String[] args) { int[] a = new int[]{4, 3, 6, 1, 2, 5}; mergeSort(a, 0, 1); for (int i = 0; i < a.length; ++i) { System.out.print(a[i] + " "); } } }5.快速排序
package com.sort; public class QuickSort { public int part(int [ ] a, int low, int high){ int key = a[low]; while(low < high){ while(low< high && a[high]>key ) high--; a[low]=a[high]; while(low< high && a[low]>key ) low++; a[high]=a[low]; } a[low]=key; return low; } public void QuickSort(int[] a,int low, int high){ if (low<high){ int index = part(a, low, high); QuickSort(a,low,index-1); QuickSort(a,index+1,high); } } public static void println(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(+array[i]); if (i != array.length - 1) { System.out.print(","); } } System.out.print("\n"); } public static void main(String[] args) { int[] a = {2, 3, 1, 5, 6, 7, 4, 3, 7,}; QuickSort ss = new QuickSort(); System.out.println("排序前的数组是:"); println(a); ss.QuickSort(a,0, a.length - 1); System.out.println("排序后的数组是:"); println(a); } }6.希尔排序
package com.sort; import java.util.Arrays; public class ShellSort{ public static void ShellSort(int[] data) { int j = 0; int temp = 0; for (int increment = data.length / 2; increment > 0; increment /= 2) { System.out.println("increment(步长):" + increment); for (int i = increment; i < data.length; i++) { // System.out.println("i:" + i); temp = data[i]; for (j = i - increment; j >= 0; j -= increment) { // System.out.println("j:" + j); // System.out.println("temp:" + temp); // System.out.println("data[" + j + "]:" + data[j]); if (temp < data[j]) { data[j + increment] = data[j]; } else { break; } } data[j + increment] = temp; } for (int i = 0; i < data.length; i++) System.out.print(data[i] + " "); } } public static void main(String[] args) { int[] data = new int[] { 26, 53, 67, 48, 57, 13, 48, 32, 60, 50 }; ShellSort(data); System.out.println(Arrays.toString(data)); } }7.基数排序
package com.sort; public class RadixSort { public static void sort(int[] number, int d) //d表示最大的数有多少位 { int k = 0; int n = 1; int m = 1; //控制键值排序依据在哪一位 int[][]temp = new int[10][number.length]; //数组的第一维表示可能的余数0-9 int[]order = new int[10]; //数组orderp[i]用来表示该位是i的数的个数 while(m <= d) { for(int i = 0; i < number.length; i++) { int lsd = ((number[i] / n) % 10); temp[lsd][order[lsd]] = number[i]; order[lsd]++; } for(int i = 0; i < 10; i++) { if(order[i] != 0) for(int j = 0; j < order[i]; j++) { number[k] = temp[i][j]; k++; } order[i] = 0; } n *= 10; k = 0; m++; } } public static void main(String[] args) { int[]data = {73, 22, 93, 43, 55, 14, 28, 65, 39, 81, 33, 100}; RadixSort.sort(data, 3); for(int i = 0; i < data.length; i++) { System.out.print(data[i] + "\n"); } } }8.堆排序
package com.sort; import java.util.Arrays; public class HeapSort { /** * 构建大根堆 */ public static void adjustHeap(int[] a, int i, int len) { int temp, j; temp = a[i]; for (j = 2 * i; j < len; j *= 2) {// 沿关键字较大的孩子结点向下筛选 if (j < len && a[j] < a[j + 1]) ++j; // j为关键字中较大记录的下标 if (temp >= a[j]) break; a[i] = a[j]; i = j; } a[i] = temp; } public static void heapSort(int[] a) { int i; for (i = a.length / 2 - 1; i >= 0; i--) {// 构建一个大顶堆 adjustHeap(a, i, a.length - 1); } for (i = a.length - 1; i >= 0; i--) {// 将堆顶记录和当前未经排序子序列的最后一个记录交换 int temp = a[0]; a[0] = a[i]; a[i] = temp; adjustHeap(a, 0, i - 1);// 将a中前i-1个记录重新调整为大顶堆 } } public static void main(String[] args) { int a[] = { 51, 46, 20, 18, 65, 97, 82, 30, 77, 50 }; heapSort(a); System.out.println(Arrays.toString(a)); } }
以上代码亲测成功。。。