最近查资料发现java排序挺有意思的,其中包含常见八种具有代表性的排序法;笔者觉得排序的功能重要,但更重要的是排序的思想;所以简单叙述一下常见排序方法名称,并用代码举例。

A.插入排序(直接插入排序、希尔排序);
B.交换排序(冒泡排序、快速排序);
C.选择排序(直接选择排序、堆排序);
D.归并排序;
E.分配排序(基数排序);

所需辅助空间最多:归并排序;
所需辅助空间最少:堆排序;
平均速度最快:快速排序;

不稳定:快速排序,希尔排序,堆排序。

代码块:

package com.sinolife.mtrs.apply.controller;

import java.util.Arrays;

/**
* @author delin Li
* @version createTime:2017-12-7下午04:10:37
* @Description
*/
public class TestSort { /**
* @param args
*/
public static void main(String[] args) {
int[] ins = {1,3,53,7,81,2,5,71,9};
// insertSort(ins);//直接插入排序
// shellSort(ins);//希尔排序,
// sampleSelectSort(ins);//简单选择排序
// heapSort(ins);//堆排序
// bubbleSort(ins);//冒泡 排序
// quikSort(ins,0,ins.length-1);//快速排序
// mergeSort(ins,0,ins.length-1);//归并排序
radixSort(ins,10,2);//基排序
print(ins);
} public static void print(int[] data) {
for (int i = 0; i < data.length; i++) {
System.out.print(data[i] + " ");
}
} //直接插入排序
/*public static void insertSort(int arg[]){
for(int i=1;i<arg.length;i++){//将第一个数当做已经排好序的值,然后将后边无序数依次插入到已经排好的列表中
int j;
int temp = arg[i];//插入的数值
for(j=i;temp<arg[j-1]&&j>0;j--){
arg[j] = arg[j-1];////通过循环,逐个后移一位找到要插入的位置。
}
arg[j]=temp;//插入
}
}*/ //希尔
/*public static void shellSort(int a[]){
int n = a.length;
for(int i = n/2;i>0; i /=2){
for(int j = i;j<n;j++){//每个元素与自己组内的数据进行直接插入排序
if(a[j]<a[j-i]){
int temp = a[j];
int k = j-i;
while(k>=0&&a[k]>temp){
a[k+i] = a[k];
k -=i;
}
a[k+i] = temp;
}
}
}
}*/ //简单选择排序
/*public static void sampleSelectSort(int[] a){
int minV;//临时保存最小值
int minI;//临时保存最小值下标
for(int i= 0;i<a.length-1;i++){
minV = a[i];
minI = i;
for(int j = i+1;j<a.length;j++){
if(a[j]<minV){
minV = a[j];
minI = j;
}
}
if(minV != a[i] && minI !=i){
a[minI] = a[i];
a[i] = minV;
}
}
}*/ //堆排序
/*public static void swap(int[] data, int i, int j) {
if (i == j) {
return;
}
data[i] = data[i] + data[j];
data[j] = data[i] - data[j];
data[i] = data[i] - data[j];
} public static void heapSort(int[] data) {
for (int i = 0; i < data.length; i++) {
createMaxdHeap(data, data.length - 1 - i);
swap(data, 0, data.length - 1 - i);
print(data);
}
} public static void createMaxdHeap(int[] data, int lastIndex) {
for (int i = (lastIndex - 1) / 2; i >= 0; i--) {
// 保存当前正在判断的节点
int k = i;
// 若当前节点的子节点存在
while (2 * k + 1 <= lastIndex) {
// biggerIndex总是记录较大节点的值,先赋值为当前判断节点的左子节点
int biggerIndex = 2 * k + 1;
if (biggerIndex < lastIndex) {
// 若右子节点存在,否则此时biggerIndex应该等于 lastIndex
if (data[biggerIndex] < data[biggerIndex + 1]) {
// 若右子节点值比左子节点值大,则biggerIndex记录的是右子节点的值
biggerIndex++;
}
}
if (data[k] < data[biggerIndex]) {
// 若当前节点值比子节点最大值小,则交换2者得值,交换后将biggerIndex值赋值给k
swap(data, k, biggerIndex);
k = biggerIndex;
} else {
break;
}
}
}
}*/ //冒泡排序
/*public static void bubbleSort(int[] a){
for (int i = 0; i < a.length-1; i++) {
for (int j = i; j < a.length-i-1; j++) {
if(a[j]>a[j+1]){
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}*/ //快速排序
/*public static void quikSort(int data[], int start, int end) { if (end - start <= 0) {
return;
}
int last = start;
for (int i = start + 1; i <= end; i++) {
if (data[i] < data[start]) {
int temp = data[++last];
data[last] = data[i];
data[i] = temp;
}
}
int temp = data[last];
data[last] = data[start];
data[start] = temp;
quikSort(data, start, last - 1);
quikSort(data, last + 1, end);
}*/ //归并排序
/*public static void mergeSort(int data[], int start, int end) {
if (start < end) {
int mid = (start + end) / 2;
mergeSort(data, start, mid);
mergeSort(data, mid + 1, end);
merge(data, start, mid, end);
}
} public static void merge(int data[], int start, int mid, int end) {
int temp[] = new int[end - start + 1];
int i = start;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= end) {
if (data[i] < data[j]) {
temp[k++] = data[i++];
} else {
temp[k++] = data[j++];
}
} while (i <= mid) {
temp[k++] = data[i++];
}
while (j <= end) {
temp[k++] = data[j++];
} for (k = 0, i = start; k < temp.length; k++, i++) {
data[i] = temp[k];
}
}*/ //基排序
public static void radixSort(int[] data, int radix, int d) {
// 缓存数组
int[] tmp = new int[data.length];
// buckets用于记录待排序元素的信息
// buckets数组定义了max-min个桶
int[] buckets = new int[radix]; for (int i = 0, rate = 1; i < d; i++) { // 重置count数组,开始统计下一个关键字
Arrays.fill(buckets, 0);
// 将data中的元素完全复制到tmp数组中
System.arraycopy(data, 0, tmp, 0, data.length); // 计算每个待排序数据的子关键字
for (int j = 0; j < data.length; j++) {
int subKey = (tmp[j] / rate) % radix;
buckets[subKey]++;
}
for (int j = 1; j < radix; j++) {
buckets[j] = buckets[j] + buckets[j - 1];
}
// 按子关键字对指定的数据进行排序
for (int m = data.length - 1; m >= 0; m--) {
int subKey = (tmp[m] / rate) % radix;
data[--buckets[subKey]] = tmp[m];
}
rate *= radix;
}
}
}

如有错误,请朋友们提出!

java常用八大排序法的更多相关文章

  1. Java使用选择排序法对数组排序

    编写程序,实现将输入的字符串转换为一维数组,并使用选择排序法对数组进行排序. 思路如下: 点击"生成随机数"按钮,创建Random随机数对象: 使用JTextArea的setTex ...

  2. java之八大排序

    的关系:  1.直接插入排序 (1)基本思想:在要排序的一组数中,假设前面(n-1)[n>=2] 个数已经是排 好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数 也是排好顺序的.如此反 ...

  3. java:选择排序法对数组排序

    最近想练一练Java的算法,然后碰到LeetCode上一道从排序数组删除重复项的小题,刚开始没看到是从排序数组中,就乱写,其实要是排序树组,就比乱序的感觉上好写多了.然后就想回顾下冒泡法对数组排序,凭 ...

  4. Java数组 —— 八大排序

    (请观看本人博文--<详解 普通数组 -- Arrays类 与 浅克隆>) 在本人<数据结构与算法>专栏的讲解中,本人讲解了如何去实现数组的八大排序. 但是,在讲解的过程中,我 ...

  5. java 的八大排序

    import java.util.Arrays;import java.util.*; public class Sort { /** * 插入排序 */ public static void ins ...

  6. java实现八大排序算法

    Arrays.sort() 采用了2种排序算法 -- 基本类型数据使用快速排序法,对象数组使用归并排序. java的Collections.sort算法调用的是归并排序,它是稳定排序 方法一:直接插入 ...

  7. java的八大排序

    public class Sort2 { public static void main(String[] args) { Sort2 sort = new Sort2(); System.out.p ...

  8. Java常用的排序算法三

    Merge Sort :归并排序:用递归的思想,分解成单个元素的排序,在归并 代码: import java.util.*; public class MergeSort { public stati ...

  9. Java实现八大排序之冒泡排序

    冒泡排序 冒泡排序的定义: 冒泡排序(Bubble Sort)它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该 ...

随机推荐

  1. Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等

    实现两个整数的加减乘除,在每个方法执行前后打印日志. ArithmeticCalculator.java: package spring.aop.impl.xml; public interface ...

  2. codeforces707B:Bakery

    Description Masha wants to open her own bakery and bake muffins in one of the n cities numbered from ...

  3. 微信开放接口获取用户昵称保存到MySQL中为空白

    微信昵称中包含emoji表情标签,某些标签是使用了4字节编码的UTF8. 而大多数MySQL数据库现在使用的是3字节UTF8编码,这样会导致保存为空,且不会提示失败. 解决方法有2个,一个是升级到My ...

  4. WebUI 常用

    //鼠标移动显示div //position:absolute这个是绝对定位:是相对于浏览器的定位.比如:position:absolute:left:20px;top:80px; 这个容器始终位于距 ...

  5. U14739 X ask Y III 子区间异或和

    题意:就是求所有子区间的异或和的和 题解:就是算每一位对结果的贡献(最近好像遇到很多次这种题目),先前缀异或,从左向右扫记录二进制前缀的1,0个数,xor[i]==xor[j]^1的时候就加上这一位的 ...

  6. LINUX C的学习

    好吧看着LINUX那么多的命令好难受= =看到第三章有介绍C的编译的,先写下试试喽. 可以用gedit或者vim,老师虽然大肆吹捧vim的经典原谅用的真吉儿难受- -,一开始没安装vim用的gedit ...

  7. MySQL学习之一数据库简介

    1.什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,长期储存在计算机内.有组织的.可共享的数据集合. 数据库中的数据指的是以一定的数据模型组织.描述和储存在一起. ...

  8. 洛谷 P3048 [USACO12FEB]牛的IDCow IDs

    题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, ...

  9. 转: 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment

    http://blog.csdn.net/my2010sam/article/details/17735159

  10. 关于讯飞语音SDK开发学习

    前奏,浑浑噩噩已经工作一年多,这一年多收获还是挺多的.逛园子应该有两年多了,工作后基本上是天天都会来园子逛逛,园子 里还是有很多牛人写了一些不错的博客,帮我解决很多问题.但是一直没写过博客,归根到底一 ...