c++ 如何使数组输出一步一步

i34xakig  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(177)

我这样做,但有些东西显示已经稍微重新排列的阵列。我不明白为什么。或者应该是这样?它仍然有可能实现这个想法?
快速排序的本质是将数组中的元素随机排列,使其分为两部分。第一部分包含小于或等于'p'的元素,第二部分包含大于或等于'p'的元素。然后我们分别对每一部分进行排序。
一般来说,选择“p”使得数组被分成两半是非常可取的。请注意,“p”可以是任何元素(不一定来自数组的中间),甚至不一定来自这个数组。

  1. #include<iostream>
  2. using namespace std;
  3. void funcprint( int arr[], int size )
  4. {
  5. for( int i = 0; i < size; i++ )
  6. cout << arr[i] << " ";
  7. cout << endl;
  8. }
  9. int *ar;
  10. void quickSort( int a[], long N ) {
  11. if( N < 2 )
  12. return;
  13. long i = 0, j = N - 1;
  14. int p = a[N >> 1];
  15. while( i < j ) {
  16. while( a[i] < p ) i++;
  17. while( a[j] > p ) j--;
  18. if( i < j )
  19. swap( a[i++], a[j--] );
  20. }
  21. funcprint( ar, 9 );
  22. quickSort( a, j );
  23. quickSort( a + i, N - i );
  24. }
  25. int main() {
  26. int a[10] = { 1,2,3,4,1,2,6,7,8 };
  27. //int a[10] = { 5,2,1,9,1,4,6,2,0 };
  28. ar = a;
  29. int N = 8;
  30. quickSort( a, N );
  31. funcprint( a, N+1 );
  32. }

字符串

tp5buhyn

tp5buhyn1#

要可视化代码的工作,您可以突出显示在每次迭代中排序的子数组。

  1. #include<iostream>
  2. using namespace std;
  3. void funcprint(int arr[], int size, int fragment_start, int fragment_end) {
  4. for(int i = 0; i < size; i++) {
  5. if (i == fragment_start)
  6. cout << "[ ";
  7. if (i == fragment_end)
  8. cout << "] ";
  9. cout << arr[i] << " ";
  10. }
  11. if (fragment_end == size)
  12. cout << "] ";
  13. cout << endl;
  14. }
  15. int *ar;
  16. void quickSort(int a[], long N) {
  17. if( N < 2 )
  18. return;
  19. long i = 0, j = N - 1;
  20. int p = a[N >> 1];
  21. while(i < j) {
  22. while(a[i] < p)
  23. i++;
  24. while(a[j] > p)
  25. j--;
  26. if(i < j)
  27. swap(a[i++], a[j--]);
  28. }
  29. funcprint(ar, 9, (a-ar), (a-ar) + N + 1);
  30. quickSort(a, j);
  31. quickSort(a + i, N - i);
  32. }
  33. int main() {
  34. int a[10] = { 1,2,3,4,1,2,6,7,8 };
  35. ar = a;
  36. int N = 8;
  37. cout << "Initial" << endl;
  38. funcprint(ar, N+1, 0, N+1);
  39. cout << endl;
  40. quickSort(a, N);
  41. cout << endl;
  42. cout << "After sorting" << endl;
  43. funcprint(a, N+1, 0, N+1);
  44. }

字符串
这段代码的输出:

  1. Initial
  2. [ 1 2 3 4 1 2 6 7 8 ]
  3. [ 1 2 3 4 1 2 6 7 8 ]
  4. 1 [ 1 3 4 2 2 6 7 8 ]
  5. 1 1 [ 2 2 4 3 6 7 8 ]
  6. 1 1 2 2 [ 4 3 6 7 8 ]
  7. 1 1 2 2 [ 3 4 6 ] 7 8
  8. 1 1 2 2 3 4 [ 6 7 8 ]
  9. After sorting
  10. [ 1 1 2 2 3 4 6 7 8 ]


对于int a[10] = { 5,2,1,9,1,4,6,2,0 };

  1. Initial
  2. [ 5 2 1 9 1 4 6 2 0 ]
  3. [ 1 1 2 9 5 4 6 2 0 ]
  4. 1 1 [ 2 2 4 5 6 9 0 ]
  5. 1 1 [ 2 2 4 ] 5 6 9 0
  6. 1 1 2 2 4 [ 5 6 9 0 ]
  7. 1 1 2 2 4 5 [ 6 9 0 ]
  8. After sorting
  9. [ 1 1 2 2 4 5 6 9 0 ]


