首先是七种排序算法的性能,如下图
下面对各种排序算法的基本原理进行说明,并用Java代码实现
- 选择排序
基本原理如下:对于给定的一组记录,经过第一轮比较后得到最小的纪录,然后将该记录与第一个记录的位置进行交换;接着对不包括第一个记录以外的其他记录进行第二轮比较,得到最小的记录只有一个时位置。以数组{38,65,97,76,13,27,49}为例,选择排序的具体步骤如下:
第一趟排序后:13【65,97,76,38,27,49】
第二趟排序后:13 27【97,76,38,65,49】
第三趟排序后:13 27 38【76,97,65,49】
第四趟排序后:13 27 38 49【97 65 76】
第五趟排序后:13 27 38 49 65【97 76】
第六趟排序后: 13 27 38 49 65 76【97】
第七趟排序后: 13 27 38 49 65 76 97
代码如下:
public class TestSort {
public static void selectSort(int[] a) {
int i;
int j;
int temp = 0;
int flag = 0;
int n = a.length;
for (i = 0; i < n; i++) {
temp = a[i];
flag = i;
for (j = i + 1; j < n; j++) {
if (a[j] < temp) {
temp = a[j];
flag = j;
}
}
if (flag != i) {
a[flag] = a[i];
a[i] = temp;
}
}
}
public static void main(String[] args) {
int i = 0;
int a[] = { 5, 4, 9, 8, 7, 6, 0, 1, 3, 2 };
selectSort(a);
for (int j = 0; j < a.length; j++) {
System.out.print(a[j] + " ");
}
System.out.println("\n");
}
}
选择排序的简洁算法如下
public class SecondSelectSort {
public static void SelectSort(int[] source) {
for (int i = 0; i < source.length; i++) {
for (int j = i + 1; j < source.length; j++) {
if (source[i] > source[j]) {
swap(source, i, j );
}
}
}
}
private static void swap(int[] source, int j, int i) {
int temp = source[j];
source[j] = source[i];
source[i] = temp;
}
public static void main(String[] args) {
int a[] = { 5, 4, 9, 8, 6, 0, 1, 3, 2 };
SelectSort(a);
for (int i = 0; i < a.length; i++) {
System.out.printf("%d ", a[i]);
}
}
}
- 冒泡排序
冒泡排序顾名思义就是整个过程像气泡一样往上升,单项冒泡排序的基本思想是(假设由小到大排序):对于给定的n个记录,从第一个记录开始依次对相邻的两个记录进行比较,当前面的记录大于后面的记录时,交换位置,进行一轮比较和换位后,n个记录中的最大记录将位于第n位;然后对前(n-1)个记录进行第二轮比较了重复该过程知道进行比较的记录只剩下一个为止。
public class TestBubbleSort {
public static void bulleSort(int[] source) {
for (int i = source.length-1,i>0;i--) {
for (int j = 0;j<i;j++) {
if (source[j]>source[j+1]])
swap(source,j,j+1);
}
}
}
private static void swap(int array[], int one, int two) {
int temp = array[one];
array[one] = array[two];
array[two] = temp;
}
public static void main(String[] args) {
int a[] = { 5, 4, 9, 8, 7, 6, 0, 1, 3, 2 };
bulleSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+ " ");
}
}
}
- 插入排序:对于给定一组记录,初始时假设第一个记录自成一个有序序列,奇遇记录为无序序列。接着从第二个记录开始,按照记录的大小依次将当前记录插入到其之前的有序序列中,直至最后一个记录插入到有序序列位置。
public class InsertSort {
public static void Test(int a[]) {
for (int i = 1; i < a.length; i++) {
int temp = a[i], j = i;
if (a[j-1] > temp) {
while (j >= 1 && a[j - 1] > temp) {
a[j] = a[j - 1];
j--;
}
}
a[j] = temp;
}
}
public static void main(String[] args) {
int a[] = { 5, 4, 9, 8, 6, 0, 1, 3, 2 };
Test(a);
for (int j = 0; j < a.length; j++) {
System.out.print(a[j] + " ");
}
}
}
输出结果:0 1 2 3 4 5 6 8 9
插入排序另一种简洁算法。
public class SecondInsertSort {
public static void InsertSort(int[] source) {
for (int i = 1; i < source.length; i++) {
for (int j = i ; (j > 0) && source[j] < source[j - 1]; j--) {
swap(source, j, j - 1);
}
}
}
private static void swap(int[] source, int j, int i) {
int temp = source[j];
source[j] = source[i];
source[i] = temp;
}
public static void main(String[] args) {
int a[] = { 5, 4, 9, 8, 6, 0, 1, 3, 2 };
InsertSort(a);
for (int i = 0; i < a.length; i++) {
System.out.printf("%d ",a[i]);
}
}
}
- 归并排序
只给出代码,很好理解
public class mergeSort {
public static void merge(int array[], int start1, int end1, int start2,
int end2) {
int i, j;// i,j分别为表1和表2的游标
i = start1;
j = start2;
int k = 0;
int[] temp = new int[end2 - start1 + 1];// 建立一个临时长度为两个子列表长度之和的数组
while (i <= end1 && j <= end2) {// 通过循环,依次从两个子列表中找出较小元素放入临时数组中
if (array[i] > array[j])
temp[k++] = array[j++];
else
temp[k++] = array[i++];
}
// 把剩下的元素依次放入临时数组中(肯定只剩下一方)
while (i <= end1)
temp[k++] = array[i++];
while (j <= end2)
temp[k++] = array[j++];
k = start1;
for (int element : temp) {// 把临时数组元素复制给原数组
array[k++] = element;
}
}
public static void mergeSort(int array[], int start, int end) {
if (start < end) {
int mid = (start + end) / 2;
// 两路归并
mergeSort(array, start, mid);
mergeSort(array, mid + 1, end);
merge(array, start, mid, mid + 1, end);
}
}
public static void main(String[] args) {
int a[] = { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };
int i;
mergeSort(a, 0, a.length - 1);
for (i = 0; i < a.length; i++) {
System.out.printf("%d ", a[i]);
}
}
}
未完待续。。。。