几种排序算法java版

来源:互联网 发布:战舰少女r 胡德 知乎 编辑:程序博客网 时间:2024/06/06 05:07


//冒泡排序public class Bubble {public static void bubbleSort(int[] array) {boolean flag = true;for (int i = 0; i < array.length - 1; i++) {if (!flag)break;flag = false;for (int j = 0; j < array.length - 1 - i; j++) {// 把最大的往后移动if (array[j] > array[j + 1]) {int tmp = array[j];array[j] = array[j + 1];array[j + 1] = tmp;flag = true;}}}}} //插入排序public class Insert {public static void insertSort(int[] array) {for (int i = 1; i < array.length; i++) {int insert = array[i];int j = 0;for (j = i; j > 0; j--) {// [0]-[i-1]个是已经排好的if (insert < array[j - 1]) {array[j] = array[j - 1];} else {break;}}array[j] = insert;}}} 选择排序public class Select {public static void selectSort(int[] array) {for (int i = 0; i < array.length - 1; i++) {int minKey = i;for (int j = i + 1; j < array.length; j++) {if (array[minKey] > array[j])minKey = j;}int tmp = array[i];array[i] = array[minKey];array[minKey] = tmp;}}} //希尔排序public class Shell {public static void shellSort(int[] array) {for (int inc = array.length / 2; inc > 0; inc /= 2) {for (int i = inc; i < array.length; i++) {int insert = array[i];int j = 0;for (j = i; j > inc - 1; j -= inc) {if (insert < array[j - inc]) {array[j] = array[j - inc];} elsebreak;}array[j] = insert;}}}} //归并排序public class Merge {public static void mergeSort(int[] array) {int length = array.length;int[] tmp = new int[length];Msort(array, tmp, 0, length - 1);}public static void Msort(int[] array, int[] tmp, int left, int right) {if (left < right) {int mid = (left + right) / 2;Msort(array, tmp, left, mid);Msort(array, tmp, mid + 1, right);merge(array, tmp, left, mid + 1, right);}}public static void merge(int[] array, int[] tmp, int lPos, int rPos, int rightEnd) {int leftEnd = rPos - 1;int tmpPos = lPos;int begin = lPos;while (lPos <= leftEnd && rPos <= rightEnd) {if (array[lPos] <= array[rPos]) {tmp[tmpPos++] = array[lPos++];} else {tmp[tmpPos++] = array[rPos++];}}while (lPos <= leftEnd) {tmp[tmpPos++] = array[lPos++];}while (rPos <= rightEnd) {tmp[tmpPos++] = array[rPos++];}while (rightEnd >= begin) {array[rightEnd] = tmp[rightEnd];rightEnd--;}}} //堆排序public class Heap {public static void heapSort(int[] array) {int length = array.length;buildHeap(array, length);for (int i = length - 1; i > 0; i--) {swap(array, 0, i);percDown(array, 0, i);}}private static void buildHeap(int[] array, int length) {for (int i = length / 2; i >= 0; i--) {percDown(array, i, length);}}public static void percDown(int[] array, int i, int length) {int child = 0;int tmp;for (tmp = array[i]; leftChild(i) < length; i = child) {child = leftChild(i);if (child < length - 1 && array[child + 1] > array[child])child++;if (tmp < array[child])array[i] = array[child];elsebreak;}array[i] = tmp;}private static int leftChild(int i) {return 2 * i + 1;}private static void swap(int[] array, int i, int j) {int tmp;tmp = array[i];array[i] = array[j];array[j] = tmp;}} //快速排序public class Quick {public static void quickSort(int arr[], int l, int r) {if (l < r) {int pivot = partition(arr, l, r);quickSort(arr, l, pivot - 1);quickSort(arr, pivot + 1, r);}}private static int partition(int array[], int l, int r) {int k = l;int pivot = array[r];for (int i = l; i < r; i++)if (array[i] <= pivot)swap(array, i, k++);swap(array, k, r);return k;}private static void swap(int[] array, int i, int j) {int tmp = array[i];array[i] = array[j];array[j] = tmp;}}


0 0
原创粉丝点击