一、最简单排序
-
算法思想
让每一个关键字都和它后面的每一个关键字比较,如果大则交换,这样第一个位置的关键字在依次循环后一定变成最小值。 -
算法示意图
从上面的示意图可以看出: -
当 i = 1时,9与1交换后,1与其余的关键字比较均最小,因此,1为最小的数,放在首位;
-
当 i= 2时,9 < 5 ,所以将9与5交换位置,此时第二个数为5,继续比较下一位,5 < 8正常,继续比较下一位,5 > 3,所以交换5和3的位置,直到最后的一位2也满足情况,继续交换,此时最终的结果是:第二位为2。按照这样的规律一直进行下去,直到该序列最后为有序序列则排序完毕。
-
代码实现
package test;
/**
* 最简单的排序
* @author Microtao
*
*/
public class SortTest01 {
private static void CompareSortTest(int[] a) {
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
public static void main(String[] args) {
int[] a = new int[] { 6, 2, 1, 9, 8, -1, 4, 11, 10 };
// 排序前
System.out.print("排序前:");
for (int i : a) {
System.out.print(i + " ");
}
System.out.println();
CompareSortTest(a);
// 排序后
System.out.print("排序后:");
for (int i : a) {
System.out.print(i + " ");
}
}
}
运行结果:
排序前:6 2 1 9 8 -1 4 11 10
排序后:-1 1 2 4 6 8 9 10 11
这是最简单的实现排序的算法,该算法的时间复杂度为O(n2)
二、冒泡排序
- 算法思想
冒泡排序是一种交换排序,它的基本思想是:两两比较相邻的关键字,如果反序则交换,直到没有反序的记录为止。一般都是从最末尾开始比较,逐个向上冒。冒泡的思想重点是相邻之间的比较 - 算法示意图
在冒泡排序中,可以看出一个循环完毕之后,第一位一定是一个最小的数,但与此同时在中间的过程中,我们同样可以看到相对小的数也会向上移动,最终都会形成有序的序列。 - 算法实现
package test;
/**
* 冒泡排序的测试
* @author Microtao
*
*/
public class SortTest01 {
private static void MaoPaoSortTest(int[] a) {
for (int i = 0; i < a.length; i++) {
//从序列的后面开始,两两之间进行比较
for (int j = a.length - 1; j > i; j--) {
if (a[j - 1] > a[j]) {
int temp;
temp = a[j - 1];
a[j - 1] = a[j];
a[j] = temp;
}
}
}
}
public static void main(String[] args) {
int[] a = new int[] { 0, 9, 12, -5, 31, 25, 100 };
// 排序前
System.out.print("排序前:");
for (int i : a) {
System.out.print(i + " ");
}
System.out.println();
MaoPaoSortTest(a);
// 排序后
System.out.print("排序后:");
for (int i : a) {
System.out.print(i + " ");
}
}
}
三、选择排序
- 算法思想
选择排序思想:在一个有N个元素的序列中,选取最小的元素与数组的第一个元素进行交换位置;再从第N-1个元素中选取最小的元素与第二个元素交换位置,不断重复该步骤,直到所有的元素都是有序的退出。 - 算法示意图
对于数列【1,-2,9,3,10,0,11,5,7】采用选择排序的过程
- 算法实现
package test;
/** 选择排序法
* @author Microtao
*
*/
public class SortTest02 {
private static void SelectSortTest(int[] a) {
for (int i = 0; i < a.length; i++) {
int min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[min]) {
min = j;
}
}
if (min != i) {
int temp;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
}
public static void main(String[] args) {
int[] a = new int[] { 1, -2, 9, 3, 10, 0, 11, 5, 7 };
// 排序前
System.out.print("排序前:");
for (int i : a) {
System.out.print(i + " ");
}
System.out.println();
SelectSortTest(a);
// 排序后
System.out.print("排序后:");
for (int i : a) {
System.out.print(i + " ");
}
}
}
运行结果:
排序前:1 -2 9 3 10 0 11 5 7
排序后:-2 0 1 3 5 7 9 10 11
四、插入排序
- 算法思想
插入排序的思想:假设一个序列中,第一个数字是有序的,从第二个数字开始,和前面的数字进行比较,如果后面的数字比前面的数字小,则将前面的数字往后挪动,直到该数(temp)大于前面的数字,依次重复该操作,直到序列是有序的。 - 算法示意图
package test;
/**
* 插入排序
*
* @author Microtao
*
*/
public class SortTest03 {
private static void InsertSortTest(int[] a) {
for (int i = 1; i < a.length; i++) {
int temp = a[i];
int leftIndex = i - 1;
while (leftIndex >= 0 && temp < a[leftIndex]) {
a[leftIndex + 1] = a[leftIndex];
leftIndex--;
}
// 将temp放在空位上
a[leftIndex + 1] = temp;
}
}
public static void main(String[] args) {
int[] a = new int[] { 6, 0, 14, -1, 3, 2, 4 };
// 排序前
System.out.print("排序前:");
for (int i : a) {
System.out.print(i + " ");
}
System.out.println();
InsertSortTest(a);
// 排序后
System.out.print("排序后:");
for (int i : a) {
System.out.print(i + " ");
}
}
}
五、快速排序(面试经常手写)
- 算法思想
快速排序算法的思想:首先选取一个初始索引值,分别和low指针、high指针进行比较,如果a[high]>temp;则high–,直到不满足情况,则将该位置的值赋值给a[low];再进行左边的操作,直至low=high,再将temp赋值给a[low] (其实赋值给a[high]是一样的),返回low的位置,即是第一次寻找的index,再重复上述的过程,直到全部有序为止。 - 算法示例图
这是进行一次的结果,选出index = 5,后面再依据这种思想,将左边和右边分别进行该操作
package test;
/**
* 快速排序
*
* @author Microtao
*
*/
public class SortTest04 {
/**
* @param a
* @param low
* 最低位
* @param high
* 最高位
*/
private static void fastSortTest(int[] a, int low, int high) {
int index;
if (low < high) {
index = getIndex(a, low, high);
fastSortTest(a, 0, index - 1);
fastSortTest(a, index + 1, high);
}
}
/**
* 获取索引的下标
*/
private static int getIndex(int[] a, int low, int high) {
int temp = a[low];
while (low < high) {
while (low < high && a[high] >= temp)
high--;
a[low] = a[high];
while (low < high && a[low] <= temp)
low++;
a[high] = a[low];
}
a[low] = temp;
return low;
}
public static void main(String[] args) {
int[] a = new int[] { 6, 0, 14, -1, 3, 2, 4 };
// 排序前
System.out.print("排序前:");
for (int i : a) {
System.out.print(i + " ");
}
System.out.println();
fastSortTest(a, 0, a.length - 1);
// 排序后
System.out.print("排序后:");
for (int i : a) {
System.out.print(i + " ");
}
}
}
运行结果:
排序前:6 0 14 -1 3 2 4
排序后:-1 0 2 3 4 6 14