八大排序算法的Java代码实现
简单插入排序
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代码实现的更多相关文章
- 常见排序算法(附java代码)
常见排序算法与java实现 一.选择排序(SelectSort) 基本原理:对于给定的一组记录,经过第一轮比较后得到最小的记录,然后将该记录与第一个记录的位置进行交换:接着对不包括第一个记录以外的其他 ...
- 常见的排序算法之Java代码解释
一 简要介绍 一般排序均值的是将一个已经无序的序列数据重新排列成有序的 常见的排序分为: 1 插入类排序 主要就是对于一个已经有序的序列中,插入一个新的记录.它包括:直接插入排序,折半插入排序和希尔排 ...
- 一遍记住 8 种排序算法与 Java 代码实现
☞ 程序员进阶必备资源免费送「21种技术方向!」 ☜ 作者:KaelQ, www.jianshu.com/p/5e171281a387 1.直接插入排序 经常碰到这样一类排序问题:把新的数据插入到已经 ...
- 八大排序算法的java实现
有时间再贴算法分析图 JDK7的Collections.sort()的算法是TimSort, 适应性的归并排序, 比较晦涩难懂, 这里没有实现 public class mySort { // 冒泡排 ...
- 八大排序算法详解(动图演示 思路分析 实例代码java 复杂度分析 适用场景)
一.分类 1.内部排序和外部排序 内部排序:待排序记录存放在计算机随机存储器中(说简单点,就是内存)进行的排序过程. 外部排序:待排序记录的数量很大,以致于内存不能一次容纳全部记录,所以在排序过程中需 ...
- 八大排序算法Java实现
本文对常见的排序算法进行了总结. 常见排序算法如下: 直接插入排序 希尔排序 简单选择排序 堆排序 冒泡排序 快速排序 归并排序 基数排序 它们都属于内部排序,也就是只考虑数据量较小仅需要使用内存的排 ...
- 八大排序算法总结与java实现(转)
八大排序算法总结与Java实现 原文链接: 八大排序算法总结与java实现 - iTimeTraveler 概述 直接插入排序 希尔排序 简单选择排序 堆排序 冒泡排序 快速排序 归并排序 基数排序 ...
- Java八大排序算法
Java八大排序算法: package sort; import java.util.ArrayList; import java.util.Arrays; import java.util.List ...
- 八大排序算法 JAVA实现 亲自测试 可用!
今天很高兴 终于系统的实现了八大排序算法!不说了 直接上代码 !代码都是自己敲的, 亲测可用没有问题! 另:说一下什么是八大排序算法: 插入排序 希尔排序 选择排序 堆排序 冒泡排序 快速排序 归并排 ...
随机推荐
- nmap使用
Nmap使用 Nmap是主机扫描工具,他的图形化界面是Zenmap,分布式框架为Dnamp. Nmap可以完成以下任务: 主机探测 端口扫描 版本检测 系统检测 支持探测脚本的编写 Nmap在实际中应 ...
- zookeeper 集群配置采坑 Connection refused WARN [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@584] - Cannot open channel to 3 at election address slave2/192.168.127.133:3888
坑一: Cannot open channel to at election address slave1/ java.net.ConnectException: Connection refused ...
- 1.3.4、CDH 搭建Hadoop在安装之前(端口---Impala使用的端口)
Impala使用的端口 Impala使用下表中列出的TCP端口.在部署Impala之前,请确保在每个系统上打开这些端口. Component Service Port Access Requireme ...
- AutoIncrement无法设置的问题
[AutoIncrement无法设置的问题] 下图红色处始终无法勾选Auto_Increment 解决方法是在详细列表里勾选. 链接:http://stackoverflow.com/question ...
- SVN基本页面
- 大数据入门到精通3-SPARK RDD filter 以及 filter 函数
一.如何处理RDD的filter 1. 把第一行的行头去掉 scala> val collegesRdd= sc.textFile("/user/hdfs/CollegeNavigat ...
- Numpy数据处理函数
Numpy函数介绍 import numpy as np #sqrt 计算各元素的平方根 arr = np.arange(10) np.sqrt(arr) array([0. , 1. , 1.414 ...
- 二 random模块
1 import random 2 3 print(random.random())#(0,1)----float 大于0且小于1之间的小数 4 5 print(random.randint(1,3) ...
- 2019年华南理工大学程序设计竞赛(春季赛)-H-Parco_Love_GCD
题目链接:https://ac.nowcoder.com/acm/contest/625/H 题意:给定n个数(<=1e9)的序列,其中n<=5e5,求该序列所有子序列的对应的gcd对1e ...
- 查看linux发行版版本
可以通过查看/etc/issue文件查看发行版版本号,不论系统是redhat\suse\debian