常见排序算法介绍

冒泡排序

  • 代码:
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. python之经典猜数字

    题目:猜数字1.让用户输入1-20,猜数字,可以猜5次.2.每次有提示,大了,或者小了!3.如果超过5次,提示game over. # !/usr/bin/env python # -*- codin ...

  2. Mongodb总结2-Java版本的HelloWorld-CRUD例子

    2013年,写的CRUD太简单了,今天在原来的基础上,稍微完善了下,用了更多语法,比如排序sort.in语句等. 参考了<Mongodb权威指南-第1版-高清>,等下上传到CSDN下载频道 ...

  3. 洛谷 P2807 三角形计数

    P2807 三角形计数 题目背景 三角形计数(triangle) 递推 题目描述 把大三角形的每条边n等分,将对应的等分点连接起来(连接线分别平行于三条边),这样一共会有多少三角形呢?编程来解决这个问 ...

  4. 初探springmvc

    Springmvc是什么 Springmvc(spring web mvc)是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进 ...

  5. 标准模板库 STL 使用之 —— vector 使用 tricks

    1. 从已有 vector(或数组)中复制 vector<int> a{....}; int an = a.size(); int half = an/2; vector<int&g ...

  6. arduino串口输出问题

  7. css结构设计思想

    本文摘自博客园-予沁安的文章:结构化CSS设计思维,作为学习笔记记录一下 1.LESS.SASS等预处理器给CSS开发带来了语法的灵活和便利,其本身却没有给我们带来结构化设计思维.很少有人讨论CSS的 ...

  8. AS2.0鼠标尾随和拖动代码

    1,鼠标尾随. a: Mouse.hide();//隐藏鼠标.Mouse.show()显示鼠标. MC1.startDrag(true);//直接利用函数实现. b: Mouse.hide(); on ...

  9. jquery-validate使用.md

    html <form id="s_form" class="form-horizontal" action="http://www.baidu. ...

  10. HDU 1874 畅通工程续 SPFA || dijkstra||floyd

    http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目大意: 给你一些点,让你求S到T的最短路径. 我只是来练习一下SPFA的 dijkstra+邻接矩阵 ...