面试:排序算法实现

  1 /*
  2  * testmain.cpp
  3  *
  4  *  Created on: 2017年7月16日
  5  *      Author: Administrator
  6  */
  7 
  8 #include <utility>
  9 #include <algorithm>
 10 #include <cstring>
 11 #include <vector>
 12 #include <list>
 13 #include <iostream>
 14 
 15 void bubbleSort(int a[], int length) {
 16     if (a == nullptr || length < 2)
 17         return;
 18     for (int i = 0; i < length - 1; i++) {
 19         for (int j = length - 1; j > i; j--) {
 20             if (a[j - 1] > a[j]) {
 21                 std::swap(a[j - 1], a[j]);
 22             }
 23         }
 24     }
 25 }
 26 
 27 void insertSort(int a[], int length) {
 28     if (a == nullptr || length < 2)
 29         return;
 30 
 31     for (int i = 1; i < length; i++) {
 32         if (a[i - 1] > a[i]) {
 33             int tmp = a[i];
 34             int j;
 35             for (j = i - 1; j >= 0 && a[j] > tmp; j--) {
 36                 a[j + 1] = a[j];
 37             }
 38             a[j + 1] = tmp;
 39         }
 40     }
 41 }
 42 
 43 void selectSort(int a[], int length) {
 44     if (a == nullptr || length < 2) return;
 45     for (int i = 0; i < length - 1; i++) {
 46         int minIndex = i;
 47         for (int j = i + 1; j < length; j++) {
 48             if (a[j] < a[minIndex]) {
 49                 minIndex = j;
 50             }
 51         }
 52         if (i != minIndex) {
 53             std::swap(a[i], a[minIndex]);
 54         }
 55     }
 56 }
 57 
 58 void shellSort(int a[], int length) {
 59     if (a == nullptr || length < 2)  return;
 60     for (int gap = length / 2; gap >= 1; gap /= 2) {
 61         for (int i = gap; i < length; i++) {
 62             if (a[i - gap] > a[i]) {
 63                 int tmp = a[i];
 64                 int j;
 65                 for (j = i - gap; j >= 0 && a[j] > tmp; j -= gap) {
 66                     a[j + gap] = a[j];
 67                 }
 68                 a[j + gap] = tmp;
 69             }
 70         }
 71     }
 72 }
 73 
 74 void quickSort(int a[], int start, int end) {
 75     if (a == nullptr || start >= end)
 76         return;
 77 
 78     int pivotVal = a[start];
 79     int left = start;
 80     int right = end;
 81 
 82     while (left < right) {
 83         while (left < right && a[right] >= pivotVal)
 84             right--;
 85         a[left] = a[right];
 86         while (left < right && a[left] <= pivotVal)
 87             left++;
 88         a[right] = a[left];
 89     }
 90     a[left] = pivotVal;
 91     quickSort(a, start, left - 1);
 92     quickSort(a, left + 1, end);
 93 }
 94 
 95 void mergeSort(int a[], int start, int end) {
 96     if (a == nullptr || start >= end)
 97         return;
 98     int mid = start + (end - start) / 2;
 99     mergeSort(a, start, mid);
100     mergeSort(a, mid + 1, end);
101 
102     //merge operation
103     int *tmp = new int[end - start + 1];
104     int left = start;
105     int right = mid + 1;
106     int k = 0;
107     while (left <= mid && right <= end) {
108         if (a[left] <= a[right]) {
109             tmp[k++] = a[left++];
110         } else {
111             tmp[k++] = a[right++];
112         }
113     }
114 
115     while (left <= mid) {
116         tmp[k++] = a[left++];
117     }
118     while (right <= end) {
119         tmp[k++] = a[right++];
120     }
121     for (int i = 0; i < k; i++) {
122         a[start + i] = tmp[i];
123     }
124     delete[] tmp;
125 }
126 
127 void radixSort(int a[], int length) {
128     if (a == nullptr || length < 2)
129         return;
130 
131     //maxValue and d;
132     int maxVal = a[0];
133     for (int i = 1; i < length; i++) {
134         maxVal = std::max(maxVal, a[i]);
135     }
136     int d = 0;
137     while (maxVal > 0) {
138         d++;
139         maxVal /= 10;
140     }
141 
142     int radix = 1;
143     int* count = new int[10];
144     int* tmp = new int[length];
145     for (int n = 0; n < d; n++) {
146         memset(count, 0, sizeof(int) * 10);
147         for (int i = 0; i < length; i++) {
148             count[a[i] / radix % 10]++;
149         }
150 
151         for (int i = 1; i < 10; i++) {
152             count[i] += count[i - 1];
153         }
154 
155         for (int i = length - 1; i >= 0; i--) {
156             tmp[--count[a[i] / radix % 10]] = a[i];
157         }
158 
159         for (int i = 0; i < length; i++) {
160             a[i] = tmp[i];
161         }
162         radix *= 10;
163     }
164     delete[] count;
165     delete[] tmp;
166 }
167 
168 // small range 0 ~ maxVal
169 void countSort(int a[], int length) {
170     if (a == nullptr || length < 2)
171         return;
172 
173     int maxVal = a[0];
174     for (int i = 1; i < length; i++) {
175         maxVal = std::max(a[i], maxVal);
176     }
177 
178     int* tmp = new int[maxVal + 1];
179     memset(tmp,0,sizeof(int)*(maxVal+1));
180 
181     for (int i = 0; i < length; i++) {
182         tmp[a[i]]++;
183     }
184     int k = 0;
185     for (int i = 0; i <= maxVal; i++) {
186         for (int j = 0; j < tmp[i]; j++) {
187             a[k++] = i;
188         }
189     }
190     delete[] tmp;
191 }
192 
193 //10 bucktets;range 0~99
194 void bucketSort(int a[], int length) {
195     if (a == nullptr || length < 2)
196         return;
197     std::vector<std::list<int>> buckets(10);
198     for (int i = 0; i < length; i++) {
199         buckets[a[i] / 10].push_back(a[i]);
200     }
201     //sort every buckets and merge
202     int k = 0;
203     for (size_t i = 0; i < buckets.size(); i++) {
204         buckets[i].sort();
205         for (int num : buckets[i]) {
206             a[k++] = num;
207         }
208     }
209 }
210 
211 void heapAdjust(int a[],int start,int end){
212     int leftChild = 2*start + 1;
213     int rightChild = 2*start + 2;
214 
215     while(rightChild <= end){
216         if(a[start] >= a[leftChild] && a[start] >= a[rightChild]){
217             break;
218         }
219 
220         if(a[leftChild] >= a[rightChild]){
221             std::swap(a[start],a[leftChild]);
222             start = leftChild;
223         }else{
224             std::swap(a[start],a[rightChild]);
225             start = rightChild;
226         }
227         leftChild = 2 * start + 1;
228         rightChild = leftChild+1;
229     }
230     if(leftChild <= end && a[leftChild] > a[start]){
231         std::swap(a[leftChild],a[start]);
232     }
233 
234 }
235 
236 void heapSort(int a[],int length){
237 
238     if(a == nullptr  || length < 2) return;
239 
240 
241     for(int i=length/2-1;i >=0;i--){
242         heapAdjust(a,i,length-1);
243     }
244     // max heap
245     for(int i=length-1;i>=0;i--){
246         std::swap(a[0],a[i]);
247         heapAdjust(a,0,i-1);
248     }
249 }
250 
251 
252 void printArray(int a[],int length){
253     for(int i=0;i<length;i++){
254         std::cout << a[i] << ",";
255     }
256     std::cout << '\n';
257 }
258 
259 
260 
261 
262 int main() {
263     int a[] = {1,8,4,2,5,3,7,6};
264     printArray(a,8);
265 
266     bubbleSort(a,8);
267 //    insertSort(a,8);
268 //    selectSort(a,8);
269 //    shellSort(a,8);
270 //    quickSort(a,0,7);
271 //    mergeSort(a,0,7);
272 //    radixSort(a,8);
273 //    countSort(a,8);
274 //    bucketSort(a,8);
275 //    heapSort(a,8);
276     printArray(a,8);
277 
278     return 0;
279 }

 

posted @ 2017-07-19 11:45  wxquare  阅读(313)  评论(0编辑  收藏  举报