常见排序算法(附java代码)
常见排序算法与java实现
一、选择排序(SelectSort)
基本原理:对于给定的一组记录,经过第一轮比较后得到最小的记录,然后将该记录与第一个记录的位置进行交换;接着对不包括第一个记录以外的其他记录进行第二次比较,得到最小的记录并与第二个记录进行位置交换;重复该过程,直到进行比较的记录只有一个为止。
public class SelectSort {
public static void selectSort(int[] array) {
int i;
int j;
int temp;
int flag;
for (i = 0; i < array.length; i++) {
temp = array[i];
flag = i;
for (j = i + 1; j < array.length; j++) {
if (array[j] < temp) {
temp = array[j];
flag = j;
}
}
if (flag != i) {
array[flag] = array[i];
array[i] = temp;
}
}
}
public static void main(String[] args) {
int[] a = { 5, 1, 9, 6, 7, 2, 8, 4, 3 };
selectSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
二、插入排序(InsertSort)
基本原理:对于给定的一组数据,初始时假设第一个记录自成一个有序序列,其余记录为无序序列。接着从第二个记录开始,按照记录的大小依次将当前处理的记录插入到其之前的有序序列中,直至最后一个记录插入到有序序列中为止。
public class InsertSort {
public static void insertSort(int[] a) {
if (a != null) {
for (int i = 1; i < a.length; i++) {
int temp = a[i];
int j = i;
if (a[j - 1] > temp) {
while (j >= 1 && a[j - 1] > temp) {
a[j] = a[j - 1];
j--;
}
}
a[j] = temp;
}
}
}
public static void main(String[] args) {
int[] a = { 5, 1, 7, 2, 8, 4, 3, 9, 6 };
// int[] a =null;
insertSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
三、冒泡排序(BubbleSort)
基本原理:对于给定的n个记录,从第一个记录开始依次对相邻的两个记录进行比较,当前面的记录大于后面的记录时,交换位置,进行一轮比较和换位后,n个记录中的最大记录将位于第n位;然后对前(n-1)个记录进行第二轮比较;重复该过程直到进行比较的记录只剩下一个为止。
public class BubbleSort {
public static void bubbleSort(int array[]) {
int temp = 0;
int n = array.length;
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int a[] = { 45, 1, 21, 17, 69, 99, 32 };
bubbleSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
四、归并排序(MergeSort)
基本原理:利用递归与分治技术将数据序列划分成为越来越小的半子表,再对半子表排序,最后再用递归方法将排好序的半子表合并成为越来越大的有序序列。对于给定的一组记录(假设共有n个记录),首先将每两个相邻的长度为1的子序列进行归并,得到n/2(向上取整)个长度为2或1的有序子序列,再将其两两归并,反复执行此过程,直到得到一个有序序列。
public class MergeSort {
public static void merge(int array[], int p, int q, int r) {
int i, j, k, n1, n2;
n1 = q - p + 1;
n2 = r - q;
int[] L = new int[n1];
int[] R = new int[n2];
for (i = 0, k = p; i < n1; i++, k++)
L[i] = array[k];
for (i = 0, k = q + 1; i < n2; i++, k++)
R[i] = array[k];
for (k = p, i = 0, j = 0; i < n1 && j < n2; k++) {
if (L[i] > R[j]) {
array[k] = L[i];
i++;
} else {
array[k] = R[j];
j++;
}
}
if (i < n1) {
for (j = i; j < n1; j++, k++)
array[k] = L[j];
}
if (j < n2) {
for (i = j; i < n2; i++, k++) {
array[k] = R[i];
}
}
}
public static void mergeSort(int array[], int p, int r) {
if (p < r) {
int q = (p + r) / 2;
mergeSort(array, p, q);
mergeSort(array, q + 1, r);
merge(array, p, q, r);
}
}
public static void main(String[] args) {
int a[] = { 5, 4, 9, 8, 7, 6, 0, 1, 3, 2 };
mergeSort(a, 0, a.length - 1);
for (int j = 0; j < a.length; j++) {
System.out.print(a[j] + " ");
}
}
}
五、快速排序(QuickSort)
基本原理:对于一组给定的记录,通过一趟排序后,将原序列分为两部分,其中前一部分的所有记录均比后一部分的所有记录小,然后再依次对前后两部分的记录进行快速排序,递归该过程,直到序列中的所有记录均有序为止。
public class QuickSort {
public static void sort(int array[], int low, int high) {
int i, j;
int index;
if (low >= high)
return;
i = low;
j = high;
index = array[i];
while (i < j) {
while (i < j && index <= array[j])
j--;
if (i < j)
array[i++] = array[j];
while (i < j && index > array[i])
i++;
if (i < j)
array[j--] = array[i];
}
array[i] = index;
sort(array, low, i - 1);
sort(array, i + 1, high);
}
public static void quickSort(int array[]) {
sort(array, 0, array.length - 1);
}
public static void main(String[] args) {
int a[] = { 5, 8, 4, 6, 7, 1, 3, 9, 2 };
quickSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
六、希尔排序(ShellSort)
基本原理:先将待排序的数组元素分成多个子序列,使得每个子序列的元素个数相对减少,然后对各个子序列分别进行直接插入排序,待整个待排序序列"基本有序后",最后再对所有元素进行一次直接插入排序。
public class ShellSort {
public static void shellSort(int[] a) {
int len = a.length;
int i, j;
int h;
int temp;
for (h = len / 2; h > 0; h = h / 2) {
for (i = h; i < len; i++) {
temp = a[i];
for (j = i - h; j >= 0; j -= h) {
if (temp < a[j]) {
a[j + h] = a[j];
} else
break;
}
a[j + h] = temp;
}
}
}
public static void main(String[] args) {
int a[] = { 5, 4, 9, 8, 7, 6, 0, 1, 3, 2 };
shellSort(a);
for (int j = 0; j < a.length; j++) {
System.out.print(a[j] + " ");
}
}
}
七、最小堆排序(MinHeapSort)
基本原理:对于给定的n个记录,初始时把这些记录看作一颗顺序存储的二叉树,然后将其调整为一个小顶堆,然后将堆的最后一个元素与堆顶元素进行交换后,堆的最后一个元素即为最小记录;接着讲前(n-1)个元素重新调整为一个小顶堆,再将堆顶元素与当前堆的最后一个元素进行交换后得到次小的记录,重复该过程直到调整的堆中只剩一个元素时为止,该元素即为最大记录,此时可得到一个有序序列。
public class MinHeapSort {
public static void adjustMinHeap(int[] a, int pos, int len) {
int temp;
int child;
for (temp = a[pos]; 2 * pos + 1 <= len; pos = child) {
child = 2 * pos + 1;
if (child < len && a[child] > a[child + 1])
child++;
if (a[child] < temp)
a[pos] = a[child];
else
break;
}
a[pos] = temp;
}
public static void myMinHeapSort(int[] array) {
int i;
int len = array.length;
for (i = len / 2 - 1; i >= 0; i--) {
adjustMinHeap(array, i, len - 1);
}
for (i = len - 1; i >= 0; i--) {
int tmp = array[0];
array[0] = array[i];
array[i] = tmp;
adjustMinHeap(array, 0, i - 1);
}
}
public static void main(String[] args) {
int[] a = { 5, 4, 9, 8, 7, 6, 0, 1, 3, 2 };
myMinHeapSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
常见排序算法(附java代码)的更多相关文章
- 常见排序算法总结 -- java实现
常见排序算法总结 -- java实现 排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此称为非线性时间比较类排序. 线性时间 ...
- 常见排序算法(java实现)
常见排序算法介绍 冒泡排序 代码: public class BubbleSort { public static void sort(int[] array) { int tValue; for ( ...
- 常见的排序算法之Java代码解释
一 简要介绍 一般排序均值的是将一个已经无序的序列数据重新排列成有序的 常见的排序分为: 1 插入类排序 主要就是对于一个已经有序的序列中,插入一个新的记录.它包括:直接插入排序,折半插入排序和希尔排 ...
- 算法 | Java 常见排序算法(纯代码)
目录 汇总 1. 冒泡排序 2. 选择排序 3. 插入排序 4. 快速排序 5. 归并排序 6. 希尔排序 6.1 希尔-冒泡排序(慢) 6.2 希尔-插入排序(快) 7. 堆排序 8. 计数排序 9 ...
- 几种常见排序算法之Java实现(插入排序、希尔排序、冒泡排序、快速排序、选择排序、归并排序)
排序(Sorting) 是计算机程序设计中的一种重要操作,它的功能是将一个数据元素(或记录)的任意序列,重新排列成一个关键字有序的序列. 稳定度(稳定性)一个排序算法是稳定的,就是当有两个相等记录的关 ...
- 几种常见排序算法的java实现
一.几种常见的排序算法性能比較 排序算法 最好时间 平均时间 最坏时间 辅助内存 稳定性 备注 简单选择排序 O(n^2) O(n^2) O(n^2) O(1) 不稳定 n小时较好 直接插入排序 O( ...
- 常见排序算法及Java实现
先上个总图↓: ①.直接插入排序 插入排序(Insertion Sort)的算法描述是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并 ...
- 常见排序算法及其java实现
最近学习了下java,感觉java在基本语法上与C++非常相似.作为练习,我用java实现了冒泡排序.选择排序.插入排序.基尔排序.快速排序.堆排序.计数排序.合并排序. 以下为实现代码: publi ...
- 常见查找算法(Java代码实现)
一,顺序查找 查找算法中顺序查找算是最简单的了,无论是有序的还是无序的都可以,只需要一个个对比即可,但其实效率很低.我们来看下代码 public static int search(int[] a, ...
随机推荐
- Linux 中我该如何备份系统
系统备份概述 在前面的一些文章中,我反复提到经常会把系统搞崩溃,所以备份系统就是一件不容忽视的事情.由于 Linux 系统本身的优越性,系统的备份和还原还是比较容易的.主要表现在以下方面: Linux ...
- MySQL基础之索引
这段时间看了好多东西却没有总结,今天在这里写一写 关于索引 索引是一种提高查询效率的方法,它是B+树的结构,从根到中间节点在到叶子节点,无需遍历全部就可以查到所需的东西 关于索引的创建 一般有俩种方式 ...
- iOS--高级技术
1.iOS---搜索功能 2.iOS--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook等系统服务开发汇总 3.iOS-技巧性总结 4.iOS-调试技巧 5.iOS-即时通讯- ...
- HTML5_04之SVG绘图
1.关于Canvas绘制图像: 问题:需要绘制多张图片时,必须等待所有图片加载完成才能开始绘制:而每张图片都是异步请求,彼此没有先后顺序,哪一张先加载完成完全无法预测: 方案: var progres ...
- Web中的XHRHttpRequest
1.提出者:Jesse James Garrett 2.IE中,XHR是通过ActiveX对象实现的.涉及浏览器的兼容性写法. 3.使用 <1>open("请求方式&qu ...
- Android自定义控件之自定义属性
前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...
- call,apply,bind的用法
关于call,apply,bind这三个函数的用法,是学习javascript这门语言无法越过的知识点.下边我就来好好总结一下它们三者各自的用法,及常见的应用场景. 首先看call这个函数,可以理解成 ...
- 你真的会玩SQL吗?EXISTS和IN之间的区别
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
- 趣说游戏AI开发:对状态机的褒扬和批判
0x00 前言 因为临近年关工作繁忙,已经有一段时间没有更新博客了.到了元旦终于有时间来写点东西,既是积累也是分享.如题目所示,本文要来聊一聊在游戏开发中经常会涉及到的话题--游戏AI.设计游戏AI的 ...
- c#编程基础之枚举
枚举的意义就在于限制变量取值范围. 当可以确定的几种取值时才可以用. 如果输入一个字符串需要进行判断是否是我们需要的字符串时,则一般需要这样写: using System; using System. ...