当前位置:   article > 正文

四种排序的实现:冒泡、插入、归并、快速_冒泡排序:o{n2} 从后往前,两两比较换位

冒泡排序:o{n2} 从后往前,两两比较换位

冒泡排序:O{n2}  从后往前,两两比较换位

选择排序:O(n2) 选择最小的,与最前面的换位

插入排序:O(n2) 从前往后,两两比较换位

堆排序: O(nlogn) 使用树结构,树顶点最大,取出后重新排列剩余树。

归并排序:O(nlogn) 分两组,在组内继续细分两组,直到一个为一组,然后比较相邻两组,比较换位

快速排序:O(nlogn) 找基准值,分左右两侧,左侧比基准小,右侧比基准大,在两侧内继续执行快速排序。

冒泡排序:

  1. #include <iostream>
  2. using namespace std;
  3. void bubbleSort1(int list[], int length){
  4. if (list == NULL){
  5. cout << "list is null, return;";
  6. return ;
  7. }
  8. cout << "bubble sort, list length="<< length<<endl;
  9. for (int h = 0; h < length; h++){
  10. cout << list[h] << "\t";
  11. }
  12. cout <<endl;
  13. int temp;
  14. bool swapped = true;
  15. int runCount = 0;
  16. for (int j = 0; j < length && swapped; j++){
  17. swapped = false;
  18. for (int i = length - 1; i > 0 && (j == 0 || list[i] > list[j-1]); i--){
  19. cout << runCount++ <<" ";
  20. if (list[i] < list[i-1]){
  21. temp = list[i];
  22. list[i] = list[i-1];
  23. list[i-1] = temp;
  24. swapped =true;
  25. }
  26. }
  27. cout <<endl<< "run "<< j+1 <<"th round: \t";
  28. for (int k = 0; k < length; k++){
  29. cout << "\t" << list[k] ;
  30. }
  31. cout <<endl;
  32. }
  33. cout <<endl;
  34. cout <<endl;
  35. cout <<endl;
  36. }
  37. int bubbleSort2(int* list, int length){
  38. if (list == NULL || length < 2){
  39. cout << " list is NULL, or not need to sort!" <<endl;
  40. return -1;
  41. }
  42. int temp;
  43. int shouldRun = true;
  44. int swapPosition = 0;
  45. int len = length - 1;
  46. int runCount = 0;
  47. for (int i = 0; shouldRun && i < length; i++){
  48. int* tempList = list;
  49. shouldRun = false;
  50. for (int j=0; j < len; j ++){
  51. cout << runCount++ <<" ";
  52. if (*tempList > *(tempList + 1)){
  53. temp = *tempList;
  54. *tempList = *(tempList + 1);
  55. *(tempList + 1) = temp;
  56. shouldRun = true;
  57. swapPosition = j;
  58. }
  59. tempList ++;
  60. }
  61. len = swapPosition;
  62. cout <<endl<< "run "<< i+1 <<"th round: \t";
  63. for (int k = 0; k < length; k++){
  64. cout << "\t" << *(list+k) ;
  65. }
  66. cout <<endl;
  67. }
  68. return 0;
  69. }
  70. int main(){
  71. // int list[10] = {0,9,8,7,6,5,4,3,2,1};
  72. int list[10] = {9,8,7,6,5,4,3,2,1, 0};
  73. // int list[10] = NULL;
  74. /*
  75. cout << "please input 10 number to sort!" << endl;
  76. for(int j = 0; j<10;j++){
  77. cin >> list[j] ;
  78. }
  79. */
  80. bubbleSort1(list, sizeof(list)/sizeof(list[0]));
  81. // int list1[10] = {9,8,7,6,5,4,3,2,1,0};
  82. // int list1[10] = {0,0,0,0,0,0,0,0,0,0};
  83. int list1[10] = {0,0,0,0,0,1,0,0,0,0};
  84. bubbleSort2(list1, sizeof(list1)/sizeof(list1[0]));
  85. cout <<" Sorted number list is ";
  86. for (int i = 0; i < 10; i++){
  87. cout << list[i] << "\t";
  88. }
  89. cout <<endl;
  90. return 0;
  91. }

