面试常见5个算法套装,4个排序+二分查找

面试常见的要你手写的算法,今天复习了一下,表示heapsort没法一次写对。

  1 #include "stdio.h"
  2 #include "stdlib.h"
  3 
  4 #define LEFT(i) (2 * i + 1)
  5 #define RIGHT(i) (2 * i + 2)
  6 
  7 void printArray(int a[], int len);
  8 void mergearray(int a[], int head, int mid, int tail);
  9 
 10 void heapify(int a[], int len, int i)
 11 {
 12     int left = LEFT(i);
 13     int right = RIGHT(i);
 14     int max = i;
 15     
 16     if(left < len && a[left] > a[max])
 17     {
 18         max = left;
 19         
 20     }
 21     if(right < len && a[right] > a[max])
 22     {
 23         max = right;
 24     }
 25     
 26     if(max != i)
 27     {
 28         int mid = a[i];
 29         a[i] = a[max];
 30         a[max] = mid;
 31         heapify(a, len, max);
 32     }
 33 }
 34 
 35 void heapcreate(int a[], int len)
 36 {
 37     int i = len / 2;
 38     
 39     for(; i>=0; i--)
 40     {
 41         heapify(a, len, i);
 42         //printArray(a, len);
 43     }
 44 }
 45 
 46 void hs(int a[], int len)
 47 {
 48     int i = len - 1;
 49     heapcreate(a, len);
 50     
 51     //printArray(a, len);
 52     
 53     for(; i > 0; i--)
 54     {
 55         int mid = a[i];
 56         a[i] = a[0];
 57         a[0] = mid;
 58         heapify(a, i, 0);
 59         
 60         printArray(a, len);
 61     }
 62 }
 63 
 64 void qs(int a[], int start, int end)
 65 {
 66     if(start < end)
 67     {
 68         int position = partition(a, start, end);
 69         qs(a, start, position - 1);
 70         qs(a, position + 1, end);
 71     }
 72 }
 73 
 74 int partition(int a[], int start, int end)
 75 {
 76     int pivot = a[end];
 77     int i = start - 1;
 78     int j = start;
 79     for(; j < end; j++)
 80     {
 81         if(a[j] < pivot)
 82         {
 83             i++;
 84             int mid = a[i];
 85             a[i] = a[j];
 86             a[j] = mid;
 87         }
 88     }
 89     
 90     i++;
 91     a[j] = a[i];
 92     a[i] = pivot;
 93     return i;        
 94 }
 95 
 96 void printArray(int a[], int len)
 97 {
 98    int i;
 99    for(i = 0; i < len; i++)
100    {
101         printf("%d,", a[i]);
102    }
103    printf("\n");
104 }
105 
106 int binarySearchUnrecursion(int a[], int start, int end, int key)
107 {
108     int head = start;
109     int tail = end;
110     
111     while(head < tail)
112     {
113         int index = (head + tail) / 2;
114         if(a[index] == key)
115         {
116             return index;
117         }
118         else{
119             if(a[index] > key)
120             {
121                 tail = index - 1;
122             }
123             else
124             {
125                 head = index + 1;
126             }
127         }
128     }
129     
130     return -1;
131 }
132 
133 int binarySearch(int a[], int start, int end, int key)
134 {
135     if(start <= end)
136     {
137         int index = (start + end) / 2;
138         if(a[index] == key)
139         {
140             return index;
141         }
142         else{
143             if(a[index] > key)
144             {
145                 return binarySearch(a, start, index-1, key);
146             }
147             else
148             {
149                 return binarySearch(a, index + 1, end, key);
150             }
151             
152         }
153     }
154     else{
155         return -1;
156     }
157 }
158 
159 void ms(int a[], int start, int end)
160 {
161     if(start < end){
162         int mid = (start + end) / 2;
163         ms(a, start, mid);
164         ms(a, mid + 1, end);
165         
166         mergearray(a, start, mid, end);
167     }
168 }
169 
170 void mergearray(int a[], int head, int mid, int tail)
171 {
172     int i = head;
173     int j = mid + 1;
174     int k = 0;
175     
176     int *temp = (int*)malloc(sizeof(int) * (tail - head + 1));
177     
178     while(i <= mid && j <= tail)
179     {
180         if(a[i] <= a[j])
181         {
182             temp[k] = a[i];
183             i = i + 1;
184         }
185         else
186         {
187             temp[k] = a[j];
188             j = j + 1;    
189         }
190         k = k + 1;
191     }
192     
193     while(i <= mid)
194     {
195         temp[k] = a[i];
196         k = k + 1;
197         i = i + 1;
198     }
199     
200     while(j <= tail)
201     {
202         temp[k] = a[j];
203         k = k + 1;
204         j = j + 1;
205     }
206     
207     for(i = 0; i < (tail - head + 1); i++)
208     {
209         a[ head + i] = temp[i];
210     }
211     
212 }
213 
214 void is(int a[], int len)
215 {
216     int i,j;
217     for(i = 1; i < len; i++)
218     {
219         int val = a[i];    
220         for(j = i - 1; j >=0; j--)
221         {
222             if(a[j] > val){
223                 a[j + 1] = a[j];
224                 if(j == 0)
225                 {
226                     a[j] = val;
227                     continue;
228                 }
229             }
230             else{
231                 a[j] = val;
232                 continue;
233             }
234         }
235     }
236 }
237 
238 void main()
239 {
240     int a[]={
241         5,4,3,2,1
242     };
243 
244     int b[]={
245         1,2,3,4,5
246     };
247     //qs(a, 0, 4);
248     //hs(a, 5);
249     //ms(a, 0, 4);
250     //is(a, 5);
251     printArray(a, 5);
252     
253     //printf("%d", binarySearchUnrecursion(a, 0, 4, 3));
254 
255 }

 

posted on 2014-09-12 14:53  itat  阅读(220)  评论(0编辑  收藏  举报

导航