复习的几种排序算法
1、快速排序
它基于分治的思想,将待排序的数组a[0...n- 1]分为a[0...p-1],a[p + 1...n-1]两部分,其中a[p]为中枢值,左边的值比它小,右边的值比它大。这样对左右两部分进行递归调用。
快速排序的 C代码:
1 void quicksort(int* intarray, int startpos, int endpos) 2 { 3 int* startp = intarray + startpos; 4 int* endp = intarray + endpos; 5 int len = endpos - startpos + 1; 6 int zhou; 7 int direction = 0; //0代表从j指针取值,1代表从i指针取值 8 if (startpos == endpos || endpos < 0) 9 return; 10 else 11 { 12 zhou = *(intarray); 13 while (startp != endp) 14 { 15 if (direction == 0) 16 { 17 if (*endp > zhou) 18 endp --; 19 else{ 20 *startp = *endp; 21 startp ++; 22 direction = 1; 23 } 24 } 25 else 26 { 27 if (*startp < zhou) 28 startp ++; 29 else{ 30 *endp = *startp; 31 endp --; 32 direction = 0; 33 } 34 } 35 } 36 *startp = zhou; 37 quicksort(intarray, 0, startp - intarray - 1); 38 quicksort(endp++, 0, (intarray + endpos) - endp - 1); 39 } 40 }
快速排序 java代码
1 public class QuickSort { 2 3 public static void uQuickSort(int[] intarray, int startpos, int endpos) { 4 int size = endpos - startpos + 1; 5 int pivot = 0; 6 if (size <= 0) 7 return; 8 else { 9 pivot = intarray[startpos]; 10 int front = startpos; 11 int back = endpos; 12 boolean turn = false; 13 while (front != back) { 14 if (!turn) { 15 if (intarray[back] >= pivot) 16 back--; 17 else { 18 intarray[front] = intarray[back]; 19 front++; 20 turn = true; 21 } 22 } else { 23 if (intarray[front] <= pivot) 24 front++; 25 else { 26 intarray[back] = intarray[front]; 27 back--; 28 turn = false; 29 } 30 } 31 } 32 intarray[front] = pivot; 33 uQuickSort(intarray, startpos, front - 1); 34 uQuickSort(intarray, front + 1, endpos); 35 } 36 } 37 }
2.归并排序
也是基于分治的思想,主要思想是把待排序的数组分为两个子数组进行排序,也是用递归实现,递归的终止条件是子数组只有一个元素不能再划分,然后从最小的划分中对两个小数组进行插入排序。这种排序实现时要一个和排序数组等大小的辅助数组的空间。
归并排序C代码
1 #include <string.h> 2 3 extern int* buffer; 4 5 6 void mergreverssort(int* intarray, int startpos, int endpos) 7 { 8 int* startp = intarray + startpos; 9 int* secondp; 10 int* pbuffer; 11 int size = endpos - startpos + 1; 12 int frontsize; 13 int backsize; 14 if (size == 1) 15 return; 16 frontsize = size / 2; 17 backsize = size - frontsize; 18 secondp = startp + frontsize; 19 mergreverssort(intarray, startpos, startpos + frontsize - 1); 20 mergreverssort(intarray, startpos + frontsize, startpos + size - 1); 21 pbuffer = buffer + startpos; 22 while (frontsize != 0 && backsize != 0) 23 { 24 if (*startp < *secondp) 25 { 26 *pbuffer = *startp; 27 startp ++; 28 frontsize --; 29 pbuffer ++; 30 } 31 else if (*startp == *secondp) 32 { 33 *pbuffer = *startp; 34 pbuffer ++; 35 frontsize --; 36 startp ++; 37 *pbuffer = *secondp; 38 pbuffer ++; 39 backsize --; 40 secondp ++; 41 } 42 else{ 43 *pbuffer = *secondp; 44 secondp ++; 45 backsize --; 46 pbuffer ++; 47 } 48 } 49 if (frontsize != 0) 50 { 51 while (frontsize != 0) 52 { 53 *pbuffer = *startp; 54 startp ++; 55 frontsize --; 56 pbuffer ++; 57 } 58 } 59 else if (backsize != 0) 60 { 61 while (backsize != 0) 62 { 63 *pbuffer = *secondp; 64 secondp ++; 65 backsize --; 66 pbuffer ++; 67 } 68 } 69 70 pbuffer = buffer + startpos; 71 72 memcpy(intarray + startpos, pbuffer, sizeof(int) * size); 73 }
归并排序的Java代码
1 public class MergSort { 2 3 public static boolean firstornot = false; 4 5 public static int[] assistArray; 6 7 public static void mergSort(int[] intarray, int startpos, int endpos) { 8 9 if (MergSort.firstornot == false) { 10 11 MergSort.assistArray = new int[intarray.length]; 12 13 MergSort.firstornot = true; 14 15 } 16 17 int size = endpos - startpos + 1; 18 19 if (size <= 1) 20 21 return; 22 23 int leftsize = size / 2; 24 25 mergSort(intarray, startpos, startpos + leftsize - 1); 26 27 mergSort(intarray, startpos + leftsize, endpos); 28 29 int insertpos = startpos; 30 31 int leftpos = startpos; 32 33 int rightpos = startpos + leftsize; 34 35 while (leftpos != startpos + leftsize && rightpos != endpos + 1) { 36 37 if (intarray[leftpos] < intarray[rightpos]) { 38 39 MergSort.assistArray[insertpos] = intarray[leftpos]; 40 41 leftpos++; 42 43 insertpos++; 44 45 } else if (intarray[leftpos] > intarray[rightpos]) { 46 47 MergSort.assistArray[insertpos] = intarray[rightpos]; 48 49 rightpos++; 50 51 insertpos++; 52 53 } else { 54 55 MergSort.assistArray[insertpos] = intarray[rightpos]; 56 57 insertpos++; 58 59 rightpos++; 60 61 MergSort.assistArray[insertpos] = intarray[leftpos]; 62 63 insertpos++; 64 65 leftpos++; 66 67 } 68 69 }// end while insertsort complete 70 71 if (leftpos != startpos + leftsize) { 72 73 while (leftpos != startpos + leftsize) { 74 75 MergSort.assistArray[insertpos] = intarray[leftpos]; 76 77 insertpos++; 78 79 leftpos++; 80 81 } 82 83 } 84 85 if (rightpos != endpos + 1) { 86 87 MergSort.assistArray[insertpos] = intarray[rightpos]; 88 89 rightpos++; 90 91 insertpos++; 92 93 } 94 95 for (int i = startpos; i != endpos + 1; i++) 96 97 intarray[i] = MergSort.assistArray[i]; 98 99 } 100 101 }
3. Shell排序
其思想是改进的插入排序,当一个数组有序时,插入排序用的时间最少,基于此要排序一个数组要使它尽量有序,这样才能得到更高的运行效率。所以可以把待排序数组按步长分为若干组,并对组内的数进行插入排序,之后减小步长,再对每组进行插入排序,一直到最后步长为1,最后一定要为1哦。这里实现选择步长方法只是每次将其减半,更优的维基上有解说。
Shell排序C代码
1 void shellsort(int* intarray, int startpos, int endpos) 2 { 3 int len = endpos - startpos + 1; 4 int steplen; 5 int groupsize; 6 int* startp = intarray; 7 int elemnum; 8 int i, j, temp; 9 steplen = len / 2; 10 groupsize = steplen; 11 elemnum = len / steplen; 12 while (steplen != 0) 13 { 14 while (groupsize != 0) 15 { 16 startp = intarray + (steplen - groupsize) ; 17 for (i = 1; i != elemnum; i++) 18 { 19 temp = *(startp + steplen * i); 20 for (j = i - 1; j != -1; j--) 21 { 22 if (*(startp + steplen * j) > temp) 23 *(startp + steplen * (j + 1)) = *(startp + steplen * j); 24 else break; 25 } 26 *(startp + steplen * (j + 1)) = temp; 27 } 28 29 groupsize --; 30 } 31 steplen = steplen / 2; 32 groupsize = steplen; 33 if (steplen != 0) 34 elemnum = len / steplen; 35 } 36 }
Shell排序Java代码
1 public static void shellSort(int[] intarray, int startpos, int endpos) { 2 int size = endpos - startpos + 1; 3 int steplen = size / 2; 4 int groupnum = 0; 5 int elemnum = 0; 6 groupnum = steplen; 7 int buffer = 0; 8 while(steplen != 0) { 9 for(int i = 0; i != groupnum; i++) { 10 elemnum = size / steplen; 11 for(int j = 1; j != elemnum; j++){ 12 buffer = intarray[i + j * steplen]; 13 int k = j; 14 for(; k != 0; k --){ 15 if(buffer < intarray[i + (k - 1) * steplen]) 16 intarray[i + k * steplen] = intarray[i + (k - 1) * steplen]; 17 else break; 18 } 19 intarray[i + k * steplen] = buffer; 20 } 21 }//end for each group 22 steplen /= 2; 23 if(steplen == 0) 24 break; 25 groupnum = steplen; 26 }//end while distance not zero 27 }

浙公网安备 33010602011771号