注:以下排序均为从小到大

一、冒泡排序

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之三大基础排序(冒泡、选择、插入)的更多相关文章

  1. python 数据结构与算法之排序(冒泡,选择,插入)

    目录 数据结构与算法之排序(冒泡,选择,插入) 为什么学习数据结构与算法: 数据结构与算法: 算法: 数据结构 冒泡排序法 选择排序法 插入排序法 数据结构与算法之排序(冒泡,选择,插入) 为什么学习 ...

  2. 基本排序-冒泡/选择/插入(python)

    # -*- coding: utf-8 -*- import random def bubble_sort(seq): n = len(seq) for i in range(n-1): print( ...

  3. C++学习(三十八)(C语言部分)之 排序(冒泡 选择 插入 快排)

    算法是解决一类问题的方法排序算法 根据元素大小关系排序 从小到大 从大到小冒泡 选择 插入 快排希尔排序 归并排序 堆排序 冒泡排序 从头到尾比较 每一轮将最大的数沉底 或者最小数字上浮 选择排序 1 ...

  4. java 整型数组基本排序,冒泡,快速选择,插入,归并

    在学java泛型,于是把排序拿来练练手了 import java.util.Arrays; public class GenericArraySort { public static void mai ...

  5. Java数据结构与算法(2) - ch03排序(冒泡、插入和选择排序)

    排序需要掌握的有冒泡排序,插入排序和选择排序.时间为O(N*N). 冒泡排序: 外层循环从后往前,内存循环从前往后到外层循环,相邻数组项两两比较,将较大的值后移. 插入排序: 从排序过程的中间开始(程 ...

  6. java面试准备之基础排序——冒泡与选择排序

    选择排序:     [java]    public void select(int[] arr){            for(int i=0;i<arr.length;i++){      ...

  7. python 中的一些基础算法:递归/冒泡/选择/插入

    递归算法 如果一个函数包含了对自己的调用,那么这个函数就是递归的. 比如我们计算下1-7乘法的计算: def func(n): if n ==1 : return 1 return n*func(n- ...

  8. 【数据结构 C++】排序——冒泡、插入、选择、希尔、归并、快排、堆排序

    LeetCode 912. 排序数组 给你一个整数数组 nums,请你将该数组升序排列. 示例 1: 输入:nums = [5,2,3,1] 输出:[1,2,3,5] 示例 2: 输入:nums = ...

  9. 三大基础排序算法BubbleSort、SelectSort、InsertSort

    public class Strategy { public static void main(String[] args) { int [] array=new int[]{26,25,15,42, ...

随机推荐

  1. c++运算符重载以及一些基本概念

    c++primer第四版435 1.赋值( = ), 下标( [ ] ) ,调用 (  ( )  ), 成员訪问箭头 (->)等操作符必须定义为成员,定义为非成员时,编译器报错 2. 像赋值一样 ...

  2. 洛谷P2668 斗地主==codevs 4610 斗地主[NOIP 2015 day1 T3]

    P2668 斗地主 326通过 2.6K提交 题目提供者洛谷OnlineJudge 标签搜索/枚举NOIp提高组2015 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 出现未知错误是说梗啊 ...

  3. ldd LD_TRACE_LOADED_OBJECTS

    1 该环境变量设置为1的话,只会打印所执行的程序的依赖,即所依赖的动态链接库

  4. 如何配置MYSQL的MASTER---SLAVE复制备份?

    如何配置MYSQL的MASTER---SLAVE复制备份? 一.配置一个mysql服务器做master:     在配置文件my.ini中添加如下内容: log-bin=matster-binlog- ...

  5. fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt

    project->xx Properties->Manifest->Input and Output->Embed Manifest将yes修改为no

  6. wtpwebapps下没有相应的文件

    右击clean

  7. ZOJ1081 Points Within 点和多边形的位置关系

    ZOJ1081 给一个点和一个多边形 判断点在多边形内(边上)还是在多边形外 在多边形外的点引一条射线必然穿过多边形的两条边 而在多边形内的点则不一定. 当然凹多边形有特殊情况 但是总能找到对应位置关 ...

  8. android 手机上运行图像算法

    在pc上调试好的图像处理算法想要在android手机上跑一下看看速度需要一下几个步骤 1.建立一个android application,通过ndk调用你写好的图像算法的c/c++ code 2. 然 ...

  9. codeforces AIM Tech Round 4 div 2

    A:开个桶统计一下,但是不要忘记k和0比较大小 #include<bits/stdc++.h> using namespace std; ]; ]; int main() { int k; ...

  10. vs code 快速生成vue 模板

    vs code 快速生成vue 模板 1.使用快捷Ctrl + Shift + P唤出控制台,然后输入snippets并选择.(或 文件>首选项>用户代码片断里面,输入 vue.json ...