小编在这篇博客中对JAVA程序员需要了解的排序算法做一个介绍并附带实现代码,希望可以对你们有所帮助,若有所不足请在下方继续评论,谢谢支持。
算法的分类
1.交换排序 (冒泡排序,快速排序)
2.插入排序 (直接插入排序,希尔排序
3.选择排序 (直接选择排序,堆排序)
4.归并排序
5.分配排序 (基数排序)
冒泡排序
冒泡排序(Bubble Sort)是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把
他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小
的元素会经由交换慢慢“浮”到数列的顶端。
package MaoPao;
public class DemoFor_MaoPao {
/**
* 冒泡排序
* 首先冒泡排序是java的一种基本排序算法,他的执行原理是什么呢?
* 答:首先第一步,他会比较相邻的两个元素,if num1>num2 那么就利用换手的方式将他们交互。
* 第二步,他会对所有这样的元素进行比较大小然后换位,在这一步呢,最后的那个元素就是整个数值中最大的哪一个。
* 第三步,针对全部元素进行重复执行,除了刚刚最后一个最大的数字。
* 第四步,一直重复执行排序,直到数字被排好为止。
*
*
*/
public static void main(String[] args) {
int array[]={12,8,20,25,50,0};
for (int i = 0; i < array.length-1; i++) {
for (int j = 0; j < array.length-i-1; j++) {
//单次循环 只排序一轮
if (array[j] > array[j+1]) {
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
System.out.print("第"+i+"次排序:"+"\n");
for (int a = 0; a < array.length; a++) {
System.out.print(array[a]+ "\t");
}
System.out.println("");
}
System.out.print("最终排序结果!"+"\n");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+ "\t");
}
}
/* int score[] = {67, 69, 10, 87, 20, 90, 99, 0};
for (int i = 0; i < score.length -1; i++){ //最多做n-1趟排序
for(int j = 0 ;j < score.length - i - 1; j++){ //对当前无序区间score[0......length-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的)
if(score[j] > score[j + 1]){ //把小的值交换到后面
int temp = score[j];
score[j] = score[j + 1];
score[j + 1] = temp;
}
}
System.out.print("第" + (i + 1) + "次排序结果:");
for(int a = 0; a < score.length; a++){
System.out.print(score[a] + "\t");
}
System.out.println("");
}
System.out.print("最终排序结果:");
for(int a = 0; a < score.length; a++){
System.out.print(score[a] + "\t");
}
}*/
}
插入排序
插入简介。
插入排序法,插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据,
所有元素,但将最后一个元素除外(让数组多一个空间才有插入的位置),而第二部分就只包含这一个元素(即待插入元素)。在第一部分
排序完成后,再将这个最后元素插入到已排好序的第一部分中。
插入排序的思想。
插入排序的基本思想是:每步将一个待排序的纪录,按其关键码值的大小插入前面已经排序的文件中
适当位置上,直到全部插入完为止。
package ChaRu;
public class DemoFor_ChaRu {
public static int count = 0;
public static void main(String[] args) {
int[] data = new int[] { 5, 3, 6, 2, 1, 9, 4, 8, 7 };
print(data);
insertSort(data);
print(data);
}
public static void insertSort(int[] data) {
for (int i = 1; i < data.length; i++) {
// 缓存i处的元素值
int tmp = data[i];
if (data[i] < data[i - 1]) {
int j = i - 1;
// 整体后移一格
while (j >= 0 && data[j] > tmp) {
data[j + 1] = data[j];
j--;
}
// 最后将tmp插入合适的位置
data[j + 1] = tmp;
print(data);
}
}
}
public static void print(int[] data) {
for (int i = 0; i < data.length; i++) {
System.out.print(data[i] + "\t");
}
System.out.println();
}
}
快速 排序
快速排序简介。
快速排序(Quicksort)是对冒泡排序的一种改进。
快速排序的思想。
它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,
package KuaiSu;
public class DemoFor_KuaiSu {
/**
* 快速排序的思想——分治法:简单说就是比大小在分区
* 快速排序的步骤:
* 1.在数组中取一个数作为基准数,
* 2.将比这个数大或等于的数都放在他的右边,将比他小的数都放在他的左边。
* 3.在对左右区间重复第二步,直到各区间只有一个数。
* */
/**
* 分区并返回索引值
* */
public int getMiddle(Integer[] list, int low, int high) {
//挖第一个坑位 tmp就是基准数
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; //返回中轴的位置 返回索引值
}
public void _quickSort(Integer[] list, int low, int high) {
if (low < high) {
int middle = getMiddle(list, low, high); //将list数组进行一分为二 分区索引位
_quickSort(list, low, middle - 1); //对低字表进行递归排序
_quickSort(list, middle + 1, high); //对高字表进行递归排序 然后进行递归调用即可
}
}
public void quick(Integer[] str) {
if (str.length > 0) { //查看数组是否为空
_quickSort(str, 0, str.length - 1);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer[] list={34,3,53,2,23,7,14,10};
DemoFor_KuaiSu qs=new DemoFor_KuaiSu(); //创建快速排序类的对象
qs.quick(list);
for(int i=0;i
选择排序
选择排序简介。
元素,存放在序列的起始位置,直到全部待排序的数据元素排完。 选择排序是不稳定的排序方法(比如序列[5, 5, 3]第一次就将
第一个[5]与[3]交换,导致第一个5挪动到第二个5后面)。
package XuanZhe;
public class DemoFor_XuanZhe {
public static void _choiceSort(Integer[] a) {
if (a == null || a.length <= 0) {
return;
}
for (int i = 0; i < a.length; i++) {
int min = i; /* 将当前下标定义为最小值下标 */
for (int j = i + 1; j < a.length; j++) {
if (a[min] > a[j]) { /* 如果有小于当前最小值的关键字 */
min = j; /* 将此关键字的下标赋值给min */
}
}
if (i != min) {/* 若min不等于i,说明找到最小值,交换 */
int tmp = a[min];
a[min] = a[i];
a[i] = tmp;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer[] list={49,38,65,97,76,13,27,14,10};
//快速排序
/* QuicSort qs=new QuicSort();
qs.quick(list);*/
//冒泡排序
/* BubbleSort bs=new BubbleSort();
bs.bubble(list);*/
//选择排序
DemoFor_XuanZhe._choiceSort(list);
for(int i=0;i
本篇博客到此为止,剩下没有提到的排序算法 会在下一篇博客中在进行介绍。谢谢浏览!
插入排序