Common sorting

Common sorting

Direct insertion sort, insertion sort bipartite, Shell sort, bubble sort, quick discharge, a simple selection sort, merge sort

  . 1 #include <the iostream>
   2  the using  namespace STD;
   . 3  
  . 4  / * *
   . 5  * insertion sort
   6  * / 
  7  // direct insertion sort 
  . 8  void insertSort ( char Sourse [], int n-) {
   . 9      for ( int I = 2 ; I <= n-; ++ I) {
 10          IF (Sourse [I] <Sourse [I- . 1 ]) {
 . 11              Sourse [ 0 ] = Sourse [I];
 12 is              int J;
 13 is              for(j=i-1; sourse[j]>sourse[0]; --j) {
 14                 sourse[j+1] = sourse[j];
 15             }
 16             sourse[j+1] = sourse[0];
 17         }
 18     }
 19 }
 20 
 21 //二分查找
 22 int binarySearch(char sourse[], int low, int high, char aim) {
 23     int mid = (low + high)/2;
 24     while(low <= high) {
 25         if(sourse[mid] == aim) {
 26             low = mid;
 27             break;
 28         }
 29         if(sourse[mid] > aim) {
 30             high = mid - 1;
 31         } else {
 32             low = mid + 1;
 33         }
 34         mid = (low + high)/2;
 35     }
 36 
 37     return low;
 38 }
 39 
 40 //折半插入排序
 41 void insertHalfSort(char sourse[], int n) {
 42     for(int i=2; i<=n; ++i) {
 43         if(sourse[i-1] > sourse[i]) {
 44             sourse[0] = sourse[i];
 45             int pos = binarySearch(sourse, 1, i-1, sourse[0]);
 46             for(int j=i-1; j>=pos; --j) {
 47                 sourse[j+1] = sourse[j];
 48             }
 49             sourse[pos] = sourse[0];
 50         }
 51     }
 52 }
 53 
 54 //希尔排序
 55 void shellSort(char sourse[], int n) {
 56     for(int step=n/2; step>=1; step=step/2) {
 57         for(int i=step+1; i<=n; ++i) {
 58             if(sourse[i] < sourse[i-step]) {
 59                 sourse[0] = sourse[i];
 60                 int j;
 61                 for(j = i-step; sourse[0] < sourse[j] && j>0; j=j-step) {
 62                     sourse[j+step] = sourse[j];
 63                 }
 64                 sourse[j+step] = sourse[0];
 65             }
 66         }
 67     }
 68 }
 69  
70  
71 is  
72  / * ******************************************* ************************************************** ************* * / 
73 is  
74  
75  
76  / * *
 77  * A sorting
 78  * / 
79  // bubble sort 
80  void bubbleSort ( char Sourse [], int n-) {
 81      for ( int I = . 1 ; I <n-; ++ I) {
 82          BOOL isChanged = to false ;
 83          for ( int j=1; j<n-i+1; ++j) {
 84             if(sourse[j]>sourse[j+1]) {
 85                 swap(sourse[j], sourse[j+1]);
 86                 isChanged = true;
 87             }
 88         }
 89         if(!isChanged) {
 90             break;
 91         }
 92     }
 93 }
 94 
 95 //快排
 96 int partition(char sourse[], int low, int high) {
 97     sourse[0] = sourse[low];
 98     while(low<high) {
 99         while(low<high && sourse[high] >= sourse[0])
100             --high;
101         sourse[low] = sourse[high];
102         while(low<high && sourse[low] <= sourse[0])
103             ++low;
104         sourse[high] = sourse[low];
105     }
106     sourse[low]=sourse[0];
107     return low;
108 }
109 void quickSort(char sourse[], int low, int high) {
110     if(low >=high) {
111         return;
112     }
113     int pos = partition(sourse,low,high);
114     quickSort(sourse, low, pos-1);
115     quickSort(sourse, pos+1, high);
116 }
117 
118 
119 /*************************************************** ************************************************** ****** * / 
120  
121  / * *
 122  * selection Sort
 123  * / 
124  // simple selection sort 
125  void SelectSort ( char Sourse [], int n-) {
 126      for ( int I = . 1 ; I <= n-; ++ I) {
 127          int MINP = I;
 128          for ( int J = I; J <= n-; ++ J) {
 129              IF (Sourse [J] < Sourse [MINP]) {
 130.                 minp = j;
131             }
132         }
133         swap(sourse[i], sourse[minp]);
134     }
135 }
136 
137 
138 /**
139 *二路归并排序
140 */
141 char medim[14];
142 void merge(char sourse[], int low, int mid, int high) {
143 
144     for(int i=low; i<=high; i++) {
145         medim[i] = sourse[i];
146     }
147     int i=low, j=mid+1;
148     int k=low;
149     while(i<=mid && j<=high) {
150         if(medim[i]<=medim[j]) {
151             sourse[k]=medim[i];
152             i++;
153         } else {
154             sourse[k]=medim[j];
155             j++;
156         }
157         k++;
158     }
159     while(i<=mid) {
160         sourse[k]=medim[i];
161         i++;
162         k++;
163     }
164     while(j<=high) {
165         sourse[k]=medim[j];
166         k++;
167         j++;
168     }
169 
170 }
171 void mergeSort(char sourse[], int low, int high) {
172     if(low >= high) {
173         return;
174     }
175     int mid = (low + high)/2;
176     mergeSort(sourse, low, mid);
177     mergeSort(sourse, mid+1, high);
178     merge(sourse, low, mid, high);
179 }
180 
181 
182 
183 void show(char sourse[], int n) {
184     for(int i=1; i<=n; i++) {
185         cout<<sourse[i];
186     }
187     cout<<endl;
188 }
189 
190 int main() {
191     char sourse[]= {' ','j', 'k','d','g','e', 'l', 'm','h','a','n','f','c', 'i','b'};
192 
193 //    insertSort(sourse, 14);
194 //    insertSort(sourse, 14);
195 //    shellSort(sourse, 14);
196 //    bubbleSort(sourse, 14);
197 //    quickSort(sourse, 1, 14);
198 //    selectSort(sourse, 14);
199     mergeSort(sourse, 1, 14);
200     show(sourse, 14);
201 
202     return 0;
203 }

 

Guess you like

Origin www.cnblogs.com/zhishoumuguinian/p/11706860.html