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, ...
随机推荐
- 常用开源<监控软件>介绍
转载地址:http://blog.csdn.net/lx_9986/article/details/6803243 一.Zenoss Core Zenoss Core是开源企业级IT管理软件-是智能监 ...
- java静态方法和实例化方法的区别(copy)
[资料来源] http://blog.csdn.net/biaobiaoqi/article/details/6732117 方法是我们每天都在写得,很多程序员大多都使用实例化方法,而很少使用静态方法 ...
- ECMA里面的一元符
只能操作一个值的叫做一元操作符.一元操作符是ECMAScript中最简单的操作符 1.递增和递减操作符 递增和递减操作符直接借鉴自c,而且各有俩个版本,前置型和后置型.顾名思义,前置型就是位于要操作的 ...
- 为什么Markdown能夺得程序员的青睐
Markdown基本情况概述 Markdown是在HTML的基础上产生的.HTML的功能已经足够强大,可以编写出漂亮的网页.HTML在没有CSS的加持上,尽管不能显示出绚丽多彩的网页,但是仍然可以达到 ...
- SpringBoot2.x版本整合SpringSecurity、Oauth2进行password认证
很多人在进行项目开发时都会用到Oauth2.0结合SpringSecurity或者Shiro进行权限拦截以及用户验证,网上也有很多的案例,前几天项目里边需要用到,顺便整合了进来,特此写篇博客,记录下过 ...
- linux守护进程的编写
linux监控一个进程进行 代码如下: #!/bin/sh cd /home/autoprocess/ auto=`pgrep -f autoProcessNew.php | wc -l` if [ ...
- 洛谷P5055 【模板】可持久化文艺平衡树(FHQ Treap)
题面 传送门 题解 日常敲板子.jpg //minamoto #include<bits/stdc++.h> #define R register #define inline __inl ...
- RHEL5.6环境下yum安装MySQL
RHEL5.6环境下yum安装MySQL记录,2017年2月20日 1.卸载原有的MySQL rpm -qa命令查询是否安装了MySQL [root@localhost mysql]# rpm -qa ...
- kafka的topic命名技巧
不多说,直接上干货! 比如,我们给kafka的topic命名为user_r2p10 表示user这个topic的副本因子(r)是2,分区数(p)是10. 这样后期在写消费者代码的时候,根据top ...
- spring-redis-data的一个坑
事故原因: 运维报告redis内存直线上升,然后查询发现都是setrange操作,review代码,没法发现setrange操作 代码如下: redisTemplate.opsForValue().s ...