常见排序算法(java实现)
常见排序算法介绍
冒泡排序
- 代码:
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
參考资料:
常见排序算法(java实现)的更多相关文章
- 常见排序算法 - Java实现
1.冒泡排序 每次比较相邻的两个元素大小,调整顺序.从头到尾执行一轮(i),最大数值的元素就排到最后.每次从头到尾执行一轮,都会排好一个元素(length - i - 1).这就是说一个包含 n 个元 ...
- 常见排序算法JAVA实现
1.冒泡排序,时间复杂度:最好:T(n) = O(n) ,情况:T(n) = O(n2) ,平均:T(n) = O(n2) public int[] bubbleSort(int[] nums) { ...
- 常见排序算法(附java代码)
常见排序算法与java实现 一.选择排序(SelectSort) 基本原理:对于给定的一组记录,经过第一轮比较后得到最小的记录,然后将该记录与第一个记录的位置进行交换:接着对不包括第一个记录以外的其他 ...
- 常见排序算法总结 -- java实现
常见排序算法总结 -- java实现 排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此称为非线性时间比较类排序. 线性时间 ...
- 几种常见排序算法之Java实现(插入排序、希尔排序、冒泡排序、快速排序、选择排序、归并排序)
排序(Sorting) 是计算机程序设计中的一种重要操作,它的功能是将一个数据元素(或记录)的任意序列,重新排列成一个关键字有序的序列. 稳定度(稳定性)一个排序算法是稳定的,就是当有两个相等记录的关 ...
- 常见排序算法题(java版)
常见排序算法题(java版) //插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sor ...
- Java基础语法(8)-数组中的常见排序算法
title: Java基础语法(8)-数组中的常见排序算法 blog: CSDN data: Java学习路线及视频 1.基本概念 排序: 是计算机程序设计中的一项重要操作,其功能是指一个数据元素集合 ...
- 常见排序算法总结(java版)
一.冒泡排序 1.原理:相邻元素两两比较,大的往后放.第一次完毕,最大值在最大索引处. 即使用相邻的两个元素一次比价,依次将最大的数放到最后. 2.代码: public static void bub ...
- 八大排序算法Java实现
本文对常见的排序算法进行了总结. 常见排序算法如下: 直接插入排序 希尔排序 简单选择排序 堆排序 冒泡排序 快速排序 归并排序 基数排序 它们都属于内部排序,也就是只考虑数据量较小仅需要使用内存的排 ...
随机推荐
- Maven搭建Spring Security3.2项目详解
本来是打算在上一篇SpringMVC+Hibernate上写的,结果发现上面那篇 一起整合的,结果发现上一篇内容实在是太长了,就另起一篇,这篇主要是采用 Maven搭建Spring+SpringMVC ...
- 【AtCoder Regular Contest 082 F】Sandglass
[链接]点击打开链接 [题意] 你有一个沙漏. 沙漏里面总共有X单位的沙子. 沙漏分A,B上下两个部分. 沙漏从上半部分漏沙子到下半部分. 每个时间单位漏1单位的沙子. 一开始A部分在上面.然后在r1 ...
- PatentTips - Use of multiple virtual machine monitors to handle privileged events
BACKGROUND OF THE INVENTION A conventional virtual-machine monitor (VMM) typically runs on a compute ...
- LeetCode Algorithm 02_Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- pragma pack,字节对齐
关于字节对齐 pragma pack 一. 测试代码: // packTest.cpp : Defines the entry point for the console application. / ...
- 使用BeautifulSoup爬取“0daydown”站点的信息(2)——字符编码问题解决
上篇中的程序实现了抓取0daydown最新的10页信息.输出是直接输出到控制台里面.再次改进代码时我准备把它们写入到一个TXT文档中.这是问题就出来了. 最初我的代码例如以下: #-*- coding ...
- java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: student is not mapped
Spring 5.0 +Jpa,使用@Query实现 自定义查询报错: java.lang.IllegalArgumentException: org.hibernate.hql.internal.a ...
- 【BZOJ 2754】[SCOI2012]喵星球上的点名
[链接]h在这里写链接 [题意] n个人; 由姓和名组成.s1[i]和s2[i]; 有m个询问串. 问你第j个询问串,是否为某个人的姓或者名的子串. 如果是的话 ...
- mysql 存相同内容:utb8mb4 会比 utf8 占用更多的内存吗,utf8mb4 浪费内存吗?utf8 utf8mb4 区别
原文:mysql 存相同内容:utb8mb4 会比 utf8 占用更多的内存吗,utf8mb4 浪费内存吗?utf8 utf8mb4 区别 参考:http://www.fengyunxiao.cn u ...
- spring项目启动后,获取bean的方法总结
如果在web项目中,用到定时器的朋友可能会遇到使用spring注解的方式获取bean的时候报空指针的异常.这是就可以使用手工的方法获取spring容器中的bean了. 下面是具体的方法: 1.先说一个 ...