Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
921 views
in Technique[技术] by (71.8m points)

arrays - BubbleSorting C language

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]);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...