堆排序思想:堆排序,顾名思义,就是基于堆。堆分为最大堆和最小堆,其实就是完全二叉树。最大堆要求节点的元素都要大于其孩子,最小堆要求节点元素都小于其左右孩子,两者对左右孩子的大小关系不做任何要求,其实很好理解。有了上面的定义,我们可以得知,处于最大堆的根节点的元素一定是这个堆中的最大值。其实我们的堆排序算法就是抓住了堆的这一特点,每次都取堆顶的元素,将其放在序列最后面,然后将剩余的元素重新调整为最大...
一、冒泡排序(Bubble Sort)public class BubbleSort {public static void main(String[] args) {int[] arr = {3, 4, 2, 9, 10, 15, 11, 0, 1};System.out.println(Arrays.toString(bubbleSort(arr)));}public static int[] bubbleSort(int[] arr){for (int i = 0; i < arr.length - 1; i++) {for(int j = 0; j < arr.length - 1 - i; j++){if(arr[j] > arr[j + 1]){int temp = arr[j + 1];arr[j + 1] = arr[j];arr[j] = temp;}}}...
快速排序(Quicksort)是对冒泡排序的一种改进。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。实现方式一: 1package test1;2 3publicclass QuicSort {4/* 5 * 使用快速排序6 * arras:要排序的数组7 * low:数组的开始下标8 * hig...
JavaScript基础之数组的重排序 数组(Array)算是继Object之后我们最常用到的类型了。与其他语言不同的是,javascript数组的每一项可以保存任何类型的数据,并且数组的大小是可以动态调整的。 JavaScript已经为我们提供了两个数组重排序的方法,reverse()和sort() 方法。 其中reverse()方法会反转数组项的顺序。举个栗子:var values = [18,4,56,1,"a","b","abc"];
values.reverse();
alert(values); // abc,b,a,1,56,4,18...
public class QuickSort { public static void quickSort(int[] arr, int left, int right){ int temp; int i = left; int j = right; if (left < right){ // temp存放基准数("枢轴") temp = arr[left]; while (i != j){ // 先从右扫描,只要比temp大j就向左移,直到找到一个比"枢轴"temp小的数 while (i < j && arr[j]>temp){ ...
快速排序是冒泡排序的优化,是一种非常高效的排序, 甚至是目前为止最高效的排序,其思想是这样的:设数组a中存放了n个数据元素,low为数组的低端下标,high为数组的高端下标,从数组a中任取一个元素(通常取a[low])做为标准元素,以该标准元素调整数组a中其他各个元素的位置,使排在标准元素前面的元素均小于标准元素,排在标准元素后面的均大于或等于标准元素,由此将数组根据标准元素分解成了两个子数组。对这两个子数组中的元...
JavaScript常见排序以下两个函数是排序中会用到的通用函数,就不一一写了function checkArray(array) {if (!array || array.length <= 2) return
}
function swap(array, left, right) {let rightValue = array[right]array[right] = array[left]array[left] = rightValue
}View Code冒泡排序冒泡排序的原理如下,从第一个元素开始,把当前元素和下一个索引元素进行比较。如果当前元素大,那么就交换位置,重复操作直到比较到最后一...
希尔排序的基本介绍:希尔排序同之前的插入排序一样,它也是一种插入排序,只不过它是简单插入排序之后的一个优化的排序算法,希尔排序也被称为缩小增量排序。希尔排序的基本思想:希尔排序是把数组中给定的元素按照下标的一定增量进行分组,在分组之后,对每组使用直接插入排序算法;随着增量的减少,每组包含的元素越来越多,当增量减少到1的时候,整个数组正好被分成一组,此时该算法终止。通常我们判断增量是通过:第一次的增量...
冒泡排序:车轮战,两辆比较,小的靠前特点:1.轮数:共比较length-1轮2.每次比较的次数:①第一次比较length-1次,确定出最大的数②第二次比较length-2次,确定出倒数第二大的数...代码:双层循环表示整个排序的过程1.外层循环:控制比较的轮数,从1,开始,到length-1(能取到)结束2.内层循环:控制每轮中比较的次数,同时也要表示参与比较的元素的下标,从0开始,到length-1-i(轮数变量)结束原文:https://www.cnblogs.com/10...
二次排序工具类:import java.io.Serializable;
import scala.math.Ordered;/**
* @author 作者 E-mail:
* @version 创建时间:2017年8月30日 下午3:48:11
* 类说明
*/
//二次排序key
public class SecondeIndexSort implements Ordered<SecondeIndexSort>, Serializable{private static final long serialVersionUID = -2366006422945129991L;// 首先在自定义key里面,定义需要进行排序的列private int first;private int second...
Sort a linked list using insertion sort.A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.With each iteration one element (red) is removed from the input data and inserted in-place into the sorted listAlgorithm of Insertion Sort:1Insertion sort iterates, consuming one input element each repetition, and growing a sorted outp...
public class SortDemo {public static void main(String[] args) {int[] arr = { 12, 3, 4, 56, 688, 9 };bubbleSort(arr);selectSort(arr);print(arr);}// 选择public static void selectSort(int arr[]) {for (int i = 0; i < arr.length; i++) {for (int j = i + 1; j < arr.length; j++) {if (arr[i] > arr[j]) {int temp = arr[j];arr[j] = arr[i];arr[i] = temp;}}}}// 冒泡public static void bubbleSort(int arr[]) {for ...
Java泛型——对一个对象数组进行排序public class GenericSort {public static void show() {Integer[] intArray = {new Integer(2),new Integer(4),new Integer(3)};Double[] doubleArray = {new Double(2.5),new Double(6.4),new Double(3.3)};Character[] charArray = {new Character(‘a‘),new Character(‘q‘),new Character(‘c‘)};String[] stringArray = {"liu","lu","hhh"};sort(intArray);sort(doubleArray);sort(cha...
一、核心思想将待排序序列R[0...n-1]看成是n个长度为1的有序序列,将相邻的有序表成对归并,得到n/2个长度为2的有序表;将这些有序序列再次归并,得到n/4个长度为4的有序序列;如此反复进行下去,最后得到一个长度为n的有序序列。综上可知:归并排序其实要做两件事:(1)“分解”——将序列每次折半划分。(2)“合并”——将划分后的序列段两两合并后排序。 我们先来考虑第二步,如何合并?在每次合并过程中,都是对两个有序的序...
publicclass TestMain {publicstaticvoid main(String[] args) {Integer[] a = new Integer[5000];for (int i = 0; i < a.length; i++) {int temp = (int)(StdRandom.random()*10000);a[i] = temp;}Integer[] b = new Integer[5000];for (int i = 0; i < b.length; i++) {b[i] = a[i];}//生成两个相同的随机数组Stopwatch timer2 = new Stopwatch();ToSort.insertsort(b);System.out.println(timer2.elapsedTime());//比较两种排序...