c array sort

  1 //
  2 // Created by jia on 19/12/18.
  3 //
  4 #include "../header.h"
  5 //information
  6 void print_label(char* string){
  7     printf("%s\n", string);
  8 }
  9 //oupt array
 10 void arr_output(int n, int arr[]){
 11     for(int i = 0; i < n; i ++){
 12         printf("arr[%d] = %4d\t", i, arr[i]);
 13     }
 14 
 15     printf("\n\n");
 16 }
 17 //swap two datas to make sure the first is smaller than the second
 18 void data_swap(int* a, int* b){
 19     if(*a > *b){
 20         int temp = *a;
 21         *a = *b;
 22         *b = temp;
 23     }
 24 }
 25 
 26 //improved bubble sort
 27 void improvedbubblesort(int n, int arr[]) {
 28     bool sorted = true;
 29     for (int i = 0; i < n; i++)
 30         for (int j = 0; j < n - i - 1; j++) {
 31             data_swap(&arr[j], &arr[j + 1]);
 32             sorted = false;
 33         }
 34     if (sorted) {
 35         return;
 36     }
 37     arr_output(n, arr);
 38 }
 39 //bubble sort
 40 void bubblesort(int n, int arr[]){
 41     for(int i = 0; i < n; i ++)
 42         for(int j = 0; j < n - i - 1; j ++){
 43             data_swap(&arr[j], &arr[j + 1]);
 44         }
 45     arr_output(n, arr);
 46 }
 47 //other sort
 48 void othersort(int n, int arr[]){
 49     for(int i = 0; i < n; i ++)
 50         for(int j = i; j < n; j ++){
 51             data_swap(&arr[i], &arr[j]);
 52         }
 53     arr_output(n, arr);
 54 }
 55 
 56 //insertion sort
 57 void insertionsort(int n, int arr[]){
 58     for(int i = 0; i < n; i ++){
 59         int temp = arr[i];
 60         bool hasmoved = false;
 61         bool notplaced = true;
 62         for(int j = i - 1; j >= 0; j --){
 63             if(arr[j] > temp){
 64                 //shift numvers till you find it's right location
 65                 arr[j + 1] = arr[j];
 66                 hasmoved = true;
 67             }else{
 68                 arr[j+1] = temp;
 69                 notplaced = false;
 70                 break;
 71             }
 72         }
 73         if(hasmoved && notplaced){
 74             arr[0] = temp;
 75         }
 76     }
 77     arr_output(n, ARR);
 78 }
 79 
 80 void selectionsort(int n, int arr[]){
 81     for(int j = n -1; j < n; j --){
 82         int pos = 0;
 83         for(int i = 1; i <= j; i ++){
 84             if(arr[i] > arr[pos]){
 85                 pos = i;
 86             }
 87             data_swap(&arr[pos], &arr[j]);
 88         }
 89     }
 90     arr_output(n, ARR);
 91 }
 92 void mergesort(int n, int arr[]){
 93 
 94 }
 95 
 96 //void (*cmc_result)(int n, int arr[]) function pointer to point the function
 97 void getsort(int t, void (*cmc_result)(int n, int arr[])){
 98     switch(t){
 99         case 0:{
100             print_label("bubble sort:");
101             //bubblesort(N, ARR);
102             cmc_result(N, ARR);
103         } break;
104         case 1:{
105             print_label("other sort:");
106             othersort(N, ARR);
107             //cmc_result(N, ARR);
108 
109             //cmc_result = othersort;
110         } break;
111         case 2:{
112             print_label("improved sort:");
113             cmc_result(N, ARR);
114         } break;
115         case 3:{
116             print_label("insertion sort:");
117             cmc_result(N, ARR);
118             //cmc_result = othersort;
119         } break;
120         case 4:{
121             print_label("selection sort:");
122             cmc_result(N, ARR);
123         } break;
124     }
125 }

猜你喜欢

转载自www.cnblogs.com/zjhangia/p/10231103.html