冒泡排序
特性:效力低,完成简朴
头脑(从小到大排):每一趟将待排序序列中最大元素移到末了,剩下的为新的待排序序列,反复上述步骤直到排完一切元素。这只是冒泡排序的一种,固然也能够从后往前排。
public void bubbleSort(int array[]) { int t = 0; for (int i = 0; i < array.length - 1; i++) for (int j = 0; j < array.length - 1 - i; j++) if (array[j] > array[j + 1]) { t = array[j]; array[j] = array[j + 1]; array[j + 1] = t; } }
挑选排序
特性:效力低,轻易完成
头脑:每一趟从待排序序列挑选一个最小的元素放到已排好序序列的末端,剩下的位待排序序列,反复上述步骤直到完成排序。
public void selectSort(int array[]) { int t = 0; for (int i = 0; i < array.length - 1; i++) for (int j = i + 1; j < array.length; j++) if (array[i] > array[j]) { t = array[i]; array[i] = array[j]; array[j] = t; } }
插进去排序
特性:效力低,轻易完成
头脑:将数组分为两部份,将后部份元素一一与前部份元素比较,假如当前元素array[i]小,就替代。找到合理位置插进去array[i]
public void insertionSort(int array[]) { int i, j, t = 0; for (i = 1; i < array.length; i++) { t = array[i]; for (j = i - 1; j >= 0 && t < array[j]; j--) array[j + 1] = array[j]; array[j + 1] = t; } }
疾速排序
特性:高效,时候复杂度为nlogn。
采纳分治法的头脑:起首设置一个轴值pivot,然后以这个轴值为分别基准将待排序序列分红比pivot大和比pivot小的两部份,接下来对分别完的子序列举行快排直到子序列为一个元素为止。
public void quickSort(int array[], int low, int high) {// 传入low=0,high=array.length-1; int pivot, p_pos, i, t;// pivot->位索引;p_pos->轴值。 if (low < high) { p_pos = low; pivot = array[p_pos]; for (i = low + 1; i <= high; i++) if (array[i] > pivot) { p_pos++; t = array[p_pos]; array[p_pos] = array[i]; array[i] = t; } t = array[low]; array[low] = array[p_pos]; array[p_pos] = t; // 分而治之 quickSort(array, low, p_pos - 1);// 排序左半部份 quickSort(array, p_pos + 1, high);// 排序右半部份 }
引荐教程:Java教程
以上就是java中关于排序体式格局的实例解说的细致内容,更多请关注ki4网别的相干文章!