Java 快排】的更多相关文章

基于分治法的快排,用递归实现. 首先讲一下实现的过程. 1.在数组中取一个数作为基准,所谓的基准就是用来对比的数. 2.然后在数组中从后往前找,找到一个逆序数为止,找到之后就把它的值赋值到基准数的位,假设此逆序数的为止为j. 3.然后再数组中从前往后找,找到一个逆序为止,然后把这个值赋值到j位置,假设此次逆序数位置为i. 4.重复23.直i>=j. 说白点,假设目标顺序是升序,那么,在一次迭代后,处于基准为止左边的数全部的数都小于基准数,右边的全部都大于基准数. 然后再用分治法的思想,递归的调用…
一.快排的一种 ==================== public class myMain { public static void main(String[] args) { int t[] = {2,6,7,8,5,4},low,high; low = 0; high = t.length-1; allSort(t,low,high); for(int t1:t){ System.out.print(t1); } } public static void allSort(int[] t…
快排是最基础的排序算法之一,今天来回顾一下. public class QuickSort { public static void quickSort(int[] array){ if(array != null){ quickSort(array, 0, array.length-1); } } private static void quickSort(int[] array,int beg,int end){ if(beg >= end || array == null) return;…
直接贴代码 快排: public class Test { private static void sort(int[] nums){ if(nums == null || nums.length == 0){ return; } quickSort(nums, 0, nums.length-1); } private static void quickSort(int[] nums, int start, int end) { int low = start; int high = end;…
1分治思想 1.1比大小在分区 1.2从数组中取出一个数做基准数 1.3将比他小的数全放在他的左边,比他大的数全放在他的右边 1.4然后递归 左边 和右边 }…
package quickSort; /** * 快速排序 * @author root * */ public class QuickSort { static int[] data = {0,2,4,5,3,1,7,6}; public static void main(String[] args) { // TODO Auto-generated method stub sort(data, 0, data.length-1); print(data); } //快速排序 public s…
本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排序.快速排序(重点).堆排序.归并排序等等.看下图: 给定数组:int data[] = {9,2,7,19,100,97,63,208,55,78} , , , , , , , , ,  }; public static void insertSort() { int tmp, j = ; for…
插入排序 import java.util.Arrays; public class InsertionSort { /** * 对数组里面进行插入排序 * 参数1 数组 * 参数2 数组大小 */ static void InsertSort(int arr[]){ int in,out,temp; for ( out = 1; out < arr.length; out++) { temp = arr[out]; in=out; while (in>0 && arr[in-…
import java.util.Arrays; public class QuickSort { //三数取中法.取出不大不小的那个位置 public static int getPivotPos(int[] a,int low,int high) { int mid=(low+high)/2; int pos=low; if(a[mid]<a[low]) { int temp=a[low]; a[low]=a[mid]; a[mid]=temp; } if(a[high]<a[low])…
//堆排序 不稳定 import java.util.Arrays; public class HeapSort { public static void main(String[] args) { int[] a={49,38,65,97,76,13,27,49,78,34,12,64}; int arrayLength=a.length; //循环建堆 for(int i=0;i<arrayLength-1;i++){ //建堆 buildMaxHeap(a,arrayLength-1-i)…