BubbleSorting C language

拜拜、爱过 提交于 2020-01-03 05:23:11

问题


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.

My Code

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:


Your Code Has Multiple Problems ,

  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
    
    } 
    
  2. 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])
    
  3. 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.

This is What Wikipedia Says

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.

Step-by-step example

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.

First Pass

( 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.

Second Pass

( 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.

Third Pass

( 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 )

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.




回答2:


When i == 4 you access the position 5 with value[i+1] and it is not yours.

You are accessing unreserved memory.



来源:https://stackoverflow.com/questions/42043489/bubblesorting-c-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!