除了冒泡排序,你还需要知道这些排序算法 |
您所在的位置:网站首页 › 排序和分类是什么的常用方法 › 除了冒泡排序,你还需要知道这些排序算法 |
那些年我们面试时经常会被问到排序算法,还有被要求现场手写排序算法。这篇文章我们来介绍下程序员遇到过的排序算法。 插入排序从第一个元素开始,该元素可以认为已经被排序; 取出下一个元素,在已经排序的元素序列中从后向前扫描; 如果该元素(已排序)大于新元素,将该元素移到下一位置; 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置; 将新元素插入到该位置后; 重复步骤2~5。 ❞ private static int[] insertionSort(int[] array) { if (array.length == 0) { return array; } int current; for (int i = 0; i = 0 && current if (array.length end) { return null; } int smallIndex = partition(array, start, end); if (smallIndex > start) { quickSort(array, start, smallIndex - 1); } if (smallIndex int pivot = (int) (start + Math.random() * (end - start + 1)); int smallIndex = start - 1; swap(array, pivot, end); for (int i = start; i smallIndex++; if (i > smallIndex) { swap(array, i, smallIndex); } } } return smallIndex; } private static void swap(int[] array, int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } 桶排序 public class BucketSort { @Test public void test1(){ int[] t = {5 ,3 ,5 ,2 ,8}; bucketSort(t); } private void bucketSort(int[] t) { int a[] = new int[11]; for (int i = 0; i |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |