简单插入排序

 public class QuickSort {
private static void quickSort(int [] a, int low, int high){
if (low >= high){
return;
}
int i = low;
int j = high;
int temp = a[i];
while (i < j){
while (i <j && a[j] >= temp){
j--;
}
if (i < j){
a[i++] = a[j];
}
while (i < j && a[i] < temp){
i++;
}
if (i < j){
a[j--] = a[i];
}
}
a[i] = temp;
quickSort(a, low, i -1);
quickSort(a, i + 1, high);
}
public static void main(String [] args){
int [] a = {9,2,5,3,1,4,8,0,7,6};
quickSort(a, 0, a.length-1);
for (int anA : a) {
System.out.print(anA + " ");
}
}
}

选择排序

 public class SelectSort {
private static void sortSelect(int [] a ){
int i;
int j;
int temp = 0;
int flag = 0;
for (i = 0; i < a.length; i++){
temp = a[i];
flag = i;
for (j = i +1; j <a.length; j++){
if (a[j] < temp){
temp = a[j];
flag = j;
}
}
if (flag != i){
a[flag] = a[i];
a[i] = temp;
}
}
}
public static void main(String [] args){
int [] a = {3,1,5,4,2,7,8,6,0,9};
sortSelect(a);
for (int i = 0; i < a.length; i++){
System.out.print(a[i] + " ");
}
}
}

冒泡排序

 public class BubbleSort {
private static void bubbleSort(int [] a) {
int n = a.length;
for (int i = 0; i < n - 1; i++){
for (int j = n -1; j > i; j--){
if (a[j-1] > a[j]){
int temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}
}
public static void main(String [] args){
int [] a = {9,2,5,3,1,4,8,0,7,6};
bubbleSort(a);
for (int anA : a) {
System.out.print(anA + " ");
}
}
}

快速排序

 public class QuickSort {
private static void quickSort(int [] a, int low, int high){
if (low >= high){
return;
}
int i = low;
int j = high;
int temp = a[i];
while (i < j){
while (i <j && a[j] >= temp){
j--;
}
if (i < j){
a[i++] = a[j];
}
while (i < j && a[i] < temp){
i++;
}
if (i < j){
a[j--] = a[i];
}
}
a[i] = temp;
quickSort(a, low, i -1);
quickSort(a, i + 1, high);
}
public static void main(String [] args){
int [] a = {9,2,5,3,1,4,8,0,7,6};
quickSort(a, 0, a.length-1);
for (int anA : a) {
System.out.print(anA + " ");
}
}
}

希尔排序

 public class ShellSort {
public static void shellSort(int[] a){
int length = a.length;
for (int gap = length/2; gap > 0; gap /= 2){
for (int i = 0; i < gap; i++) {
for (int j = i + gap; j < length; j += gap){ //每个子序列都从第二个开始
if (a[j] < a[j - gap]){
int temp = a[j];
int k = j;
while ( k >= gap && a[k-gap] > temp){
a[k] = a[k-gap];
k -= gap;
}
a[k] = temp;
}
}
}
} }
public static void main(String[] args){
int [] a = {9,2,5,3,10,1,4,8,0,7,6};
shellSort(a);
for (int anA : a) {
System.out.print(anA + " ");
}
}
}

归并排序

 public class MergeSort {
public static void merge(int [] a, int start, int median, int end){
int i;
int j;
int k;
int n1 = median - start + 1;
int n2 = end - median;
int[] L = new int[n1];
int[] R = new int[n2];
for (i = 0, k = start; i < n1; i++, k++){
L[i] = a[k];
}
for (i = 0, k = median + 1; i < n2; i++, k++){
R[i] = a[k];
}
for (k = start, i = 0, j = 0; i < n1 && j < n2; k++){
if (L[i] < R[j]) {
a[k] = L[i];
i++;
} else {
a[k] = R[j];
j++;
}
}
while (i < n1){
a[k] = L[i];
i++;
k++;
}
while (j < n2){
a[k] = R[j];
j++;
k++;
}
}
public static void mergeSort(int[] a, int start, int end){
if (start < end){
int median = (start + end) / 2;
mergeSort(a, start, median);
mergeSort(a, median + 1, end);
merge(a, start, median, end);
}
} public static void main(String[] args){
int [] a = {9,2,5,3,10,1,4,8,0,7,6};
mergeSort(a, 0, a.length-1);
for (int anA : a) {
System.out.print(anA + " ");
}
}
}

堆排序

 public class HeapSort {
private static void maxHeapDown(int[] a, int start, int end){
int father = start;
int child = 2 * father + 1; // 左孩子
while (child <= end){
if (child < end && a[child] < a[child + 1]){
child++; // 如果右孩子大,将左孩子变为右孩子
}
if (a[father] >= a[child]){
break;
}else {
int tmp = a[father];
a[father] = a[child];
a[child] = tmp;
}
father = child;
child = 2 * father + 1;
}
}
private static void heapSortAsc(int[] a){
int i;
int n = a.length;
for (i = n / 2 -1; i >= 0; i--){ // 从最后一个非终端节点开始,逐个向上遍历
maxHeapDown(a, i, n-1);
}
for (i = n - 1; i > 0; i--){
int tmp = a[0];
a[0] = a[i];
a[i] = tmp;
maxHeapDown(a, 0, i-1);
}
}
public static void main(String[] args){
int[] a = {9,2,5,4,7,3,8,0,1,6};
heapSortAsc(a);
for (int i :a){
System.out.print(i + " ");
}
}
}

基数排序

 public class RadixSort {
private static int getMax(int[] a ){
int max = a[0];
for (int i : a){
if (i > max){
max = i;
}
}
return max;
}
private static void countSort(int[] a, int exp){
int[] output = new int[a.length];
int[] buckets = new int[10];
for (int anA : a) {
buckets[(anA / exp) % 10]++;
}
for (int i : buckets)
System.out.print(i + " ");
System.out.println();
for (int i = 1; i < 10; i++){
buckets[i] += buckets[i - 1];
}
for (int i : buckets)
System.out.print(i + " ");
System.out.println();
for (int i = a.length - 1; i >= 0; i--){
output[buckets[(a[i] / exp) % 10] - 1] = a[i];
buckets[(a[i] / exp) % 10]--;
}
for (int i = 0; i < a.length; i++){
a[i] = output[i];
}
output = null;
buckets = null;
}
private static void radixSort(int[] a){
int max = getMax(a);
for (int exp = 1; max / exp > 0; exp *= 10){
countSort(a, exp);
}
}
public static void main(String[] args){
int[] a = {53, 3, 542, 748, 14, 214, 154, 63, 616};
radixSort(a);
for (int i : a){
System.out.print(i + " ");
}
}
}

八大排序算法的Java代码实现的更多相关文章

  1. 常见排序算法(附java代码)

    常见排序算法与java实现 一.选择排序(SelectSort) 基本原理:对于给定的一组记录,经过第一轮比较后得到最小的记录,然后将该记录与第一个记录的位置进行交换:接着对不包括第一个记录以外的其他 ...

  2. 常见的排序算法之Java代码解释

    一 简要介绍 一般排序均值的是将一个已经无序的序列数据重新排列成有序的 常见的排序分为: 1 插入类排序 主要就是对于一个已经有序的序列中,插入一个新的记录.它包括:直接插入排序,折半插入排序和希尔排 ...

  3. 一遍记住 8 种排序算法与 Java 代码实现

    ☞ 程序员进阶必备资源免费送「21种技术方向!」 ☜ 作者:KaelQ, www.jianshu.com/p/5e171281a387 1.直接插入排序 经常碰到这样一类排序问题:把新的数据插入到已经 ...

  4. 八大排序算法的java实现

    有时间再贴算法分析图 JDK7的Collections.sort()的算法是TimSort, 适应性的归并排序, 比较晦涩难懂, 这里没有实现 public class mySort { // 冒泡排 ...

  5. 八大排序算法详解(动图演示 思路分析 实例代码java 复杂度分析 适用场景)

    一.分类 1.内部排序和外部排序 内部排序:待排序记录存放在计算机随机存储器中(说简单点,就是内存)进行的排序过程. 外部排序:待排序记录的数量很大,以致于内存不能一次容纳全部记录,所以在排序过程中需 ...

  6. 八大排序算法Java实现

    本文对常见的排序算法进行了总结. 常见排序算法如下: 直接插入排序 希尔排序 简单选择排序 堆排序 冒泡排序 快速排序 归并排序 基数排序 它们都属于内部排序,也就是只考虑数据量较小仅需要使用内存的排 ...

  7. 八大排序算法总结与java实现(转)

    八大排序算法总结与Java实现 原文链接: 八大排序算法总结与java实现 - iTimeTraveler 概述 直接插入排序 希尔排序 简单选择排序 堆排序 冒泡排序 快速排序 归并排序 基数排序 ...

  8. Java八大排序算法

    Java八大排序算法: package sort; import java.util.ArrayList; import java.util.Arrays; import java.util.List ...

  9. 八大排序算法 JAVA实现 亲自测试 可用!

    今天很高兴 终于系统的实现了八大排序算法!不说了 直接上代码 !代码都是自己敲的, 亲测可用没有问题! 另:说一下什么是八大排序算法: 插入排序 希尔排序 选择排序 堆排序 冒泡排序 快速排序 归并排 ...

随机推荐

  1. get请求中url传参中文乱码问题--集锦

    一:get请求url中带有中文参数,有三种方式进行处理防止中文乱码 1.如果使用tomcat作为服务器,那么修改tomcat配置文件conf/server.xml中,在  <Connector  ...

  2. apt-get出现的问题

    报的错 E: 无法获得锁 /var/cache/apt/archives/lock – open (11 资源临时不可用) E: 无法锁定下载目录 解决方法一: #:ps -aux (列出进程,形式如 ...

  3. video 播放本地视屏

    var file = document.getElementById('file_video').files[0]; var url = URL.createObjectURL(file); docu ...

  4. 关于WPS查看PDF文件操作问题

    自己一直使用WPS打开PDF类的文档,但是使用过程中,存在下面的几个问题: pdf 如何查看当前页码pdf 如何根据目录跳转到指定页WPS 查看pdf 如何跳转到指定页 后来百度后,可以考虑将PDF转 ...

  5. 虚拟机 安装 CentOS7

    初次接触CentOS,最好是在虚拟机中安装: 因为CentOS的安装选项有很多,不理解的情况下千万不要在物理机上尝试: 不然可能出现这种情况:安装好以后,只有黑色的屏幕,只能在里面敲命令:这对于新手来 ...

  6. Dictionary在多线程情况下

    Add时出错 错误信息: Index was outside the bounds of the array. 详细信息: at System.Collections.Generic.Dictiona ...

  7. Unity&Sqlite数据库

    Sqlite是一个跨平台关系型小型数据库,非常便利,适合于嵌入式设备:对于Sqlite数据库来说,这个数据库是以文件的形成存在的(比如data.db):数据库是由表组成的,在一个数据库里面可以存储多个 ...

  8. 2021工厂取消2094仓位需求,不参与FP分析

    正确做法应该是修改这个sSIS NOT IN 2094 目前在不修改SSIS前提下,可以先在进IN表前过滤掉

  9. 更新linux下python版本

    # 安装所有的开发工具包 yum groupinstall -y "Development tools" # 安装其它的必需包 yum install -y zlib-devel ...

  10. date命令转换日期命令提示date: illegal time format

    问题:运行date命令抛错 date -j -f "%a %b %d %T %Z %Y" "Sat Sep 29 11:33:00 CST 2018" &quo ...