什么外循环和内循环代表冒泡排序 [英] What outer loop and inner loop represent in bubble sort

查看:279
本文介绍了什么外循环和内循环代表冒泡排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在Visual Studio 2015 c#控制台应用程序中工作。



我尝试对未排序的数组进行冒泡排序但我不能这样做



i关于我理解的想法互联网搜索。



它是在小型和最大型之间进行比较然后按顺序交换项目数组



但我无法理解这段代码。



以便外循环代表什么内圈表示?



外圈和内圈的粗线



Hi i work in visual studio 2015 c# console applications .

I try to make bubble sort to array unsorted but i cannot do that

i search for internet about that i understand idea .

it is make comparison between small and largest then swap items to make in order array

but i cannot understand this code .

so that what outer loop represent and inner loop represent ?

bold lines for outer loop and inner loop

/* 
 * C# Program to Perform Bubble Sort 
 */  
using System;  
class bubblesort  
{  
        static void Main(string[] args)  
        {  
            int[] a = { 30, 20, 50, 40, 10 };    
            int t;  
            Console.WriteLine("The Array is : ");  
            for (int i = 0; i < a.Length; i++)  
            {  
                Console.WriteLine(a[i]);  
            }  
            for (int j = 0; j <= a.Length - 2; j++) outer loop  
            {  
                for (int i = 0; i <= a.Length - 2; i++) inner loop  
                {  
                    if (a[i] > a[i + 1])  
                    {  
                        t = a[i + 1];  
                        a[i + 1] = a[i];  
                        a[i] = t;  
                    }  
                }  
            }  
            Console.WriteLine("The Sorted Array :");  
            foreach (int aray in a)                           
                Console.Write(aray + " ");  
            Console.ReadLine();  
        }  
    }





我的尝试:



i需要更多解释这段代码



What I have tried:

i need more explain to this code

推荐答案

嗨艾哈迈德,



我不能那样做是什么意思?这是因为代码很好,只是你缺少评论指标(//)。

Hi Ahmed,

What do you mean by "i cannot do that"? This is because the code is fine, only that you are missing the comments indicator (//).
for (int j = 0; j <= a.Length - 2; j++) //outer loop
{
    for (int i = 0; i <= a.Length - 2; i++) //inner loop
    ...........................
    ...........................

该算法检查每两个连续的数字,并在必要时交换它们(用于排序)。在完成一次扫描(并根据需要交换数字)后,它会再次开始扫描整个列表(从头开始)并根据需要交换数字。第一个循环(外部)确保它遍历整个数组n次(n =数组中的元素数)。第二个循环(内部)确保它在每次遍历中交换数字。



泡泡排序有几种变体。这一个的复杂性是O(n平方),因为它总共遍历'n x n'次。因此,在算法复杂性方面,这个算法并不是很好。



理解排序的最佳方法是在调试模式下在Visual Studio IDE中运行应用程序。



1)在for循环上放置一个断点并启动应用程序(F5)。

2)然后打开 autos 窗口(点击 menu-> Debug-> Windows-> Autos

3 )同时打开本地人窗口(点击 menu-> Debug-> Windows-> Autos

4)这两个窗口将使您能够在逐行调试时查看值。

5)现在按F10键,代码将逐行执行-线。您可以在 autos locals 窗口中看到RED变量的变化。数组'a'应该在 locals 窗口内。



请谷歌更多关于不同的文章冒泡排序的变体。

The algorithm checks for every two consecutive numbers and swaps them if necessary (for sorting). After one complete sweep (and swapping of numbers as needed), it starts sweeping again the whole list (starting from the beginning) and swaps numbers as necessary. The first loop (outer) makes sure it traverses the entire array n times (n = number of elements in the array). The second loop (inner) makes sure it swaps numbers in each traversal.

There are several variants of bubble sort. Complexity of this one is O(n-squared) as it traverses 'n x n' times in total. So in terms of algorithmic complexity this one is not a very good one.

The best way to understand the sorting is running the application in Visual Studio IDE in debug mode.

1) Place a breakpoint on the for loop and start the application (F5).
2) Then open the autos window (click on menu->Debug->Windows->Autos)
3) Also open the locals window (click on menu->Debug->Windows->Autos)
4) These two windows will enable you to view the values as you debug line-by-line.
5) Now press F10 and the code will be executed line-by-line. You can see changes in variables in RED in the autos and locals windows. The array 'a' should be inside locals window.

Please Google more for a lot of articles on different variants of bubble sort.


使用冒泡排序,要对10个值进行排序,您需要对9个值进行排序。并找到你需要将其与其他值进行比较所需的下一个最大值。

研究算法,调试器将向您展示程序的工作原理。

冒泡排序 - 维基百科 [ ^ ]

请注意,程序设计不合理,工作量可以除以2.



您应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码正在执行并确保它能达到预期的效果。



Debugger - 维基百科,免费的百科全书 [ ^ ]

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



调试器在这里向您展示您的代码正在做什么以及您的任务是与它应该做的比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
With Bubble sort, to sort 10 values, you need to sort 9 times 1 value. And to find the next biggest value you need to compare it against the other values.
Study the algorithm, the debugger will show you how the program works.
Bubble sort - Wikipedia[^]
Note that the program is poorly designed, the workload can be divided by 2.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

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天全站免登陆