插入排序:

  1. #include <iostream>
  2. using namespace std;
  3. int insertSort(int array[], int length);
  4. void print(int array[], int length);
  5. int swapValue(int& a, int& b, int& temp);
  6. int main(){
  7. int array[] = {9,8,7,6,5,4,3,2,1,0};
  8. int length = sizeof(array)/sizeof(array[0]);
  9. insertSort(array, length);
  10. print(array, length);
  11. return 0;
  12. }
  13. int insertSort(int array[], int length){
  14. if (array == NULL || length < 2){
  15. return -1;
  16. }
  17. int temp;
  18. for (int i = 0; i < length; i++){
  19. for (int j = i; j > 0 && array[j] < array[j-1]; j--){
  20. swapValue(array[j], array[j-1], temp);
  21. }
  22. }
  23. return 0;
  24. }
  25. int swapValue(int& a, int& b, int& temp){
  26. temp = a;
  27. a = b;
  28. b = temp;
  29. return 0;
  30. }
  31. void print(int array[], int length){
  32. cout<<endl<< "print array, length="<<length<<endl;
  33. for(int i = 0; i< length;i++){
  34. cout<<"\t"<<array[i];
  35. }
  36. cout<<endl;
  37. }

归并排序:

  1. #include <iostream>
  2. using namespace std;
  3. int mergeSort(int array[], int length);
  4. void print(int array[], int length);
  5. int swapValue(int& a, int& b, int& temp);
  6. int doMerge(int* a, int leftLength, int* b, int rightLength);
  7. int count = 1;
  8. int main(){
  9. int array[] = {9,8,7,6,5,4,3,2,1,0};
  10. // int array[] = {9,8};
  11. int length = sizeof(array)/sizeof(array[0]);
  12. mergeSort(array, length);
  13. print(array, length);
  14. return 0;
  15. }
  16. int mergeSort(int array[], int length){
  17. if (array == NULL || length <2){
  18. return -1;
  19. }
  20. int subLength = length / 2;
  21. doMerge(array, subLength, array + subLength, length - subLength);
  22. return 0;
  23. }
  24. int doMerge(int* a, int leftLength, int* b, int rightLength){
  25. if (a == NULL || b == NULL){
  26. cout<<" array is null, please check!"<<endl;
  27. return -1;
  28. }
  29. int temp;
  30. // cout << "leftL="<<leftLength << " rightL="<<rightLength << endl;
  31. if (leftLength == 1 && rightLength == 1){
  32. cout <<endl<<count++<< " swap:"<<*a<<"--"<<*b<<endl;
  33. swapValue(*a, *b, temp);
  34. return true;
  35. }
  36. if (leftLength > 1){
  37. int subLLength = leftLength / 2;
  38. doMerge(a, subLLength, a + subLLength, leftLength - subLLength);
  39. }
  40. if (rightLength > 1){
  41. int subRLength = rightLength / 2;
  42. doMerge(b, subRLength, a + subRLength, rightLength - subRLength);
  43. }
  44. for (int i = 0; i < rightLength; i++){
  45. for (int j = leftLength - 1; j >= 0; j--){
  46. if (*(a+i) > *(b+j)){
  47. cout <<endl<<count++<< " swap:"<<*(a+i)<<"--"<<*(b+j)<<endl;
  48. swapValue(*(a+i), *(b+j), temp);
  49. }
  50. }
  51. }
  52. return 0;
  53. }
  54. void print(int array[], int length){
  55. cout<<endl<< "print array, length="<<length<<endl;
  56. for(int i = 0; i< length;i++){
  57. cout<<"\t"<<array[i];
  58. }
  59. cout<<endl;
  60. }
  61. int swapValue(int& a, int& b, int& temp){
  62. temp = a;
  63. a = b;
  64. b = temp;
  65. return 0;
  66. }

