以下为集中排序的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. PHP 面向对象及Mediawiki 框架分析(二)

    mediaHandler可以理解为处理media文件的 /includes/filerepo/file/File.php /** * Get a MediaHandler instance for t ...

  2. 一览Swift中的常用关键字

    要学习Swift这门语言,就必须先了解Swift的关键字及对应的解释.这里就列一下在Swift中常用到的关键字. 关键字是类似于标识符的保留字符序列,除非用重音符号(`)将其括起来,否则不能用作标识符 ...

  3. 去除带有iframe页面中的2个滚动条[转]

    方法一:加载frame时修改高度 <div>    <iframe id="frame_content" name="frame_content&quo ...

  4. linux基础(8)-颜色显示

    echo显示内容-带颜色显示 格式:echo -e "\033[字体背景颜色;文字颜色m字符串 \033[0m" 实例:echo -e "\n\n \t\t \033[4 ...

  5. MySQL性能优化-内存参数配置

    Mysql对于内存的使用,可以分为两类,一类是我们无法通过配置参数来配置的,如Mysql服务器运行.解析.查询以及内部管理所消耗的内存:另一类如缓冲池所用的内存等. Mysql内存参数的配置及重要,设 ...

  6. shitf+tab

    在eclipse中,shitf+tab可以使代码向左移动.

  7. jQuery download file

    jQuery.download = function (url, method, p, c, e, i, o, goodsType, reciveUser, suplier) { jQuery('&l ...

  8. svn更新时忽略指定文件或文件夹

    选择一个收SVN控制的文件夹->右击->选择TortoiseSVN->更新至版本,就会出现   选择更新深度为工作副本,再选择项目,出现如图中所示的界面,把不想更新的文件或者文件夹前 ...

  9. liunx常用命令-----查找命令

    locate 根据文件名查找文件 根据数据库记录搜索,当天创建的搜不到 whereis which  搜索命令的命令   如 whereis ls find   /root   -name       ...

  10. Redis 简单介绍(知识整理笔记)

    前言: Redis 介绍:轻量级.Key-Value.内存数据库.支持持久化 Redis 数据结构:string(字符串),hash(哈希),list(列表),set(集合)及 zset (sorte ...