Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.12/5 (5 votes)
See more:
How can bubble sort algorithm be implemented in C?

What I have tried:

I just can't figure it out. Can someone please provide me with the code?
Posted
Updated 14-Jun-22 6:33am
v2

We are willing to help solve a problem you might be having in trying to learn how to program but that's the object of giving homework to learn.
 
Share this answer
 
Comments
CPallini 14-Jun-22 6:58am    
5.
The algorithm is quite a simple one.
You iterate all the entries in your data one by one. Check each pair to see which is smaller (or larger) and swap them into the correct order. Keep a note of any changes. Repeat this process until your changes flag indicates that you did not make any changes in the last pass. You now have your data sorted into order.
 
Share this answer
 
Comments
CPallini 14-Jun-22 6:58am    
5.
Do you know how a bubble sort[^] works? That's the first step.

This is not a "please do my assignment for me" site. But if you search on "bubble sort", you can easily find an implementation in C. However, your instructor probably knows about the site I'm referring to, so if you just copy their code, you're likely to find yourself in trouble.
 
Share this answer
 
Comments
CPallini 14-Jun-22 6:58am    
5.
[no name] 14-Jun-22 19:23pm    
Test (notification).
[no name] 14-Jun-22 19:27pm    
Test 2
Greg Utas 14-Jun-22 19:48pm    
Each of your comments resulted in me getting 2 emails and 2 little red box notifications.
[no name] 14-Jun-22 20:10pm    
You should probably let Chris know. He specifically asked about this in the Bugs forum.
Quote:
I just can't figure it out. Can someone please provide me with the code?

Too bad Google don't work in your place !
You are supposed to do minimum search before asking a question here.
A little search on Google gives85 millions answers, lets guess there is thousands of answer that will fulfill your wish?
 
Share this answer
 
Comments
CPallini 14-Jun-22 6:58am    
5.
Patrice T 14-Jun-22 6:59am    
Thank you.
Much improved versions in pseudocode can be found on Wikipedia: https://en.wikipedia.org/wiki/Bubble_sort
procedure bubbleSort(A : list of sortable items)
    n := length(A)
    repeat
        newn := 0
        for i := 1 to n - 1 inclusive do
            if A[i - 1] > A[i] then
                swap(A[i - 1], A[i])
                newn := i
            end if
        end for
        n := newn
    until n = 1
end procedure
 
Share this answer
 
Bubble sort is a sorting algorithm that compares two nearby elements and swaps them until they are out of order. I have included comments for a better understanding
C
// C program for implementation of Bubble sort
#include<stdio.h>

int main(){

   int  number[30], elementNumber, swap, i, d;

   printf("Number of elements: ");
   scanf("%d",&elementNumber);

   printf("Enter %d integer numbers:\n", elementNumber);
   for(i=0; i < elementNumber; i++)
   scanf("%d",&number[i]);

   // function to implement bubble sort  
    
   for (i = 0 ; i < elementNumber - 1 ; i++){
       
        for(d = 0; d < elementNumber - i - 1 ; d++){
            
            if(number[d] > number[d+1]){ 
            // Use '<' instead of '>' for decreasing order 
                
                swap = number[d];
                number[d] = number[d+1];
                number[d+1] = swap;
        }
      }
   }


    //Prints out sorted elements
    printf("Sorted elements:\n");
    for(i = 0; i < elementNumber; i++)
    printf(" %d\n", number[i]);

   return 0;
}
 
Share this answer
 
Comments
CPallini 14-Jun-22 6:58am    
You should validate the elementNumber value.
BTW have my 5.
Richard MacCutchan 14-Jun-22 6:58am    
You do not help people by doing their work for them.
Darshendran 14-Jun-22 7:40am    
It is easier to understand programming by understanding the answer don't you think?
jeron1 14-Jun-22 10:27am    
The fact that there was no attempt (effort?) shown makes me think that there will also be no attempt to understand the answer. Maybe I'm wrong...
Richard MacCutchan 14-Jun-22 10:55am    
The point of the assignment is to make the pupil think, and use the lessons they have been studying. Giving them the complete code just encourages laziness.
Have you tried anything from your end? If you have stuck anywhere we could have guide you.

I am just trying to help you with your question -

Short Definition: Bubble sort in C is one of the easiest and basic sorting technique that is very easy to implement. In Bubble sorting, we compare two adjacent elements of an array to find which one is greater or lesser and swap them based on the given condition until the final place of the element is not found.

Code Example using For Loop -

C
#include <stdio.h>

int main(){
    int n = 5;
    int arr[5] = {44,33,11,22,55}; //array of size 5
    
    for(int i=0;i<n-1;i++){
        for(int j=0;j<n-i-1;j++){
            if(arr[j]>arr[j+1]){ 
     //checking and swapping adjacent elements when left_elem > right_elem
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
    printf("Array after implementing Bubble sort: ");
    for(int i=0;i<n;i++){
        printf("%d  ",arr[i]);
    }
    return 0;
}


If you want to learn more or understand the topic in-depth read these resources once:

https://www.scaler.com/topics/c-bubble-sort/

https://en.wikipedia.org/wiki/Bubble_sort
 
Share this answer
 
v2
Comments
Richard MacCutchan 14-Jun-22 11:19am    
Please read the responses to Solution 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900