1、选择排序
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | void SelectSort(ElemType* A,int n){
 int i,j,min;
 for(i=0;i<n-1;i++)
 {
 min=i;
 for(j=i+1;j<n;j++)
 {
 if(A[j]<A[min])
 {
 min=j;
 }
 }
 if(min!=i)
 {
 
 swap(A[i],A[min]);
 }
 }
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 
 | #include <stdio.h>#include <stdlib.h>
 #include <time.h>
 #include <string.h>
 typedef int ElemType;
 typedef struct{
 ElemType *elem;
 int TableLen;
 }SSTable;
 
 void ST_Init(SSTable &ST,int len)
 {
 ST.TableLen=len;
 ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen);
 int i;
 srand(time(NULL));
 for(i=0;i<ST.TableLen;i++)
 {
 ST.elem[i]=rand()%100;
 }
 }
 void ST_print(SSTable ST)
 {
 for(int i=0;i<ST.TableLen;i++)
 {
 printf("%3d",ST.elem[i]);
 }
 printf("\n");
 }
 void swap(ElemType &a,ElemType &b)
 {
 ElemType tmp;
 tmp=a;
 a=b;
 b=tmp;
 }
 
 void SelectSort(ElemType* A,int n)
 {
 int i,j,min;
 for(i=0;i<n-1;i++)
 {
 min=i;
 for(j=i+1;j<n;j++)
 {
 if(A[j]<A[min])
 {
 min=j;
 }
 }
 if(min!=i)
 {
 
 swap(A[i],A[min]);
 }
 }
 }
 
 int main() {
 SSTable ST;
 ST_Init(ST,10);
 
 
 
 ST_print(ST);
 SelectSort(ST.elem,10);
 ST_print(ST);
 return 0;
 }
 
 | 
2、堆排序
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | void SelectSort(ElemType* A,int n){
 int i,j,min;
 for(i=0;i<n-1;i++)
 {
 min=i;
 for(j=i+1;j<n;j++)
 {
 if(A[j]<A[min])
 {
 min=j;
 }
 }
 if(min!=i)
 {
 
 swap(A[i],A[min]);
 }
 }
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 
 | #include <stdio.h>#include <stdlib.h>
 #include <time.h>
 #include <string.h>
 typedef int ElemType;
 typedef struct{
 ElemType *elem;
 int TableLen;
 }SSTable;
 
 void ST_Init(SSTable &ST,int len)
 {
 ST.TableLen=len;
 ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen);
 int i;
 srand(time(NULL));
 for(i=0;i<ST.TableLen;i++)
 {
 ST.elem[i]=rand()%100;
 }
 }
 void ST_print(SSTable ST)
 {
 for(int i=0;i<ST.TableLen;i++)
 {
 printf("%3d",ST.elem[i]);
 }
 printf("\n");
 }
 void swap(ElemType &a,ElemType &b)
 {
 ElemType tmp;
 tmp=a;
 a=b;
 b=tmp;
 }
 
 void SelectSort(ElemType* A,int n)
 {
 int i,j,min;
 for(i=0;i<n-1;i++)
 {
 min=i;
 for(j=i+1;j<n;j++)
 {
 if(A[j]<A[min])
 {
 min=j;
 }
 }
 if(min!=i)
 {
 
 swap(A[i],A[min]);
 }
 }
 }
 
 int main() {
 SSTable ST;
 ST_Init(ST,10);
 
 
 
 ST_print(ST);
 SelectSort(ST.elem,10);
 ST_print(ST);
 return 0;
 }
 
 | 
3、归并排序
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | void SelectSort(ElemType* A,int n){
 int i,j,min;
 for(i=0;i<n-1;i++)
 {
 min=i;
 for(j=i+1;j<n;j++)
 {
 if(A[j]<A[min])
 {
 min=j;
 }
 }
 if(min!=i)
 {
 
 swap(A[i],A[min]);
 }
 }
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 
 | #include <stdio.h>#include <stdlib.h>
 #include <time.h>
 #include <string.h>
 typedef int ElemType;
 typedef struct{
 ElemType *elem;
 int TableLen;
 }SSTable;
 
 void ST_Init(SSTable &ST,int len)
 {
 ST.TableLen=len;
 ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen);
 int i;
 srand(time(NULL));
 for(i=0;i<ST.TableLen;i++)
 {
 ST.elem[i]=rand()%100;
 }
 }
 void ST_print(SSTable ST)
 {
 for(int i=0;i<ST.TableLen;i++)
 {
 printf("%3d",ST.elem[i]);
 }
 printf("\n");
 }
 void swap(ElemType &a,ElemType &b)
 {
 ElemType tmp;
 tmp=a;
 a=b;
 b=tmp;
 }
 
 void SelectSort(ElemType* A,int n)
 {
 int i,j,min;
 for(i=0;i<n-1;i++)
 {
 min=i;
 for(j=i+1;j<n;j++)
 {
 if(A[j]<A[min])
 {
 min=j;
 }
 }
 if(min!=i)
 {
 
 swap(A[i],A[min]);
 }
 }
 }
 
 int main() {
 SSTable ST;
 ST_Init(ST,10);
 
 
 
 ST_print(ST);
 SelectSort(ST.elem,10);
 ST_print(ST);
 return 0;
 }
 
 |