如果你想看到每个交换,你可以使用这种方法:

  1. #include<iostream>
  2. using namespace std;
  3. void funcprint(int arr[], int size, int fragment_start, int fragment_end) {
  4. for(int i = 0; i < size; i++) {
  5. if (i == fragment_start)
  6. cout << "[ ";
  7. if (i == fragment_end)
  8. cout << "] ";
  9. cout << arr[i] << " ";
  10. }
  11. if (fragment_end == size)
  12. cout << "] ";
  13. cout << endl;
  14. }
  15. void printswap(int arr[], int size, int first, int second) {
  16. for(int i = 0; i < size; i++) {
  17. if (i == first || i == second)
  18. cout << "<" << arr[i] << "> ";
  19. else
  20. cout << arr[i] << " ";
  21. }
  22. cout << endl;
  23. }
  24. int *ar;
  25. void quickSort(int a[], long N) {
  26. if( N < 2 )
  27. return;
  28. long i = 0, j = N - 1;
  29. int p = a[N >> 1];
  30. int iteration = 1;
  31. while(i < j) {
  32. while(a[i] < p)
  33. i++;
  34. while(a[j] > p)
  35. j--;
  36. if(i < j) {
  37. swap(a[i++], a[j--]);
  38. cout << "swap at " << iteration << " iteration:" << endl;
  39. printswap(ar, 9, (a-ar) + i-1, (a-ar) + j+1);
  40. }
  41. ++iteration;
  42. }
  43. funcprint(ar, 9, (a-ar), (a-ar) + N + 1);
  44. quickSort(a, j);
  45. quickSort(a + i, N - i);
  46. }
  47. int main() {
  48. int a[10] = { 1,2,3,4,1,2,6,7,8 };
  49. ar = a;
  50. int N = 8;
  51. cout << "Initial" << endl;
  52. funcprint(ar, N+1, 0, N+1);
  53. cout << endl;
  54. quickSort(a, N);
  55. cout << endl;
  56. cout << "After sorting" << endl;
  57. funcprint(a, N+1, 0, N+1);
  58. }


输出量:

  1. [ 1 2 3 4 1 2 6 7 8 ]
  2. swap at 1 iteration:
  3. <1> 2 3 4 <1> 2 6 7 8
  4. [ 1 2 3 4 1 2 6 7 8 ]
  5. swap at 1 iteration:
  6. 1 <1> 3 4 <2> 2 6 7 8
  7. 1 [ 1 3 4 2 2 6 7 8 ]
  8. swap at 1 iteration:
  9. 1 1 <2> 4 2 <3> 6 7 8
  10. swap at 2 iteration:
  11. 1 1 2 <2> <4> 3 6 7 8
  12. 1 1 [ 2 2 4 3 6 7 8 ]
  13. 1 1 2 2 [ 4 3 6 7 8 ]
  14. swap at 1 iteration:
  15. 1 1 2 2 <3> <4> 6 7 8
  16. 1 1 2 2 [ 3 4 6 ] 7 8
  17. 1 1 2 2 3 4 [ 6 7 8 ]
  18. After sorting
  19. [ 1 1 2 2 3 4 6 7 8 ]


对于int a[10] = { 5,2,1,9,1,4,6,2,0 };

  1. Initial
  2. [ 5 2 1 9 1 4 6 2 0 ]
  3. swap at 1 iteration:
  4. <1> 2 1 9 <5> 4 6 2 0
  5. swap at 2 iteration:
  6. 1 <1> <2> 9 5 4 6 2 0
  7. [ 1 1 2 9 5 4 6 2 0 ]
  8. swap at 1 iteration:
  9. 1 1 2 <2> 5 4 6 <9> 0
  10. swap at 2 iteration:
  11. 1 1 2 2 <4> <5> 6 9 0
  12. 1 1 [ 2 2 4 5 6 9 0 ]
  13. swap at 1 iteration:
  14. 1 1 <2> <2> 4 5 6 9 0
  15. 1 1 [ 2 2 4 ] 5 6 9 0
  16. 1 1 2 2 4 [ 5 6 9 0 ]
  17. 1 1 2 2 4 5 [ 6 9 0 ]
  18. After sorting
  19. [ 1 1 2 2 4 5 6 9 0 ]


这段代码有点乱,只是为了说明一下。

展开查看全部

相关问题