常见排序算法介绍

冒泡排序

  • 代码:
public class BubbleSort {

    public static void sort(int[] array) {
int tValue;
for (int i = 0; i < array.length; i++) {
for (int j = i; j < array.length; j++) {
if (array[i] > array[j]) {
tValue = array[i];
array[i] = array[j];
array[j] = tValue;
}
}
}
} public static void main(String[] args) {
int a[] = { 6, 2, 4, 5, 1, 9, 10, 7 };
sort(a);
for (int v : a) {
System.out.print(v + " ");
}
}
}

输入结果:

1 2 4 5 6 7 9 10

插入排序:

  • 效果图:

  • 代码:

public class InsertSort {
public static void sort(int[] array) {
int tValue;
int j;
for (int i = 0; i < array.length; i++) {
j = i;
tValue = array[i];
while (j > 0 && tValue < array[j - 1]) {
array[j] = array[j - 1];
j--;
}
array[j] = tValue;
}
} public static void main(String[] args) {
int a[] = { 6, 2, 4, 5, 1, 9, 10, 7 };
sort(a);
for (int v : a) {
System.out.print(v + " ");
}
}
}

输入结果:

1 2 4 5 6 7 9 10

选择排序:

  • 代码:
public class SelectSort {
public static void sort(int[] array) {
int index, tValue;
for (int i = 0; i < array.length; i++) {
index = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[index]) {
index = j;
}
}
if (index != i) {
tValue = array[i];
array[i] = array[index];
array[index] = tValue;
}
}
} public static void main(String[] args) {
int a[] = { 6, 2, 4, 5, 1, 9, 10, 7 };
sort(a);
for (int v : a) {
System.out.print(v + " ");
}
}
}

输入结果:

1 2 4 5 6 7 9 10

高速排序:

  • 代码:
public class QuickSort {
public static void quickSort(int[] array, int left, int right) {
int i, j, bValue, tValue;
if (left > right) {
return;
}
i = left;
j = right;
bValue = array[left];
while (i != j) {
while (array[j] >= bValue && i < j) {
j--;
}
while (array[i] <= bValue && i < j) {
i++;
}
if (i < j) {
tValue = array[i];
array[i] = array[j];
array[j] = tValue;
}
} array[left] = array[i];
array[i] = bValue; quickSort(array, left, i - 1);
quickSort(array, i + 1, right);
} public static void main(String[] args) {
int a[] = { 6, 2, 4, 5, 1, 9, 10, 7 };
quickSort(a, 0, a.length - 1);
for (int v : a) {
System.out.print(v + " ");
}
}
}

输入结果:

1 2 4 5 6 7 9 10

參考资料:

http://blog.jobbole.com/11745/

常见排序算法(java实现)的更多相关文章

  1. 常见排序算法 - Java实现

    1.冒泡排序 每次比较相邻的两个元素大小,调整顺序.从头到尾执行一轮(i),最大数值的元素就排到最后.每次从头到尾执行一轮,都会排好一个元素(length - i - 1).这就是说一个包含 n 个元 ...

  2. 常见排序算法JAVA实现

    1.冒泡排序,时间复杂度:最好:T(n) = O(n) ,情况:T(n) = O(n2) ,平均:T(n) = O(n2) public int[] bubbleSort(int[] nums) { ...

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

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

  4. 常见排序算法总结 -- java实现

    常见排序算法总结 -- java实现 排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此称为非线性时间比较类排序. 线性时间 ...

  5. 几种常见排序算法之Java实现(插入排序、希尔排序、冒泡排序、快速排序、选择排序、归并排序)

    排序(Sorting) 是计算机程序设计中的一种重要操作,它的功能是将一个数据元素(或记录)的任意序列,重新排列成一个关键字有序的序列. 稳定度(稳定性)一个排序算法是稳定的,就是当有两个相等记录的关 ...

  6. 常见排序算法题(java版)

    常见排序算法题(java版) //插入排序:   package org.rut.util.algorithm.support;   import org.rut.util.algorithm.Sor ...

  7. Java基础语法(8)-数组中的常见排序算法

    title: Java基础语法(8)-数组中的常见排序算法 blog: CSDN data: Java学习路线及视频 1.基本概念 排序: 是计算机程序设计中的一项重要操作,其功能是指一个数据元素集合 ...

  8. 常见排序算法总结(java版)

    一.冒泡排序 1.原理:相邻元素两两比较,大的往后放.第一次完毕,最大值在最大索引处. 即使用相邻的两个元素一次比价,依次将最大的数放到最后. 2.代码: public static void bub ...

  9. 八大排序算法Java实现

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

随机推荐

  1. .net core 修改网站启动端口

    原文:.net core 修改网站启动端口 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yenange/article/details/81675 ...

  2. CSS3实现的立体button

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Android 蓝牙扫描代码

    /** * Created by rbq on 2016/11/1. */ import android.bluetooth.BluetoothAdapter; import android.blue ...

  4. 特征描述子(feature descriptor) —— HOG(方向梯度直方图)

    HOG(Histogram of Oriented Gradients),描述的是图像的局部特征,其命名也暗示了其计算方法,先计算图像中某一区域不同方向上梯度的值,然后累积计算频次,得到直方图,该直方 ...

  5. [WPF自定义控件库]使用TextBlockHighlightSource强化高亮的功能,以及使用TypeConverter简化调用

    1. 强化高亮的功能 上一篇文章介绍了使用附加属性实现TextBlock的高亮功能,但也留下了问题:不能定义高亮(或者低亮)的颜色.为了解决这个问题,我创建了TextBlockHighlightSou ...

  6. 云服务器搭建 Nginx 静态网站

    第一步:安装 Nginx 在 CentOS 上,可直接使用 yum 来安装 Nginx(当然也可以通过下载压缩包.解压.编译的方式安装,不过太麻烦了) yum install nginx -y 第二步 ...

  7. struts2漏洞原理及解决的方法

    1.原理 Struts2的核心是使用的webwork框架,处理 action时通过调用底层的getter/setter方法来处理http的參数,它将每一个http參数声明为一个ONGL(这里是ONGL ...

  8. stm32的adc时钟周期,ADC_SampleTime_1Cycles5是多长时间

  9. 26、从零写UVC驱动之分析描述符

    指令:lsusb 可以查看usb设备的描述符信息,当然lsusb指令要带一些参数 一个usb设备有多个config配置+设备描述符,一个config有多个接口和association.config描述 ...

  10. 小米笔记本(13.3 I7) ubuntu14.04下网卡驱动安装

    ubuntu 内核升级到4.6.4(更高版本可能造成系统无法启动) kernel debian包下载地址 http://kernel.ubuntu.com/~kernel-ppa/mainline/v ...