排序算法(冒泡排序、选择排序、插入排序)

来源:互联网 发布:8个灯流水灯单片机程序 编辑:程序博客网 时间:2024/06/03 00:02

一、冒泡排序:

    1、算法思想:

        对要排序的数据,从上到下依次比较两个相邻的数并加以调整,将最大的数向下移动,较小的数向上冒起。即:每一趟依次比较相邻的两个数据元素,将较小的数放在左边,循环进行同样的操作,直到全部待排序的数据元素排完。

    2、实例分析:

       例如:我们要将身高不等的十个人站在一排,要求他们按照身高由低到高排队,设将10个人编号为0---9 ,相邻的两个人依次比较,如果左边的比右边的人高,则交换两个人的位置,否则不交换,直到最后两个人,即此时完成了一趟排序。一趟排序后,最高的人已经在最右边了。

 

一趟排序后的结果:(将最高者一趟排序后移动到最后位置)

    如此都多趟的排序,最终就完成了整个的排序。

3、算法分析:(有小到大排序)

      1>、每一趟过程中,就是依次比较两个相邻的数,若a[i]>a[i+1],则交换两数,否则不换;

       2>、每一趟就是一重循环,而由于要经过多趟过程,即外面还有一重循环,所以就存在双重循环。

4、算法代码:(Java版)

[java] view plaincopyprint?
  1. /** 
  2.  * 冒泡排序 算法 
  3.  * @author xcbeyond 
  4.  * 
  5.  */  
  6.   
  7. public class BubbleSort {  
  8.   
  9.     public static void main(String[] args) {  
  10.         // TODO Auto-generated method stub  
  11.         int a[] ={13,15,37,89,60,39,12,109,56,72} ;  
  12.         int i = 0;  
  13.         int j = 0;  
  14.           
  15.         for(i=0;i<10;i++)  
  16.             System.out.print(a[i]+"  ");  
  17.         System.out.println();  
  18.           
  19.         for(i=0;i<a.length;i++)  
  20.             for(j=0;j<a.length-i-1;j++)  
  21.             {  
  22.                 if(a[j]>a[j+1])  
  23.                 {  
  24.                     int temp;  
  25.                     temp=a[j];  
  26.                     a[j]=a[j+1];  
  27.                     a[j+1]=temp;  
  28.                 }  
  29.             }  
  30.           
  31.         for(i=0;i<10;i++)  
  32.             System.out.print(a[i]+"  ");  
  33.           
  34.     }  
  35.   
  36. }  

二、选择排序

1、算法思想:

        将待排序序列分为两部分,一部分为有序序列,另一部分为无序序列。第一趟:从a[0]到a[n-1]中找到最小的数a[i],然后将a[i]与a[0]交换,第二趟:从a[1]到a[n-1]中找到最小的数a[j],然后将a[j]与a[1]交换,第三趟:从a[2]到a[n-1]中找到最小的数a[k],然后将a[k]与a[2]交换 ……

2、实例分析:

      {13,15,37,89,60,39,12,109,56,72}

第一趟 :12  {15,37,89,60,39,13,109,56,72}

第二趟:12 ,13 {37,89,60,39,15,109,56,72}

第三趟:12 ,13 ,15 {89,60,39,37,109,56,72}

……

3、算法代码:

 

[java] view plaincopyprint?
  1. /** 
  2.  * 选择排序 
  3.  * @author xcbeyond 
  4.  * 
  5.  */  
  6. public class SelectSort {  
  7.     public static void main(String[] args) {  
  8.         // TODO Auto-generated method stub  
  9.         int a[] ={13,15,37,89,60,39,12,109,56,72} ;  
  10.         int i;  
  11.         int j;  
  12.           
  13.         for(i = 0;i<a.length;i++)  
  14.             System.out.print(a[i]+"  ");  
  15.         System.out.println();  
  16.           
  17.         for(i = 0;i<a.length;i++){  
  18.             int min = a[i];  
  19.             for(j = i+1;j<a.length;j++){  
  20.                 if(min>a[j]){  
  21.                     int temp;  
  22.                     temp = min;  
  23.                     min = a[j];  
  24.                     a[j] = temp;  
  25.                 }  
  26.             }  
  27.             a[i] = min;  
  28.         }  
  29.           
  30.         for(i = 0;i<a.length;i++)  
  31.             System.out.print(a[i]+"  ");  
  32.     }  
  33.   
  34. }  


三、插入排序

