Java之三大基础排序(冒泡、选择、插入)
注:以下排序均为从小到大
一、冒泡排序
package com.yunche.testsort; import java.util.Arrays; /**
* @ClassName: BubbleSort
* @Description:
* @author: yunche
* @date: 2018/11/30
*/
public class BubbleSort {
public static void main(String[] args) {
int[] a = {1, 7, 3, 3, 5, 4, 6, 2, 8};
new BubbleSort().sort(a);
System.out.println(Arrays.toString(a));
}/*Output:
[1, 2, 3, 3, 4, 5, 6, 7, 8]
*/ private void sort(int[] a) {
int len = a.length;
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - i - 1; j++) {
//swap
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
}
二、选择排序
package com.yunche.testsort; import java.util.Arrays; /**
* @ClassName: SelectionSort
* @Description:
* @author: yunche
* @date: 2018/11/30
*/
public class SelectionSort {
public static void main(String[] args) {
int[] a = {2, 1, 3, 5, 3, 4, 10, 7, 6};
new SelectionSort().sort(a);
System.out.println(Arrays.toString(a));
}/*
Output:[1, 2, 3, 3, 4, 5, 6, 7, 10]
*/ private void sort(int[] a) {
int len = a.length;
for (int i = 0; i < len - 1; i++) {
//select max
int index = 0;
int j;
for (j = 1; j < len - i; j++) {
index = a[index] < a[j] ? j : index;
}
//swap
int temp = a[index];
a[index] = a[j - 1];
a[j - 1] = temp;
}
} }
三、插入排序
package com.yunche.testsort; import java.util.Arrays; /**
* @ClassName: InsertionSort
* @Description:
* @author: yunche
* @date: 2018/11/30
*/
public class InsertionSort {
public static void main(String[] args) {
int[] a = {2, 1, 3, 1, 7, 4, 5, 3, 8, 6};
new InsertionSort().sort(a);
System.out.println(Arrays.toString(a));
}/*
Output:[1, 1, 2, 3, 3, 4, 5, 6, 7, 8]
*/ private void sort(int[] a) {
//将当前元素插入到左侧已经排好序的数组中,使之依然有序
int len = a.length;
for (int i = 1; i < len; i++) {
for (int j = i; j > 0; j--) {
//使当前元素找到属于自己的位置
if (a[j] < a[j - 1]) {
int temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
}
}
}
}
}
Java之三大基础排序(冒泡、选择、插入)的更多相关文章
- python 数据结构与算法之排序(冒泡,选择,插入)
目录 数据结构与算法之排序(冒泡,选择,插入) 为什么学习数据结构与算法: 数据结构与算法: 算法: 数据结构 冒泡排序法 选择排序法 插入排序法 数据结构与算法之排序(冒泡,选择,插入) 为什么学习 ...
- 基本排序-冒泡/选择/插入(python)
# -*- coding: utf-8 -*- import random def bubble_sort(seq): n = len(seq) for i in range(n-1): print( ...
- C++学习(三十八)(C语言部分)之 排序(冒泡 选择 插入 快排)
算法是解决一类问题的方法排序算法 根据元素大小关系排序 从小到大 从大到小冒泡 选择 插入 快排希尔排序 归并排序 堆排序 冒泡排序 从头到尾比较 每一轮将最大的数沉底 或者最小数字上浮 选择排序 1 ...
- java 整型数组基本排序,冒泡,快速选择,插入,归并
在学java泛型,于是把排序拿来练练手了 import java.util.Arrays; public class GenericArraySort { public static void mai ...
- Java数据结构与算法(2) - ch03排序(冒泡、插入和选择排序)
排序需要掌握的有冒泡排序,插入排序和选择排序.时间为O(N*N). 冒泡排序: 外层循环从后往前,内存循环从前往后到外层循环,相邻数组项两两比较,将较大的值后移. 插入排序: 从排序过程的中间开始(程 ...
- java面试准备之基础排序——冒泡与选择排序
选择排序: [java] public void select(int[] arr){ for(int i=0;i<arr.length;i++){ ...
- python 中的一些基础算法:递归/冒泡/选择/插入
递归算法 如果一个函数包含了对自己的调用,那么这个函数就是递归的. 比如我们计算下1-7乘法的计算: def func(n): if n ==1 : return 1 return n*func(n- ...
- 【数据结构 C++】排序——冒泡、插入、选择、希尔、归并、快排、堆排序
LeetCode 912. 排序数组 给你一个整数数组 nums,请你将该数组升序排列. 示例 1: 输入:nums = [5,2,3,1] 输出:[1,2,3,5] 示例 2: 输入:nums = ...
- 三大基础排序算法BubbleSort、SelectSort、InsertSort
public class Strategy { public static void main(String[] args) { int [] array=new int[]{26,25,15,42, ...
随机推荐
- java图片处理工具之-ImageMagick+jmagick(二)
简单的图片处理測试类: public class ImageUtil { static{ System.setProperty("jmagick.systemclassl ...
- javascript里的prototype
在javascript中,prototype是函数的一个固有属性,其他对象,比如字符串什么的,并没有这个属性. 这个属性做什么用呢? 1.用于该函数的所有实例进行共享 比如,共同的属性,共同的方法.类 ...
- ROUND function and arithmetic overflow
遇到如下错误 Arithmetic overflow error converting expression to data type numeric. ), ); https://stackover ...
- Oracle存储过程(增、删、改)写法、oracle执行存储过程
Oracle存储过程(增.删.改)写法 发布时间: 2010-3-24 11:07 作者: ZHF 来源: 51Testing软件测试网采编 字体: 小 中 大 | 上一篇 下一篇 ...
- bzoj 1800: [Ahoi2009]fly 飞行棋【枚举】
在圆里所以没有平行四边形,n^4枚举点即可 #include<iostream> #include<cstdio> using namespace std; const int ...
- 乐字节Java8核心特性之方法引用
大家好,我是乐字节的小乐,上一次我们说到了Java8核心特性之函数式接口,接下来我们继续了解Java8又一核心特性--方法引用. Java8 中引入方法引用新特性,用于简化应用对象方法的调用, 方法引 ...
- Akka源码分析-Akka-Streams-Materializer(1)
本博客逐步分析Akka Streams的源码,当然必须循序渐进,且估计会分很多篇,毕竟Akka Streams还是比较复杂的. implicit val system = ActorSystem(&q ...
- 2017青岛网络赛1008 Chinese Zodiac
Chinese Zodiac Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) T ...
- 二分图最大匹配(匈牙利算法) POJ 3020 Antenna Placement
题目传送门 /* 题意:*的点占据后能顺带占据四个方向的一个*,问最少要占据多少个 匈牙利算法:按坐标奇偶性把*分为两个集合,那么除了匹配的其中一方是顺带占据外,其他都要占据 */ #include ...
- E - Cheap Kangaroo(求多个数的最大公约数)
Description There are N kangaroos going out to eat at an Indian restaurant. The ith kangaroo wants t ...