Java排序算法
一、直接插入排序
Java学习之直接插入排序,思路源于:http://blog.youkuaiyun.com/chao360559040/article/details/5973716
原理:每次执行,把后面的数插入到前面已经排序好的数组中,直到最后一个完成。
public class DirectinsertSort {
public void directinsertSort(int arr[]) {
int j,temp;
for(int i = 1; i < arr.length; i ++) {
temp = arr[i];
for(j =i-1; j >= 0; j --) {
if(temp >= arr[j]) {
break;
}
arr[j+1] = arr[j];
}
arr[j+1] = temp;
}
}
public static void main(String[] args) {
int arr[] = {11,23,14,88,99,77,55,67,66};
new DirectinsertSort().directinsertSort(arr);
for(int a : arr) {
System.out.print(a + " ");
}
}
}
运行结果:11 14 23 55 66 67 77 88 99
代码编写与原文相差之处为if条件判断条件为temp>=arr[j],在此处改变的原因是想要让其更改为从小至大排序。
思路如下:
- 传入directinsert_sort(int arr[])的为一个数组arr[],而数组通过数组名称定位到数组内存所在地址,所以不需要返回值即可实现数组的改变。
- 在directinsert_sort(int arr[])中首先定义一个临时变量temp用于存储将要插入数组的值。
- 第一层i的for循环从arr[1]到arr[arr.length]循环遍历整个数组,在其中设定temp = arr[i],用于存储第一个要插入的数值,i=1时第一次遍历arr[1]之前的数组已排序好(只有一个arr[0],想不排好也难)。
- 第二层j的for循环中从i-1到0遍历整个数组,判断如果要插入的值temp >= arr[j]成立时,终止第二层循环,将temp值插入原本排好序的数组最后(i=1时,即arr[i] = temp,arr[0]已排好序);如果判断条件不成立及将原本排序好的数组的最后一个数后移后一位,继续第二层循环,知道判断成立,将temp插入数组正确位置。
二、快速排序
Java学习之快速排序,思路源于:https://www.cnblogs.com/hjy9420/p/5032309.html
原理:选择一个关键值作为基准值。比基准值小的都在左边序列(一般是无序的),比基准值大的都在右边(一般是无序的)。
public class QuitSort {
public void quitSort(int arr[], int low, int high) {
int start = low;
int end = high;
int key = arr[low];
while(start < end) {
while(end > start && key <= arr[end]) {
end --;
}
if(key >= arr[end]) {
int temp = arr[end];
arr[end] = arr[start];
arr[start] = temp;
}
while(start < end && arr[start] <= key) {
start ++;
}
if(arr[start] >= key) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}
if(start > low) {
quit_sort(arr, low, start-1);
}
if(end < high) {
quit_sort(arr, end+1, high);
}
}
public static void main(String[] args) {
int arr[] = {12,20,5,16,15,1,30,45,23,9};
int start = 0;
int end = arr.length - 1;
new QuitSort().quitSort(arr, start, end);
for(int a : arr){
System.out.print(a + " ");
}
}
}
运行结果:1 5 9 12 15 16 20 23 30 45
思路如下:
- 传入quit_sort(int arr[], int low, int high)的为数组arr[]、数组最初的下标起点和下标终点,而数组通过数组名称定位到数组内存所在地址,所以不需要返回值即可实现数组的改变。
- 在quit_sort(int arr[], int low, int high)中首先定义三个变量start、end、key用于标识数组下标游走的起始位置、数组下标游走的最终位置、基准值。
- 比较start和end,start >= end时表明基于基准值key左右两边已摆好(左边值比基准值key小,右边值比基准值大)。
- start < end则进入循环,因为选定的基准值key为原数组第一个值,所以先对原数组进行从右至左的遍历,当遇到比基准值key大或等时将标识end减少1再进行判断,直到从右至左遍历原数组中值小于基准值key时退出循环之后将此位置的值与数组之前的标识start的值arr[start]替换位置;然后再进行从左至右的遍历,当遇到比基准值key小或等时将标识start增加1再进行判断,直到从左至右遍历原数组中值大于基准值key时退出循环之后将此位置的值与数组之前的标识end的值arr[end]替换位置。
最后通过递归将当前基准值两边的杂乱数据排序,直到标识start、end回归最初的初始标记也就是查询数组的下标起点low和下标终点high。
三、冒泡排序
Java学习之冒泡排序,思路源于:http://blog.youkuaiyun.com/pzhtpf/article/details/7560294
原理:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。
public class BubbleSort {
public void bubbleSort(int[] arr) {
for(int i = 0; i < arr.length - 1; i++) {
for(int j = 0; j < arr.length - 1 - i; j++) {
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
public static void main(String[] args) {
int arr[] = {11,23,14,88,99,77,55,67,66,1,30,45,23,9};
new BubbleSort().bubbleSort(arr);
for(int a : arr) {
System.out.print(a + " ");
}
}
}
运行结果:1 9 11 14 23 23 30 45 55 66 67 77 88 99
思路如下:
外循环用于控制循环次数,内循环用于将数组中较大值后移,通过j < arr.length - 1 - i能将判断好的较大值排除出要判断的数组,最终,将数组中的值完全排列。