快速排序:

  1. #include <iostream>
  2. using namespace std;
  3. void print(int list[], int length);
  4. void print(int i, int j, int& runCount, int list[], int length);
  5. int swapValue(int& a, int& b);
  6. int quickSort2(int* list, int length);
  7. int quickSort3(int list[], int low, int high);
  8. int runQuickSort(int list[], int low, int high);
  9. int runQuickSort2(int list[], int low, int high);
  10. int main(){
  11. // int list1[] = {9,2,5,1,8,7,0,6,3,4};
  12. int list1[] = {0,1,2,3,4,5,6,7,8,9};
  13. // int list1[] = {9,8,7,6,5,4,3,2,1,0};
  14. // int list1[] = {3,2,0,1,4};
  15. int length = sizeof(list1)/sizeof(list1[0]);
  16. // quickSort2(list1, length);
  17. quickSort3(list1, 0, length - 1);
  18. cout <<endl<<"quick sort list: ";
  19. for (int h = 0; h< length; h++){
  20. cout << "\t"<<list1[h];
  21. }
  22. cout <<endl;
  23. return 0;
  24. };
  25. int quickSort2(int* list, int length){
  26. if (list == NULL || length < 2){
  27. return -1;
  28. }
  29. int temp;
  30. int* runList = list;
  31. int compare = *(runList + length - 1);
  32. int last = length - 2;
  33. int changePosition = -1;
  34. // cout <<endl<<"quick sort2: "<< length << " compare:"<< compare<<endl;
  35. // print(list, length);
  36. for (int i = 0; i <= last; i ++){
  37. if (*(runList+i) > compare){
  38. for (; last > i; last --){
  39. if(*(runList+last) < compare){
  40. swapValue(*(runList + i), * (runList + last));
  41. break;
  42. }
  43. }
  44. }
  45. changePosition = i;
  46. }
  47. // cout <<endl<<"quick sort2: "<< length << " changePosition:"<< changePosition <<endl;
  48. if (changePosition > -1 && changePosition < length){
  49. swapValue(*(list+changePosition), *(list+ + length - 1));
  50. }
  51. if(changePosition > 1 && changePosition < length){
  52. cout << "left side: "<<endl;
  53. print(list, changePosition);
  54. quickSort2(list, changePosition);
  55. }
  56. if (changePosition > -1 && changePosition < length - 2){
  57. cout << "right side: "<<endl;
  58. print(list + changePosition + 1, length - changePosition - 1);
  59. quickSort2(list + changePosition + 1, length - changePosition - 1);
  60. }
  61. return 0;
  62. }
  63. int quickSort3(int list[], int low, int high){
  64. if(low < high){
  65. // int comparePosition = runQuickSort(list, low, high);
  66. int comparePosition = runQuickSort2(list, low, high);
  67. cout<<endl<<"run begin:::";
  68. for(int h = low; h <high+1; h++){
  69. cout <<"\t"<<list[h];
  70. }
  71. // if (comparePosition > low + 1 && comparePosition < high){
  72. cout<<endl<<"left left ccc="<<comparePosition<<endl;
  73. quickSort3(list, low, comparePosition -1);
  74. // }
  75. // if (comparePosition > low && comparePosition < high - 1){
  76. cout<<endl<<"right right cccc==="<<comparePosition<<endl;
  77. quickSort3(list, comparePosition + 1, high);
  78. // }
  79. }
  80. return 0;
  81. }
  82. int runQuickSort(int list[], int low, int high){
  83. if (list != NULL && high <= low){
  84. return low;
  85. }
  86. cout <<endl<<"run quick sort begion " <<endl;
  87. for(int h = low; h <high+1; h++){
  88. cout <<"\t"<<list[h];
  89. }
  90. int i = low;
  91. int j = high - 1;
  92. int compareValue = list[high];
  93. while(i <= j){
  94. if (list[i] > compareValue){
  95. while(j > i){
  96. if (list[j] < compareValue){
  97. swapValue(list[i], list[j]);
  98. break;
  99. }
  100. j--;
  101. }
  102. }
  103. cout << endl << "i="<<i<<" j="<<j<<endl;
  104. if (i == j){
  105. swapValue(list[i],list[high]);
  106. cout <<endl<<"swap comapre is : "<<"i = "<<i <<"; value="<<list[i] << ", high value:"<< list[high] <<endl;
  107. break;
  108. }
  109. i++;
  110. }
  111. cout <<endl<<"run quick sort end,: "<<compareValue <<endl;
  112. for(int h = low; h <high+1; h++){
  113. cout <<"\t"<<list[h];
  114. }
  115. return i;
  116. }
  117. int runQuickSort2(int list[], int low, int high){
  118. int key = list[low];
  119. cout <<"run sort begin: low ="<<low<<" high="<<high<<" key="<<key<<endl;
  120. while(low < high){
  121. while (low < high && list[high] >= key){
  122. high --;
  123. }
  124. if (low < high){
  125. list[low] = list[high];
  126. }
  127. while(low < high && list[low] <= key){
  128. low++;
  129. }
  130. if (low < high){
  131. list[high] = list[low];
  132. }
  133. }
  134. list[low] = key;
  135. return low;
  136. }
  137. int swapValue(int& a, int& b){
  138. int temp = a;
  139. a = b;
  140. b = temp;
  141. return 0;
  142. }
  143. void print(int list[], int length){
  144. for (int h = 0; h< length; h++){
  145. cout << "\t"<<list[h];
  146. }
  147. cout <<endl;
  148. }
  149. void print(int i, int j, int& runCount, int list[], int length){
  150. cout <<"run count: "<< runCount++ <<"\t" << i<<" "<<j;
  151. cout <<endl;
  152. cout <<endl;
  153. for (int h = 0; h< length; h++){
  154. cout << "\t"<<list[h];
  155. }
  156. cout <<endl;
  157. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/475718
推荐阅读
相关标签
  

闽ICP备14008679号