BubbleSorting C语言 [英] BubbleSorting C language

查看:119
本文介绍了BubbleSorting C语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在学习数组,而我只是在进行气泡排序.
我编写了以下代码,以升序对数组进行排序,但是存在问题.
我找不到它,但我知道有问题.
我找到了正确的代码,但是我仍然不明白为什么这不起作用.

We are learning arrays and I just came around bubble sorting.
I wrote the following code to sort the array in ascending order but there is a problem.
I can't find it out but I know there's a problem.
I have found the correct code but I still don't understand why this is not working.

我的代码

int a;
int i;

int temp;
int value[5] = { 5, 4, 3, 2, 1 };

for (i = 0; i < 5; i++) {
    if (value[i] > value[i + 1]) {
        temp = value[i];
        value[i] = value[i + 1];
        value[i + 1] = temp;
    }
}

for (a = 0; a < 5; a++) {
    printf(" %d ", value[i]);
}

推荐答案

您的代码有多个问题,

  1. 首先要进行的操作与value[i]相同的排序之后,您要打印数组元素,并且正在使用变量a循环运行.

  1. First And Foremost You are printing the array elements after that sorting that you did with value[i] and you are running loop with variable a.

for(a=0;a<5;a++){               //You Are Incrementing a

printf(" %d ",value[i]);        //But Using i here , change it to a.
                                //As It will just print the value[i] member

} 

  • 您正在访问value[5],这不是您的.

  • You are Accessing value[5] , which is not yours.

     for(i=0;i<5;i++){ //Here In The Last Loop value[4] is  compared with value[5],
                       // value[5] is not defined 
    
    //for(i=0;i<4;i++)       
    //Instead Run Loop Till i<4 , I guess this is what you 
    //wanted but accidently made a mistake.
    
       if(value[i]>value[i+1])
    

  • 最大的问题是,您尚未完全了解Bubblesort. 在冒泡排序中,循环将运行多次,直到在循环中没有要交换的成员(即停止循环时)为止.

  • The Biggest Problem, is that you have not understood Bubblesort completely. In Bubble Sort The Loop is run multiple times, until in a loop there are no member to swap, that is when you stop looping.

    这就是维基百科

    气泡排序,有时也称为下沉排序,是一种简单的排序算法,它反复遍历要排序的列表,比较每对相邻项目并交换如果它们的顺序错误.重复遍历该列表,直到不需要交换为止,这表明该列表已排序.该算法是一种比较排序,以较小或较大的元素冒泡"到列表顶部的方式命名.尽管该算法很简单,但与插入排序相比,它对于大多数问题而言仍然太慢且不切实际.如果输入通常是按排序顺序进行的,但偶尔可能有一些乱序的元素几乎在适当的位置,则该方法是可行的./p>

    Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.It can be practical if the input is usually in sorted order but may occasionally have some out-of-order elements nearly in position.

    请参见下面的演示以了解气泡排序的工作原理.请参见循环遍历",直到没有成员可以交换为止.

    See The Below Presentation To Understand How Bubble Sort Works.See the Loop Run again and again, till there are no member left to swap.

    分步示例

    让我们取数字数组"5 1 4 2 8",并使用冒泡排序将数组从最小数到最大数进行排序.在每个步骤中,将比较以粗体编写的元素.需要三张通行证.

    Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required.

    首次通过

    ( 5 1 4 2 8)到( 1 5 4 2 8),此处算法比较前2个元素,并从5> 1开始交换. > (1 5 4 2 8)到(1 4 5 2 8),从5> 4
    交换 (1 4 5 2 8)到(1 4 2 5 8),从5> 2
    交换 (1 4 2 5 8 )到(1 4 2 5 8 ),现在,由于这些元素已经按顺序排列(8> 5),因此算法不会交换它们.

    ( 5 1 4 2 8 ) to ( 1 5 4 2 8 ), Here algorithm compares the first 2 elements,and swap since 5 > 1.
    ( 1 5 4 2 8 ) to ( 1 4 5 2 8 ), Swap since 5 > 4
    ( 1 4 5 2 8 ) to ( 1 4 2 5 8 ), Swap since 5 > 2
    ( 1 4 2 5 8 ) to ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.

    第二张通行证

    ( 1 4 2 5 8)到( 1 4 2 5 8)
    (1 4 2 5 8)到(1 2 4 5 8),从4> 2
    交换 (1 2 4 5 8)到(1 2 4 5 8)
    (1 2 4 5 8 )到(1 2 4 5 8 )

    ( 1 4 2 5 8 ) to ( 1 4 2 5 8 )
    ( 1 4 2 5 8 ) to ( 1 2 4 5 8 ), Swap since 4 > 2
    ( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
    ( 1 2 4 5 8 ) to ( 1 2 4 5 8 )

    现在,数组已经排序,但是算法不知道它是否完成.该算法需要一整遍而无需任何交换就可以知道它已被排序.

    Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.

    第三张通行证

    ( 1 2 4 5 8)到( 1 2 4 5 8)
    (1 2 4 5 8)到(1 2 4 5 8)
    (1 2 4 5 8)到(1 2 4 5 8)
    (1 2 4 5 8 )到(1 2 4 5 8 )

    ( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
    ( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
    ( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
    ( 1 2 4 5 8 ) to ( 1 2 4 5 8 )

    因此,您需要多次运行循环,直到没有成员要交换为止,您可以通过添加一个新变量count来进行操作,该变量最初在开始时初始化为1,并且在每个循环开始之前初始化为0,如果在循环中执行Swap语句,则更改为1,然后再次执行循环,因为count为1,如果在最后一次传递中,其值未更改为1因为现在所有成员都已排序,所以循环不会再次运行.

    So You Need To Run The Loop Multiple Times Until No Member Are There To Swap, You Can Do So By Having a new variable count, initially initialised to 1 at the start, and before each loop starts, it get initialised to 0, if in the loop, Swap statement executes then, it changes to 1, and again the loop is executed because count is 1, if in the last pass, its value does not changes to 1 because all member are now sorted, so loop does not run again.

    这篇关于BubbleSorting C语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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