Loop breaking in quick-sort algorithm
本问题已经有最佳答案,请猛点这里访问。
用错误结果实现快速排序算法。我想做什么的详细信息可以在视频中找到:https://courses.edx.org/courses/courses-v1:harvardx+cs50+x/courseware/cdf0594e6a80402bbe902bb107fd2976/22251a2b00ac42788c70ca6f6cbcb7fd/break命令是将我从内部循环还是两个循环中取出?下面是代码,欢迎任何帮助。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | int main (void) { // Array of integers to be sorted int list[] = { 3, 9, 8, 5, 7, 6, 4, 2, 1}; int arrayIndexLimit = (sizeof (list) / sizeof (int)) - 1; int temp = 0; //"J" separates sorted part of array from unsorted one for (int j = 0; j < arrayIndexLimit; j++) { // Find smallest value in unsorted part of array for (int i = j; i <= arrayIndexLimit; i++) { // Put smallest value in jth position if (list[i] < list[arrayIndexLimit]) { temp = list[j]; list[j] = list[i]; list[i] = temp; break; } // Pivot (last item of array) is the smallest if (i == arrayIndexLimit) { temp = list[i]; list[i] = list[j]; list[j] = temp; break; } } // Print sorted list } |
来源