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

一、冒泡排序

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. ‘CONFIG_ENV_SIZE’未声明(不在函数内【转】

    本文转载自: http://bbs.csdn.net/topics/390678466 见论坛讨论.可以临时修复.

  2. android 3G移植【转】

    本文转载自:http://blog.csdn.net/hanmengaidudu/article/details/17028383 一 开发环境简介 内容 说明 3G模块 华为EM820W(WCDMA ...

  3. rspec

    require 'rails_helper' RSpec.describe Jira, '#set_jira_jlist' do it "this sentence is after it& ...

  4. js数值型遇0开始自动转换为8进制

    如题,今天在项目更新时发现了js的这个自动转换问题,代码如下: var num = 0110; render:function(num){       var html="<a hre ...

  5. 关于file文件操作的头文件 【LINUX】 (转载)

    转自:http://blog.csdn.net/figo77ll/article/details/3156052 Linux下如果要对文件进行读取访问,需要包含至少以下两个头文件: #inlcude ...

  6. Eclipse使用Tomcat发布项目时出现YadisException异常解决方案

    调整使用Eclipse的JDK版本,大概JDK版本过低会出现这个org.openid4java.discovery.yadis.YadisException: 0x704: I/O transport ...

  7. bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞【spfa判负环】

    tag是假的,用了及其诡异的方法判负环 正权无向边和负权有向边的图 #include<iostream> #include<cstdio> #include<cstrin ...

  8. 10.11 NOIP模拟题(1)

    /* 离散化 差分 */ #include<bits/stdc++.h> #define N 4000007 using namespace std; int n,ans; int tmp ...

  9. AutoCAD VBA添加菜单

    # 给cad添加自定义菜单 Private Sub AddBar() Dim NewMenuItem As AcadPopupMenuItem Dim TheMacro As String Dim M ...

  10. c#自定义ORM框架---(泛型&反射&实体类扩展属性<附带通用增、删、查、改>)

    该教材主要是运用到泛型.反射和实体类扩展属性 步骤一.建立扩展属性类 实体类扩展属性要继承Attribute基类完成 [AttributeUsage(AttributeTargets.Property ...