以下为集中排序的java代码实现(部分是在引用别人代码):

插入排序(InsertSort):

//代码原理
public static void iSort(int[] a){
for(int i = 1;i < a.length; i++){
if(a[i] < a[i-1]){
int temp = a[i];
while(temp < a[i-1]){
a[i] = a[i-1];
if(i-1 > 0){
i--;
}else{
break;
}
}
a[i-1] = temp;
}
}
}
//精简代码1
public static void iSort2(int[] a){
for(int i = 1;i < a.length; i++){
if(a[i] < a[i-1]){
int temp = a[i];
while(temp < a[i-1]){
a[i] = a[i-1];
if(i-1 > 0){
i--;
}else{
break;
}
}
a[i-1] = temp;
}
}
}
//精简代码2
for(current=1;current<=arr.length-1;current++){
//每当current变化,key和before都随之变化
key = arr[current];
before = current-1;//红色有序索引第一个值。 while(before>=0 && arr[before]>key){ arr[before+1] = arr[before];//大值向后移一位
before--;
} //插入点:before+1
arr[before+1] = key;
}

冒泡排序(BubbletSort):

public class BubbleSort {
public static int[] bSort(int[] a){
for(int j = a.length-1; j >= 1; j--){
// 如果flag没有变为false那么证明数组本身有序
boolean flag = true;
for(int i = 0; i <= j-1; i++){
if(a[i] > a[i+1]){
int temp = a[i];
a[i+1] = a[1];
a[i] = temp;
flag = false;
}
}
if(flag)
break;
}
return a;
}
}

选择排序(SelectionSort):

/*
* 选择排序基本思路:
* 把第一个元素依次和后面的所有元素进行比较。
* 第一次结束后,就会有最小值出现在最前面。
* 依次类推
*/
public class SelectionSort {
public static void sort(int[] data) {
for (int x = 0; x < data.length - 1; x++) {
for (int y = x + 1; y < data.length; y++) {
if (data[y] < data[x]) {
SortTest.swap(data, x, y);
}
}
}
}
}

快速排序(QuickSort):

/**
* 快速排序
* @author mly11
*
*/
public class QuickSort {
static int low = 0;
static int high = 0;
static int mid = 0;
public static void main(String[] args) {
int[] arr = {5,1,3,9,2,7,2,4}; // 开始的数组为
System.out.println("开始的数组为:");
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i]+"\t");
}
System.out.println(); // 排序
judge(arr); // 排序后遍历
System.out.println("排序后数组为:");
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i]+"\t");
}
System.out.println();
} // 判断数组是否为空
public static void judge(int[] arr){
if(arr.length > 0){
all(arr,0,arr.length-1);
}else{
System.out.println("换个吧");;
}
} // 循环条件,递归调用循环体
public static void all(int[] arr,int low,int high){
if(low < high){
// 用mid记录每一次选择的分界数字的角标,
mid = structure(arr, low, high); // 当low < mid -1的时候,从low到mid-1进行递归
if(low < mid - 1){
all(arr, low, mid-1);
} // 当high>mid+1时候,从mid+1到high递归
if(high > mid +1){
all(arr, mid+1, high);
}
}
} // 循环体 一次循环
public static int structure(int[] arr,int low,int high){
// 当索引low小于high时,进行运算,让小于所选值在左边,否则在右边
/* 原理:
取出a[low]的数据存入temp,使a[low]为空;开始循环:
当low < high 时,一直循环
while(low < high){
当low从第一个开始时,从后向前一一查找,如果比temp大,则high--;
否则交换a[low]和a[high],此时a[low]的值为a[high],a[high]为空;
现在从前(a[low]被a[high]占据的位置)向后查找,如果比temp小,则low++;
否则将a[low]的位置存入a[high](上一步为空的a[high]),此时a[low]为空;
}
a[low] = temp;
返回low,此时low的位置之前数据全部<a[low],low之后的位置的数据全部>a[low];
*/ int temp = arr[low];
while (low < high) {
while (low < high && arr[high] >= temp) {
high--;
}
arr[low] = arr[high];
while (low < high && arr[low] < temp) {
low++;
}
arr[high] = arr[low];
}
arr[low] = temp;
return low; /*错误的方法
while(low < high){
int temp = arr[low];
while(arr[low] <= arr[high]){
high--;
}
arr[low] = arr[high];
low++;
arr[high] = arr[low];
arr[low] = temp;
}
return low;*/
} }

以上代码为自己在最开始接触的时候写的,不足之处请谅解。

