常用排序算法(选择排序)

每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。 选择排序是不稳定的排序方法。
更多信息请参考:http://baike.baidu.com/view/547263.htm

C语言代码:

  1 #include <stdlib.h>
  2 #include <stdio.h>
  3 #include <memory.h>
  4 #include <time.h>
  5 
  6 void init(int *array, int count)
  7 {
  8     int n = 0;
  9     srand((unsigned int)time(NULL));
 10 
 11     for (n=0; n<count; n++)
 12     {
 13         array[n] = rand()%100 + 1;
 14     }
 15 }
 16 
 17 void output(int *array, int count)
 18 {
 19     int n = 0;
 20     for (n=0; n<count; n++)
 21     {
 22         printf("%5d", array[n]);
 23     }
 24 
 25     printf("\n");
 26 }
 27 
 28 /* 选择排序 */
 29 void selectsort(int* array, int count, int type) 
 30 {
 31     int i = 0;
 32     int j = 0;
 33     int index = 0;
 34     int temp = 0;
 35     if (type == 0) 
 36     { 
 37         //正排序,从小排到大
 38         for (i = 0; i < count; i++) 
 39         {
 40             index = i;//这里不是index=0
 41             for (j = i+1; j<count; j++)
 42             {
 43                 if (array[index]>array[j])
 44                 {
 45                     index = j;
 46                 }
 47             }
 48 
 49             if(index!=i)
 50             {
 51                 temp=array[i];
 52                 array[i]=array[index];
 53                 array[index]=temp;
 54             }
 55         }
 56     } 
 57     else
 58     { 
 59         //倒排序,从大排到小
 60         for (i = 0; i<count; i++) 
 61         {
 62             index = i;//这里不是index=0
 63             for (j =i+1;j<count; j++)
 64             {
 65                 if (array[index]<array[j])
 66                 {
 67                     index = j;  
 68                 }
 69             }
 70 
 71             //交换在位置i和index(最大值)两个数
 72             if(index!=i)
 73             {
 74                 temp=array[i];
 75                 array[i]=array[index];
 76                 array[index]=temp;
 77             }                 
 78         }
 79     } 
 80 }
 81 
 82 int main()
 83 {
 84     const int count = 10;
 85     int array[count];
 86 
 87     memset(array, 0, sizeof(int)*count);
 88 
 89     init(array, count);
 90     
 91     printf("data before sort:      ");
 92     output(array, count);
 93 
 94     selectsort(array, count, 0);  // 正排序,从小排到大
 95     printf("data after sort(asc):  ");
 96     output(array, count);
 97 
 98     selectsort(array, count, 1);  // 倒排序,从大排到小
 99     printf("data after sort(desc): ");
100     output(array, count);
101     
102     return 0;
103 }

运行结果如下:
data before sort:         96   75   98   99   95   21   76   22   68   57
data after sort(asc):     21   22   57   68   75   76   95   96   98   99
data after sort(desc):    99   98   96   95   76   75   68   57   22   21


Java代码:

  1 import java.util.Random;  
  2   
  3 /* 直接选择排序法----选择排序的一种  
  4 * 方法:每一趟从待排序的数据元素中选出最小(或最大)的一个元素,  
  5 * 顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。  
  6 */    
  7   
  8   
  9 public class Sort   
 10 {  
 11     /* 
 12      * 输出数组中的数据 
 13      */  
 14     public static void OutputArray(int[] array)   
 15     {  
 16         for (int data : array)   
 17         {  
 18             System.out.print(data + "\t");  
 19         }  
 20           
 21         System.out.println();  
 22     }  
 23       
 24     /* 
 25      * 生成需要排序的数组 
 26      */  
 27     public static int[] createArray(int count)   
 28     {  
 29         int array[] = new int[count];  
 30         Random r = new Random();  
 31           
 32         for (int i = 0; i < count; i++)   
 33         {  
 34             array[i] = r.nextInt(100);  
 35         }  
 36           
 37         System.out.println("");  
 38         System.out.print("data before sort:\t");  
 39           
 40         OutputArray(array);  
 41           
 42         return array;  
 43     }  
 44       
 45      /* 
 46       * 选择排序 
 47       */  
 48      public static void selectSort(int[] array, String sortType)   
 49      {  
 50          int temp,index;  
 51          if (sortType.equals("asc"))   
 52          {   
 53              //正排序,从小排到大  
 54              for (int i = 0; i < array.length; i++)   
 55              {  
 56                  index = i;//这里不是index=0  
 57                  for (int j = i+1; j <array.length; j++)  
 58                  {  
 59                      if (array[index]>array[j])  
 60                      {  
 61                          index = j;  
 62                      }  
 63                  }  
 64                    
 65                  if(index!=i)  
 66                  {  
 67                      temp=array[i];  
 68                      array[i]=array[index];  
 69                      array[index]=temp;  
 70                  }  
 71              }  
 72          }   
 73          else if (sortType.equals("desc"))   
 74          {   
 75              //倒排序,从大排到小  
 76              for (int i = 0; i <array.length; i++)   
 77              {  
 78                  index = i;//这里不是index=0  
 79                  for (int j =i+1;j <array.length; j++)  
 80                  {  
 81                      if (array[index]<array[j])  
 82                      {  
 83                          index = j;    
 84                      }  
 85                  }  
 86                    
 87                  //交换在位置i和index(最大值)两个数  
 88                  if(index!=i)  
 89                  {  
 90                      temp=array[i];  
 91                      array[i]=array[index];  
 92                      array[index]=temp;  
 93                  }                   
 94              }  
 95          }   
 96          else   
 97          {  
 98              System.out.println("您输入的排序类型错误!");  
 99          }  
100      }  
101       
102     public static void main(String[] args)   
103     {  
104         int[] arr1=createArray(10);  
105           
106         System.out.print("data after sort(asc):\t");  
107         selectSort(arr1, "asc");  
108         OutputArray(arr1);  
109           
110         System.out.print("data after sort(desc):\t");  
111         selectSort(arr1, "desc");  
112         OutputArray(arr1);  
113     }  
114 }  


输出结果如下:

data before sort:    83    49    67    32    41    4    9    15    24    21    
data after sort(asc):    4    9    15    21    24    32    41    49    67    83    
data after sort(desc):    83    67    49    41    32    24    21    15    9    4  

转载于:https://www.cnblogs.com/yuanping/archive/2012/12/24/2831630.html

猜你喜欢

转载自blog.csdn.net/weixin_34319111/article/details/94067231