算法

一、冒泡排序:

1、算法思想:

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

2、实例分析:

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

 

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

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

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

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

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

4、算法代码:(Java版)

  1. /**
  2.  * 冒泡排序 算法
  3.  * @author xcbeyond
  4.  *
  5.  */
  6. public class BubbleSort {
  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 = 0;
  11.         int j = 0;
  12.         for(i=0;i<10;i++)
  13.             System.out.print(a[i]+”  “);
  14.         System.out.println();
  15.         for(i=0;i<a.length;i++)
  16.             for(j=0;j<a.length-i-1;j++)
  17.             {
  18.                 if(a[j]>a[j+1])
  19.                 {
  20.                     int temp;
  21.                     temp=a[j];
  22.                     a[j]=a[j+1];
  23.                     a[j+1]=temp;
  24.                 }
  25.             }
  26.         for(i=0;i<10;i++)
  27.             System.out.print(a[i]+”  “);
  28.     }
  29. }

二、选择排序

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、算法代码:

 

  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.         for(i = 0;i<a.length;i++)
  13.             System.out.print(a[i]+”  “);
  14.         System.out.println();
  15.         for(i = 0;i<a.length;i++){
  16.             int min = a[i];
  17.             for(j = i+1;j<a.length;j++){
  18.                 if(min>a[j]){
  19.                     int temp;
  20.                     temp = min;
  21.                     min = a[j];
  22.                     a[j] = temp;
  23.                 }
  24.             }
  25.             a[i] = min;
  26.         }
  27.         for(i = 0;i<a.length;i++)
  28.             System.out.print(a[i]+”  “);
  29.     }
  30. }

三、插入排序

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、算法代码:

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

 

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

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注