以下为集中排序的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;*/
} }

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

最新文章

  1. 【转载】在HTML中插入swf文件(转)
  2. oracle实用sql之将逗号分割的字符串分割多个列
  3. LeetCode——Merge k Sorted Lists
  4. Python 中的函数与类的方法
  5. 加快Win7整体运行速度的12个小技巧
  6. javascript中数组的迭代等操作
  7. Vmware中为Mac Os安装vmtools
  8. mysql常用操作命令
  9. IOS9提示“不受信任的开发者”如何处理
  10. Python os与sys模块解析
  11. win10 uwp 上传Nuget 让别人用我们的库
  12. vertical-align 和 img属性 和 鼠标样式
  13. iOS常用控件尺寸大集合
  14. [python]PyPI使用国内源
  15. AdvStringGrid常用操作
  16. 剑指offer编程题Java实现——面试题4后的相关题目
  17. 小组冲刺第十四天站立会议(Beta版发布)
  18. Centos7.0 配置docker 镜像加速
  19. 第一次php之旅
  20. Java之集合(二十五)ConcurrentHashMap

热门文章

  1. java基础11(IO流)-字符流
  2. Spring的DI初步
  3. JMeter接口测试报错,反馈和postman不一样(一)
  4. 使用struts碰到的错误
  5. 关于const_cast转换
  6. CSS媒体查询 width VS device-width
  7. 解析Ceph: 恢复与数据一致性
  8. 项目管理理论与实践(4)——UML应用(上)
  9. java学习笔记 --- 多线程(1)
  10. 原生JDBC的使用