1、算法思想:

    1〉从第一个元素开始,该元素可以认为已经被排序

    2〉取出第一个未排序元素存放在临时变量temp中,在已经排序的元素序列中从后往前扫描,逐一比较

    3〉如果temp小于已排序元素,将该元素移到下个位置

    4〉重复步骤3〉,直到找到已排序的元素小于或者等于

2、实例分析:

       {13,15,37,89,60,39,12,109,56,72}

第一趟:   13 {15,37,89,60,39,12,109,56,72}     temp = 15

                     temp>13

第二趟:   13 ,15 { 37,89,60,39,12,109,56,72}      temp = 37

                    temp>15         temp>13 
 

第三趟: 13,15 ,37  {89,60,39,12,109,56,72}     temp = 89

                   temp>37       temp>15       temp>13

第三趟: 13,15 ,37 ,89 {60,39,12,109,56,72}     temp =60

                   temp< 89:60<->89          13,15 ,37 , 60,89 {39,12,109,56,72}  

                    temp>37        temp>15        temp>13

第四趟: 13,15 ,37 , 60,89  {39,12,109,56,72}        temp = 39

                   temp<89:  39<->89           13,15 ,37 , 60,39,89 {12,109,56,72}   

                  temp<60  :   39<->60           13,15 ,37 , 39,60,89 {12,109,56,72}   

                   temp>37      temp>15         temp >13

第五趟:    13,15 ,37 , 39,60,89 {12,109,56,72}       temp = 12         

                    temp<89 :12<->89                   13,15 ,37 ,39,60, 12,89 {109,56,72}  

                    temp<60:12<->60                    13,15 ,37 ,39,12,60,89 {109,56,72}  

                    temp<39 :12<->39                   13,15 ,37 ,12,39,60,89 {109,56,72}  

                   temp<37 :12<->37                    13,15 ,12 ,37 ,39,60,89 {109,56,72}  

                   temp<15: 12<->15                   13,12,15 ,37 ,39,60,89 {109,56,72}  

                   temp<13 :12<->13                     12,13,15 ,37 ,39,60,89 {109,56,72}  

第六趟:  12,13,15 ,37 ,39,60,89 {109,56,72}           temp = 109

……

 

3、算法代码:

[java] view plaincopyprint?
  1. import java.util.Arrays;  
  2.   
  3. /** 
  4.  * 插入排序 
  5.  * @author xcbeyond 
  6.  * 
  7.  */  
  8. public class InsertSort {  
  9.   
  10.     public static void main(String[] args) {  
  11.         // TODO Auto-generated method stub  
  12.         int a[] ={13,15,37,89,60,39,12,109,56,72} ;  
  13.         a = insertSort(a);  
  14.         String s = Arrays.toString(a);  
  15.         System.out.println(s);  
  16.     }  
  17.       
  18.     public static int[] insertSort(int[] ary){  
  19.         for(int i = 1;i < ary.length;i++){  
  20.             int temp = ary[i];  
  21.             int j;  
  22.             for(j = i-1;j>=0 && temp < ary[j];j--){  
  23.                 ary[j+1] = ary[j];  
  24.             }  
  25.             ary[j+1] = temp;  
  26.         }  
  27.           
  28.         return ary;  
  29.     }  
  30. }  

另一种(易于理解):

[java] view plaincopyprint?
  1. import java.util.Arrays;  
  2.   
  3. /** 
  4.  * 插入排序 
  5.  * @author xcbeyond 
  6.  * 
  7.  */  
  8. public class InsertSort {  
  9.   
  10.     public static void main(String[] args) {  
  11.         // TODO Auto-generated method stub  
  12.         int a[] ={13,15,37,89,60,39,12,109,56,72} ;  
  13.         a = insertSort(a);  
  14.         String s = Arrays.toString(a);  
  15.         System.out.println(s);  
  16.     }  
  17.       
  18.     public static int[] insertSort(int[] ary){  
  19.         for(int i = 1;i < ary.length;i++){  
  20.             int temp = ary[i];  
  21.             int j;  
  22.             for(j = i-1;j>=0;j--){  
  23.                 if(temp < ary[j]){  
  24.                     ary[j+1] = ary[j];  
  25.                 }  
  26.                 else  
  27.                     break//找到插入位置  
  28.             }  
  29.             ary[j+1] = temp;  
  30.         }  
  31.           
  32.         return ary;  
  33.     }  
  34. }  
原创粉丝点击