排序算法

来源:互联网 发布:淘宝账号密码怎么修改 编辑:程序博客网 时间:2024/04/28 17:20

常见的排序算法

一、冒泡排序

冒泡排序(BubbleSort)的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面。即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后。然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,直至比较最后两个数,将小数放前,大数放后。至此第一趟结束,将最大的数放到了最后。在第二趟:仍从第一对数开始比较(因为可能由于第2个数和第3个数的交换,使得第1个数不再小于第2个数),将小数放前,大数放后,一直比较到倒数第二个数(倒数第一的位置上已经是最大的),第二趟结束,在倒数第二的位置上得到一个新的最大数(其实在整个数列中是第二大的数)。如此下去,重复以上过程,直至某一趟的交换顺序为0,最终完成排序。

由于在排序过程中总是小数往前放,大数往后放,相当于气泡往上升,所以称作冒泡排序。
特点:1、它的时间复杂度为O(n^2
      2.具有稳定性,这里的稳定性是指原序列中相同元素的相对顺序仍然保持到排序后的序列,

例如对10,-3,5,34,-34,5,0,9进行排序

  第一趟:-3,5,10,-34,5,0,9,34

  第二趟:-3,5,-34,5,0,9,10,34

  第三趟:-3,-34,5,5,0,9,10,34

  第四趟:-34,-3,5,0,5,9,10,34

  第五趟:-34,-3,0,5,5,9,10,34

import junit.framework.TestCase;public class BubbleSort extends TestCase {public void sort(Integer a[]) {boolean exchange = false; // 某一趟的交换次数为0for (int i = a.length - 1; i > 0; i--) { for (int j = 0; j < i; j++) {if (a[j + 1] < a[j]) {exchange = true;Integer temp = a[j + 1];a[j + 1] = a[j];a[j] = temp;}}if (!exchange) {           //如果本趟排序次数为0,表示排序次数为0break;}}}public void test() {Integer a[] = {10,-3,5,34,-34,5,0,9 };sort(a);for (int i = 0; i < a.length; i++) {System.out.print(a[i] + "、");}}}

二、选择排序

每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。选择排序是不稳定的排序方法。 

     初始        [49 38 65 97 76 13 27 49]

  第一趟排序后 13 [38 65 97 76 49 27 49]

  第二趟排序后 13 27 [65 97 76 49 38 49]

  第三趟排序后 13 27 38 [97 76 49 65 49]

  第四趟排序后 13 27 38 49 [76 97 65 49]

  第五趟排序后 13 27 38 49 49 [97 65 76]

  第六趟排序后 13 27 38 49 49 65 [97 76]

  第七趟排序后 13 27 38 49 49 65 76 [97]

  最后排序结果 13 27 38 49 49 65 76 97

import junit.framework.TestCase;public class CollectSort extends TestCase {// 选择排序public void sort(Integer a[]) {int min;int temp;for (int i = 0; i < a.length - 1; i++) {min = i; // 本次排序最小的for (int j = i + 1; j < a.length; j++) {if (a[j] < a[min]) {min = j;}}if (min != i) {temp = a[min];a[min] = a[i];a[i] = temp;}}}public void test() {Integer array[] = { 49, 38, 65, 97, 76, 13, 27, 49 };sort(array);for (int i = 0; i < array.length; i++) {System.out.print(array[i] + "  ");}}}

 三、快速排序

快速排序它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。

public class QuickSort {public static void sort(Comparable[] data, int low, int high) {// 枢纽元,一般以第一个元素为基准进行划分int i = low;int j = high;if (low < high) {// 从数组两端交替地向中间扫描Comparable pivotKey = data[low];// 进行扫描的指针i,j;i从左边开始,j从右边开始while (i < j) {while (i < j && data[j].compareTo(pivotKey) > 0) {j--;}// end whileif (i < j) {// 比枢纽元素小的移动到左边data[i] = data[j];i++;}// end ifwhile (i < j && data[i].compareTo(pivotKey) < 0) {i++;}// end whileif (i < j) {// 比枢纽元素大的移动到右边data[j] = data[i];j--;}// end if}// end while// 枢纽元素移动到正确位置data[i] = pivotKey;// 前半个子表递归排序sort(data, low, i - 1);// 后半个子表递归排序sort(data, i + 1, high);}// end if}// end sortpublic static void main(String[] args) {// 在JDK1.5版本以上,基本数据类型可以自动装箱// int,double等基本类型的包装类已实现了Comparable接口Comparable[] c = { 49 ,38, 65, 97, 76, 13, 27 };sort(c, 0, c.length - 1);for (Comparable data : c) {System.out.println(data);}}}

四、直接插入排序

插入排序(insert sorting)思想:当插入第i个元素时,前面的v[0],v[1],v[2]......v[i-1],已经排好序了.这时用v[i]的插入码与v[i-1],v[i-2],......排序码进行比较,找到插入的位置即插入v[i],原来位置上的元素从后向前依次后移。

第一趟比较前两个数,然后把第二个数按大小插入到有序表中; 第二趟把第三个数据与前两个数从前向后扫描,把第三个数按大小插入到有序表中;依次进行下去,进行了(n-1)趟扫描以后就完成了整个排序过程。

直接插入排序属于稳定的排序,最坏时间复杂性为O(n2)空间复杂度为O(1)。 

例子

[46] 58 15 45 90 18 10 62
[15 46] 58 45 90 18 10 62
[15 45 46] 58 90 18 10 62
[15 45 46 58] 90 18 10 62
[15 18 45 46 58] 90 10 62
[10 15 18 45 46 58] 90 62
[10 15 18 45 46 58 62] 90 

public class Straight_Insert_Sort {public static void sort(Comparable array[]) {int i, j;Comparable temp;for (i = 1; i < array.length; i++) {temp = array[i];for (j = 0; j < i; j++) {if (array[j].compareTo(array[i]) > 0) {for (int k = i; k > j; k--) {array[k] = array[k - 1];}array[j] = temp;break;}}//以下的代码只是为了打印输出System.out.print("[");j = 0;for (j = 0; j <= i - 1; j++) {System.out.print(array[j] + " ");}System.out.print(array[i]);System.out.print("] ");for (j = i+1; j < array.length; j++) {System.out.print(array[j] + " ");}System.out.println();}}public static void main(String[] args) {Integer array[] = { 46, 58, 15, 45, 90, 18, 10, 62 };sort(array);}}/*[46 58] 15 45 90 18 10 62 [15 46 58] 45 90 18 10 62 [15 45 46 58] 90 18 10 62 [15 45 46 58 90] 18 10 62 [15 18 45 46 58 90] 10 62 [10 15 18 45 46 58 90] 62 [10 15 18 45 46 58 62 90]  */