目前在看排序算法有:冒泡排序,选择排序,插入排序,快速排序,归并排序。后面两种有递归操作,还没完成理解,先打个样,后面有时间再来琢磨~
冒泡排序算法
public class BubbleSort {
static final String TAG = "BubbleSort";
public static void sort(int[] array){
boolean flag;
//第一个循环,表示有多少轮
for(int i = 0; i < array.length; i++){
flag = false;
Log.d(TAG, "第" + i + "轮开始。。。");
//第二个循环,表示比较多少次
for(int j = 0; j < array.length - i - 1;j++){
//判断语句,表示替换多少次
if(array[j] > array[j + 1]){
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
flag = true;
}
}
//如果没有替换,则退出循环
if(!flag){
break;
}
}
}
}
选择排序算法
/**
* 作者:luoxiaohui
* 日期:2018/10/18 18:02
* 文件描述: 选择排序
* 1. 从第一个元素开始,分别与后面的元素进行比较,找到最小的元素,并与第一个元素交换位置
* 2. 从第二个元素开始,分别与后面的元素进行比较,找到最小的元素,并与第二个元素交换位置
* 3. 以此类推,直到最后一个元素
*/
public class SelectSort {
public static void sort(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
int minIndex = i;
//定义j=minIndex+1,能确保前面已经被比较过的值,不会被再次比较
for (int j = minIndex + 1; j < array.length; j++) {
//如果被比较的值小于目前已经被比较过的的最小值,则将当前值的索引赋值的最小值的索引
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
//当前的元素与后面元素的最小值,交换位置
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}
}
插入排序算法
/**
* 作者:luoxiaohui
* 日期:2018/10/18 17:12
* 文件描述: 插入排序算法
* 1、以数组的某一位作为分隔位,比如index=1,假设左面的都是有序的.
*
* 2、将index位的数据拿出来,放到临时变量里,这时index位置就空出来了.
*
* 3、从leftindex=index-1开始将左面的数据与当前index位的数据(即temp)进行比较,如果array[leftindex]>temp,
* 则将array[leftindex]后移一位,即array[leftindex+1]=array[leftindex],此时leftindex就空出来了.
*
* 4、再用index-2(即leftindex=leftindex-1)位的数据和temp比,重复步骤3,
* 直到找到<=temp的数据或者比到了最左面(说明temp最小),停止比较,将temp放在当前空的位置上.
*
* 5、index向后挪1,即index=index+1,temp=array[index],重复步骤2-4,直到index=array.length,排序结束,
* 此时数组中的数据即为从小到大的顺序.
*
*/
public class InsertSort {
public static void sort(int[] array){
for(int index = 1; index < array.length; index++){
//用于比较的临时数据
int temp = array[index];
//临时数据的左边的数据的索引
int leftIndex = index - 1;
//当临时数据的左边的索引大于等于0,且左边数据大于临时数据是,进入死循环
while (leftIndex >= 0 && array[leftIndex] > temp){
//将左边数据右移一位
array[leftIndex + 1] = array[leftIndex];
//将索引左移一位
leftIndex--;
}
// 把临时数据放到空位上
array[leftIndex + 1] = temp;
}
}
}
快速排序算法
public class FastSort {
public static void sort(int[] a, int low, int high) {
int start = low;
int end = high;
int key = a[low];
while (end > start) {
//从后往前比较
while (end > start && a[end] >= key) //如果没有比关键值小的,比较下一个,直到有比关键值小的交换位置,然后又从前往后比较
end--;
if (a[end] <= key) {
int temp = a[end];
a[end] = a[start];
a[start] = temp;
}
//从前往后比较
while (end > start && a[start] <= key)//如果没有比关键值大的,比较下一个,直到有比关键值大的交换位置
start++;
if (a[start] >= key) {
int temp = a[start];
a[start] = a[end];
a[end] = temp;
}
//此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用
}
//递归
if (start > low) sort(a, low, start - 1);//左边序列。第一个索引位置到关键值索引-1
if (end < high) sort(a, end + 1, high);//右边序列。从关键值索引+1到最后一个
}
}
归并排序算法
/**
* 作者:luoxiaohui
* 日期:2018/10/19 10:10
* 文件描述: 归并排序
* 分而治之(divide - conquer);每个递归过程涉及三个步骤
* 第一, 分解: 把待排序的 n 个元素的序列分解成两个子序列, 每个子序列包括 n/2 个元素.
* 第二, 治理: 对每个子序列分别调用归并排序MergeSort, 进行递归操作
* 第三, 合并: 合并两个排好序的子序列,生成排序结果.
*/
public class MergerSort {
public static int[] sort(int[] a, int low, int high) {
int mid = (low + high) / 2;
if (low < high) {
sort(a, low, mid);
sort(a, mid + 1, high);
//左右归并
merge(a, low, mid, high);
}
return a;
}
public static void merge(int[] a, int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int i = low;
int j = mid + 1;
int k = 0;
// 把较小的数先移到新数组中
while (i <= mid && j <= high) {
if (a[i] < a[j]) {
temp[k++] = a[i++];
} else {
temp[k++] = a[j++];
}
}
// 把左边剩余的数移入数组
while (i <= mid) {
temp[k++] = a[i++];
}
// 把右边边剩余的数移入数组
while (j <= high) {
temp[k++] = a[j++];
}
// 把新数组中的数覆盖nums数组
for (int x = 0; x < temp.length; x++) {
a[x + low] = temp[x];
}
}
}
参考文章
Java实现归并排序