Java八大排序算法
Java八大排序算法:
package sort; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
* mysort
*
* @author Arya
*
*/
public class MySort { public static void main(String[] args) {
MySort mySort = new MySort();
mySort.insertSort();
mySort.shellSort();
mySort.selectSort();
mySort.bubbleSort();
mySort.quickSort();
mySort.HeapSort();
mySort.mergingSort();
mySort.radixSort();
} /**
* 直接插入排序:稳定 时间复杂度:最好O(n),其它O(n^2) 空间复杂度:O(1)
*/
public void insertSort() {
int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,
99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
int temp = 0;
for (int i = 1; i < a.length; ++i) {
temp = a[i];
int j = i - 1;
for (; j >= 0 && temp < a[j]; --j) {
a[j + 1] = a[j];
}
a[j + 1] = temp;
}
for (int i = 0; i < a.length; ++i)
System.out.print(a[i] + "--");
System.out.println();
} /**
* 希尔排序:不稳定 时间复杂度:O(nlog2n)~O(n^2) 空间复杂度:O(1)
*/
public void shellSort() {
int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,
99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
int d = a.length;
int temp = 0;
while (true) {
d = (int) Math.ceil(d / 2);
for (int x = 0; x < d; x++) {
for (int i = x + d; i < a.length; i += d) {
int j = i - d;
temp = a[i];
for (; j >= 0 && temp < a[j]; j -= d) {
a[j + d] = a[j];
}
a[j + d] = temp;
}
}
if (d == 1)
break;
}
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + "--");
System.out.println();
} /**
* 选择排序:不稳定 时间复杂度:O(n^2) 空间复杂度:O(1)
*/
public void selectSort() {
int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,
99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
int position = 0;
for (int i = 0; i < a.length; i++) {
int j = i + 1;
position = i;
int temp = a[i];
for (; j < a.length; j++) {
if (a[j] < temp) {
temp = a[j];
position = j;
}
}
a[position] = a[i];
a[i] = temp;
}
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + "--");
System.out.println();
} /**
* 冒泡排序:稳定 时间复杂度:最好O(n),其它O(n^2) 空间复杂度:O(1)
*/
public void bubbleSort() {
int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,
99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
int temp = 0;
int flag = 1;
for (int i = 0; i < a.length - 1 && flag == 1; i++) {
flag = 0;
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
flag = 1;
}
}
}
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + "--");
System.out.println();
} /**
* 快速排序:不稳定 时间复杂度:最好,平均O(nlog2n),最差O(n^2) 空间复杂度:O(log2n)
*/
public void quickSort() {
int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,
99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
_quickSort(a, 0, a.length - 1);
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + "--");
System.out.println();
} public void _quickSort(int[] list, int low, int high) {
if (low < high) {
int middle = getMiddle(list, low, high);
_quickSort(list, low, middle - 1);
_quickSort(list, middle + 1, high);
}
} public int getMiddle(int[] list, int low, int high) {
int tmp = list[low];
while (low < high) {
while (low < high && list[high] >= tmp) { high--;
}
list[low] = list[high];
while (low < high && list[low] <= tmp) {
low++;
}
list[high] = list[low];
}
list[low] = tmp;
return low;
} /**
* 堆排序:不稳定 时间复杂度:O(nlog2n) 空间复杂度:O(1)
*/
public void HeapSort() {
int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,
99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
int n = a.length;
int i;
int temp;
InitCreateHeap(a, n);
for (i = n - 1; i > 0; i--) {
temp = a[0];
a[0] = a[i];
a[i] = temp; CreateHeap(a, i, 0);
}
for (i = 0; i < a.length; i++)
System.out.print(a[i] + "--");
System.out.println();
} public void InitCreateHeap(int a[], int n) {
for (int i = (n - 1) / 2; i >= 0; i--)
CreateHeap(a, n, i);
} public void CreateHeap(int a[], int n, int h) {
int i, j, flag;
int temp; i = h;
j = 2 * i + 1;
flag = 0;
temp = a[i]; while (j < n && flag != 1) {
if (j < n - 1 && a[j] < a[j + 1])
j++;
if (temp > a[j])
flag = 1;
else {
a[i] = a[j];
i = j;
j = 2 * i + 1;
}
}
a[i] = temp;
} /**
* 归并排序:稳定 时间复杂度:O(nlog2n) 空间复杂度:O(n)
*/
public void mergingSort() {
int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,
99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
sort(a, 0, a.length - 1);
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + "--");
System.out.println();
} public void sort(int[] data, int left, int right) {
// TODO Auto-generated method stub
if (left < right) {
// 找出中间索引
int center = (left + right) / 2;
// 对左边数组进行递归
sort(data, left, center);
// 对右边数组进行递归
sort(data, center + 1, right);
// 合并
merge(data, left, center, right); }
} public void merge(int[] data, int left, int center, int right) {
// TODO Auto-generated method stub
int[] tmpArr = new int[data.length];
int mid = center + 1;
// third记录中间数组的索引
int third = left;
int tmp = left;
while (left <= center && mid <= right) { // 从两个数组中取出最小的放入中间数组
if (data[left] <= data[mid]) {
tmpArr[third++] = data[left++];
} else {
tmpArr[third++] = data[mid++];
}
}
// 剩余部分依次放入中间数组
while (mid <= right) {
tmpArr[third++] = data[mid++];
}
while (left <= center) {
tmpArr[third++] = data[left++];
}
// 将中间数组中的内容复制回原数组
while (tmp <= right) {
data[tmp] = tmpArr[tmp++];
}
// System.out.println(Arrays.toString(data));
} /**
* 基数排序:稳定 时间复杂度:O(kn),其中n是排序元素个数,k是数字位数。 空间复杂度:O(kn)
*/
public void radixSort() {
int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,
99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
sort(a);
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + "--");
System.out.println();
} public void sort(int[] array) {
// 首先确定排序的趟数;
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
} int time = 0;
// 判断位数;
while (max > 0) {
max /= 10;
time++;
} // 建立10个队列;
List<ArrayList> queue = new ArrayList<ArrayList>();
for (int i = 0; i < 10; i++) {
ArrayList<Integer> queue1 = new ArrayList<Integer>();
queue.add(queue1);
} // 进行time次分配和收集;
for (int i = 0; i < time; i++) { // 分配数组元素;
for (int j = 0; j < array.length; j++) {
// 得到数字的第time+1位数;
int x = array[j] % (int) Math.pow(10, i + 1)
/ (int) Math.pow(10, i);
ArrayList<Integer> queue2 = queue.get(x);
queue2.add(array[j]);
queue.set(x, queue2);
}
int count = 0;// 元素计数器;
// 收集队列元素;
for (int k = 0; k < 10; k++) {
while (queue.get(k).size() > 0) {
ArrayList<Integer> queue3 = queue.get(k);
array[count] = queue3.get(0);
queue3.remove(0);
count++;
}
}
}
} }
Java八大排序算法的更多相关文章
- 必须知道的Java八大排序算法
冒泡排序.简单选择.直接插入.快速排序.堆排序.希尔排序.归并排序.基数排序. 将其按排序方式分类如下图所示: 1.冒泡排序: 基本思想——在要排序的一组数中,对当前还未排好序的范围内的全部数据,自上 ...
- 八大排序算法Java实现
本文对常见的排序算法进行了总结. 常见排序算法如下: 直接插入排序 希尔排序 简单选择排序 堆排序 冒泡排序 快速排序 归并排序 基数排序 它们都属于内部排序,也就是只考虑数据量较小仅需要使用内存的排 ...
- 八大排序算法详解(动图演示 思路分析 实例代码java 复杂度分析 适用场景)
一.分类 1.内部排序和外部排序 内部排序:待排序记录存放在计算机随机存储器中(说简单点,就是内存)进行的排序过程. 外部排序:待排序记录的数量很大,以致于内存不能一次容纳全部记录,所以在排序过程中需 ...
- 八大排序算法总结与java实现(转)
八大排序算法总结与Java实现 原文链接: 八大排序算法总结与java实现 - iTimeTraveler 概述 直接插入排序 希尔排序 简单选择排序 堆排序 冒泡排序 快速排序 归并排序 基数排序 ...
- 八大排序算法 JAVA实现 亲自测试 可用!
今天很高兴 终于系统的实现了八大排序算法!不说了 直接上代码 !代码都是自己敲的, 亲测可用没有问题! 另:说一下什么是八大排序算法: 插入排序 希尔排序 选择排序 堆排序 冒泡排序 快速排序 归并排 ...
- [Data Structure & Algorithm] 八大排序算法
排序有内部排序和外部排序之分,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存.我们这里说的八大排序算法均为内部排序. 下图为排序 ...
- Java常用排序算法+程序员必须掌握的8大排序算法+二分法查找法
Java 常用排序算法/程序员必须掌握的 8大排序算法 本文由网络资料整理转载而来,如有问题,欢迎指正! 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排 ...
- Java 常用排序算法/程序员必须掌握的 8大排序算法
Java 常用排序算法/程序员必须掌握的 8大排序算法 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排序(直接选择排序.堆排序) 4)归并排序 5)分配 ...
- 八大排序算法的 Python 实现
转载: 八大排序算法的 Python 实现 本文用Python实现了插入排序.希尔排序.冒泡排序.快速排序.直接选择排序.堆排序.归并排序.基数排序. 1.插入排序 描述 插入排序的基本操作就是将一个 ...
随机推荐
- 兼容 Spring Boot 1.x 和 2.x 配置类参数绑定的工具类 SpringBootBindUtil
为了让我提供的通用 Mapper 的 boot-starter 同时兼容 Spring Boot 1.x 和 2.x,增加了这么一个工具类. 在 Spring Boot 中,能够直接注入 XXProp ...
- VSTO:使用C#开发Excel、Word【17】
使用Range对象Range对象表示电子表格中的单元格范围.范围可以包含一个单元格,多个连续的单元格,甚至多个不连续的单元格.您可以在Excel中选择时按住Ctrl键选择多个不连续的单元格. 获取特定 ...
- hadoop 设置回收站
我的问题是:hadoop回收站为什么会保留多个过期时间的数据 我们知道hadoop的回收站是在我们删除数据后能恢复的目录,但是我们并不希望在回收站保存太久的数据,我们可以使用如下参数进行配置. ...
- TensorFlow函数:tf.reduce_sum
tf.reduce_sum 函数 reduce_sum ( input_tensor , axis = None , keep_dims = False , name = None , reducti ...
- golang结构体、接口、反射
struct结构体 struct用来自定义复杂数据结构,可以包含多个字段属性,可以嵌套; go中的struct类型理解为类,可以定义方法,和函数定义有些许区别; struct类型是值类型. struc ...
- Javascript中的this指向。
一.JavaScript中的函数 在了解this指向之前,要先弄明白函数执行时它的执行环境是如何创建的,这样可以更清楚的去理解JavaScript中的this指向. function fn(x,y,n ...
- 框架tensorflow2
TensorFlow 2 TensorFlow 激励函数 TensorFlow 添加层: 思考:matmul和multiply两种乘法的区别:http://www.soaringroad.com/?p ...
- 桥接模式-pattern系列
git链接 桥接模式 桥梁模式的用意是"将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化".这句话有三个关键词,也就是抽象化. ...
- C++中的指针,指针函数和函数指针
指针是C或C++中的一大难题,因此弄懂指针对C和C++的学习有很大的帮助,最近一直在研究指针,因此写一篇随笔把心得记录一下. 简单来说指针也是一种变量,只不过指针变量所存储的不是我们直观上看到的,而是 ...
- Python语法教程总结规范
Python语法易错点记录 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享. ...