请告诉我为什么在交换排序中使用嵌套循环? [英] Please tell me why nested loop is used in exchange sort?

查看:74
本文介绍了请告诉我为什么在交换排序中使用嵌套循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for(i=0;i<5;i++)
  scanf("%d",&x[i]);

for(j=0;j<5;j++)
  for(i=0;i<5;i++)      
    if(x[i] > x[i+1])     
    {
      holder = x[i];
      x[i] = x[i+1];
      x[i+1] = holder;
    }
    
printf("\n\n");
	
for(i=4;i>=0;i--)
  printf("%d \n",x[i]);





我的尝试:



我们在这个程序中使用单维数组,但为什么我们使用嵌套for循环?



What I have tried:

We are using single dimensional array in this program but why we are using nested for loop ?

推荐答案

因为它是一种气泡排序:每次绕过内环都会移动一个尽可能高的物品,就像上升到液体顶部的一系列气泡一样。首先它将最大值移动到最后一个位置,然后将下一个最高点移动到较低位置,依此类推,直到所有项目都正确为止。



但是...... 。它将无法正常工作。

你能明白为什么不行吗?毕竟这是你的作业。
Because it's a bubble sort: each pass round the inner loop moves an item as high as it can go, like a series of bubbles rising to the top of a liquid. First it moves the biggest to the last position, then it moves the next highest to the position one lower, and so on until all items are correct.

But...It won't work correctly.
Can you see why not? This is your homework after all.


匆忙:因为它是这样的。



错误提示:

如果您真的想了解它,可以通过以下方式进行操作:

了解Quicksort(使用交互式演示)★me.dt.in.th [ ^ ]




希望更好(只需谷歌找到):

冒泡排序〜轻松学习 [ ^ ]
In hurry: Because it is like this.

Wrong hint:
In case you really like to understand it, work through someting like this:
Understanding Quicksort (with interactive demo) ★ me.dt.in.th[^]


Hopefully better (simply found by google):
Bubble Sort ~ Easy Learning[^]


使用调试器查看您的算法有效,它也可以帮助您找到代码中的错误。

Use the debugger to see how your algorithm works, it will also help you to find the bug in your code.
引用:

我们正在使用这个程序中的单维数组,但为什么我们使用嵌套for loop?

We are using single dimensional array in this program but why we are using nested for loop ?



使用示例输入为5,4,3,2,1并查看 j ,你会看到你需要2个嵌套循环的原因。


有一个工具可以让你看到你的代码在做什么,它的名字是调试的。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Use a sample input as 5,4,3,2,1 and see what is the array after only 1 loop for j, you will see the reason why you need 2 nested loops.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于请告诉我为什么在交换排序中使用嵌套循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