几种排序方式的java实现(01:插入排序,冒泡排序,选择排序,快速排序)的更多相关文章

  1. C# 插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序

    C# 插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序 以下列出了数据结构与算法的八种基本排序:插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序 ...

  2. 几种常见排序算法之Java实现(插入排序、希尔排序、冒泡排序、快速排序、选择排序、归并排序)

    排序(Sorting) 是计算机程序设计中的一种重要操作,它的功能是将一个数据元素(或记录)的任意序列,重新排列成一个关键字有序的序列. 稳定度(稳定性)一个排序算法是稳定的,就是当有两个相等记录的关 ...

  3. java结构与算法之选择排序

    一 .java结构与算法之选择排序(冒择路兮快归堆) 什么事选择排序:从一组无序数据中选择出中小的的值,将该值与无序区的最左边的的值进行交换. 简单的解释:假设有这样一组数据 12,4,23,5,找到 ...

  4. JAVA实现--基础算法FOR选择排序

    首先 实现简单的选择排序. 简单排序的思路很简单,就是通过遍历(数组的length次)的数组,每次遍历找出最小的放到数组的第一个位置,下次遍历时就不用考虑第0位置的数从第1的位置开始找1到length ...

  5. 归并排序 & 计数排序 & 基数排序 & 冒泡排序 & 选择排序 ----> 内部排序性能比较

    2.3 归并排序 接口定义: int merge(void* data, int esize, int lpos, int dpos, int rpos, int (*compare)(const v ...

  6. 算法 排序lowB三人组 冒泡排序 选择排序 插入排序

    参考博客:基于python的七种经典排序算法   [经典排序算法][集锦]     经典排序算法及python实现 首先明确,算法的实质 是 列表排序.具体就是操作的列表,将无序列表变成有序列表! 一 ...

  7. 学习C#之旅 冒泡排序,选择排序,插入排序,希尔排序[资料收集]

    关于冒泡排序,选择排序,插入排序,希尔排序[资料收集]  以下资料来源与网络 冒泡排序:从后到前(或者从前到后)相邻的两个两两进行比较,不满足要求就位置进行交换,一轮下来选择出一个最小(或最大)的放到 ...

  8. python算法(一)基本知识&冒泡排序&选择排序&插入排序

    本节内容: 算法基本知识 冒泡排序 选择排序 插入排序 1. 算法基本知识 1.1 什么是算法? 算法(algorithm):就是定义良好的计算过程,他取一个或一组的值为输入,并产生出一个或一组值作为 ...

  9. C语言实现 冒泡排序 选择排序 希尔排序

    // 冒泡排序 // 选择排序 // 希尔排序 // 快速排序 // 递归排序 // 堆排序 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h& ...

随机推荐

  1. Cocos2d-x项目移植到WP8系列之六:C#工程使用C++的DLL

    原文链接: http://www.cnblogs.com/zouzf/p/3984510.html 此时,一些大问题都被解决后,整个工程基本能跑起来了,最后一个大问题是:业务层是用Lua开发的,底层的 ...

  2. IDEA: 遇到问题Error during artifact deployment. See server log for details.详解

    IDEA 的配置确实有些烦人,完整的配置我之前发过,现在有个著名的报错: Error during artifact deployment. See server log for details. 这 ...

  3. python去掉行尾的换行符

    python去掉行尾的换行符 mystring.strip().replace(' ', '').replace('\n', '').replace('\t', '').replace('\r', ' ...

  4. [转载]Google Android开发精华教程

    原文地址:Android开发精华教程">Google Android开发精华教程作者:huiyi8zai Android是Google于2007年11月5日宣布的基于Linux平台的开 ...

  5. java:system根据输入的内容,然后输出(字节流)

    把输入的内容输出来:根据system.in的内容System.out.println输出出来 都是字节流,的形式: //限制读取的字符长度 //字节流 InputStream ips = System ...

  6. EF-局部更新

    // ////1 public Task ReservedQuantity(long productId, long skuId, int reservedQuantity, long userId) ...

  7. python中常用的文件和目录操作(二)

    一. os模块概述 python os模块提供了非常丰富的方法用来处理文件和目录 二. 导入os模块: import os 三. 常用方法 1. os.name 输出字符串表示正在使用的平台,如果是w ...

  8. 用Find命令查找文件

    ➜ AFNetworking find . -iname "*.swift" ./Example/tvOS Example/AppDelegate.swift ./Example/ ...

  9. android之Uri的常用几个例子

    显示网页:   1. Uri uri = Uri.parse("http://www.google.com");   2. Intent it = new Intent(Inten ...

  10. Mybatis 别名机制,自动扫描 数据的增删改

    mybatis别名机制: 在mybatis.xml文件中的<configuration></configuration>标签中间加入属性<typeAliases>& ...