导航

Basic sort algorithms - CLRS 2nd edition, chapter 2

Posted on 2009-10-28 13:51  KevinXi  阅读(198)  评论(0)    收藏  举报

把几种非常基础的排序算法整理并实现了一下,在这贴出来,想看Shell和快排可以跳过,里面没有。 

  1 /**
  2  * @brief: sort algorithm in CLRS 2nd - chapter 2
  3  * @author: Kevin Xi
  4  *
  5  */
  6 
  7 /*************************************************************
  8  * INSERTION_SORT, running time cost O(n^2)
  9  *************************************************************/
 10 
 11 /**
 12  * @brief: pseudocode of insertion sort
 13 INSERTION-SORT(A)
 14     for j <- 2 to length[A]
 15         do key <- A[j]
 16         >> Insert A[j] into the sorted sequence A[1..j-1].
 17         i <- j-1
 18         while i > 0 and A[i] > key
 19             do A[i+1] <- A[i]
 20                 i <- i-1
 21         A[i+1] <- key
 22  */
 23 
 24 /**
 25  * @brief: ascending insertion sort
 26  */
 27 void ascending_insert(int A[], int len)
 28 {
 29     for (int j = 1; j < len; j++)
 30     {
 31         int key = A[j];
 32         // Insert A[j] into the sorted sequence A[1..j-1].
 33         int i = j-1;
 34         while (i >= 0 && A[i] > key)
 35         {
 36             A[i+1= A[i];
 37             i--;
 38         }
 39         A[i+1= key;
 40     }
 41 }
 42 
 43 /**
 44  * @brief: descending insertion sort
 45  */
 46 void descending_insert(int A[], int len)
 47 {
 48     for (int j = 1; j < len; j++)
 49     {
 50         int key = A[j];
 51         // Insert A[j] into the sorted sequence A[1..j-1].
 52         int i = j-1;
 53         while (i >= 0 && A[i] < key)
 54         {
 55             A[i+1= A[i];
 56             i--;
 57         }
 58         A[i+1= key;
 59     }
 60 }
 61 
 62 /*************************************************************
 63  * SELECTION_SORT, running time cost O(n^2)
 64  *************************************************************/
 65 
 66 /**
 67  * @brief: pseudocode of selection sort
 68 SELECTION-SORT(A)
 69     for i <- 1 to length[A]-1
 70         do j <- FIND(A, i, length[A])
 71         >> Find A[j] and exchange with A[i].
 72         A[j] <-> A[i]
 73  */
 74 
 75 /**
 76  * @brief: ascending selection sort
 77  */
 78 void ascending_select(int A[], int len)
 79 {
 80     for (int i = 0; i < len - 1; i++)
 81     {
 82         int key, j = i;
 83         // Find A[j] and exchange with A[i].
 84         for (int m = i; m < len; m++)
 85         {
 86             if (A[m] < A[j])
 87                 j = m;
 88         }
 89         if (j != i)
 90         {
 91             key  = A[j];
 92             A[j] = A[i];
 93             A[i] = key;
 94         }
 95     }
 96 }
 97 
 98 /**
 99  * @brief: descending selection sort
100  */
101 void descending_select(int A[], int len)
102 {
103     for (int i = 0; i < len - 1; i++)
104     {
105         int key, j = i;
106         // Find A[j] and exchange with A[i].
107         for (int m = i; m < len; m++)
108         {
109             if (A[m] > A[j])
110                 j = m;
111         }
112         if (j != i)
113         {
114             key  = A[j];
115             A[j] = A[i];
116             A[i] = key;
117         }
118     }
119 }
120 
121 /*************************************************************
122  * MERGE_SORT, running time cost O(nlogn)
123  *************************************************************/
124 
125 /**
126  * @brief: pseudocode of merge sort
127 MERGE(A, p, q, r)
128     n1 <- q-p+1
129     n2 <- r-q
130     create arrays L[1..n1+1] and R[1..n2+1]
131     for i <- 1 to n1
132         do L[i] <- A[p+i-1]
133     for j <- 1 to n2
134         do R[j] <- A[q+j]
135     L[n1+1] <- $
136     R[n2+1] <- $
137     i <- 1
138     j <- 1
139     for k <- p to r
140         do if L[i] <= R[j]
141             then A[k] <- L[i]
142                 i <- i+1
143             else A[k] <- R[j]
144                 j <- j+1
145 
146 MERGE-SORT(A, p, r)
147     if p < r
148         then q <- [(p+r)/2]
149         MERGE-SORT(A, p, q)
150         MERGE-SORT(A, q+1, r)
151         MERGE(A, p, q, r)
152  */
153 
154 /**
155  * @brief: ascending merge function
156  */
157 void ascending_merge_func(int A[], int p, int q, int r)
158 {
159     int a = p;
160     int b = q + 1;
161     int i = 0;
162     int* L = new int[r-p+1];
163     while ((a <= q) && (b <= r))
164     {
165         if (A[a] < A[b])
166             L[i++= A[a++];
167         else
168             L[i++= A[b++];
169     }
170     while (a <= q) L[i++= A[a++];
171     while (b <= r) L[i++= A[b++];
172 
173     for (int j = p; j <= r; j++)
174         A[j] = L[j-p];
175 
176     delete []L;
177 }
178 
179 /**
180  * @brief: descending merge function
181  */
182 void descending_merge_func(int A[], int p, int q, int r)
183 {
184     int a = p;
185     int b = q + 1;
186     int i = 0;
187     int* L = new int[r-p+1];
188     while ((a <= q) && (b <= r))
189     {
190         if (A[a] > A[b])
191             L[i++= A[a++];
192         else
193             L[i++= A[b++];
194     }
195     while (a <= q) L[i++= A[a++];
196     while (b <= r) L[i++= A[b++];
197 
198     for (int j = p; j <= r; j++)
199         A[j] = L[j-p];
200 
201     delete []L;
202 }
203 
204 /**
205  * @brief: ascending merge sort
206  */
207 void ascending_merge(int A[], int p, int r)
208 {
209     if (p < r)
210     {
211         int q = (p + r) / 2;
212         ascending_merge(A, p, q);
213         ascending_merge(A, q+1, r);
214         ascending_merge_func(A, p, q, r);
215     }
216 }
217 
218 /**
219  * @brief: descending merge sort
220  */
221 void descending_merge(int A[], int p, int r)
222 {
223     if (p < r)
224     {
225         int q = (p + r) / 2;
226         descending_merge(A, p, q);
227         descending_merge(A, q+1, r);
228         descending_merge_func(A, p, q, r);
229     }
230 }
231 
232 /*************************************************************
233  * CONCLUSION, sort algorithms running time cost
234  * 1. optimize insertion sort to nlogn with binary search
235  * 2. shell sort performed better than nlogn in time cost
236  * 3. try to combine insertion sort with merge sort
237  *************************************************************/

238