Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Bubble sort in C

In this post, let’s see how to implement Bubble Sort in C.

Bubble Sort, also known as sinking sort,compares adjacent elements and swap them if they are not in correct order.
Here is a simple illustration of bubble sort.
Bubble sort
Above GIF is generated from algorithms app.

#include
 
int main()
{
   int i,j,temp;
    
   int myArray[] = {67,23,45,74,12,34};

   int n = sizeof(myArray)/sizeof(int);
   printf("\nArray before sorting: ");
   for(i=0;imyArray[j+1])
            {
                temp=myArray[j];
                myArray[j]=myArray[j+1];
                myArray[j+1]=temp;	
            }
        }
    }
            
    printf("\nArray after sorting: ");
    for(i=0;i

Output:

Array before sorting: 67 23 45 74 12 34
Array after sorting: 12 23 34 45 67 74

Both worst case and average case complexity is O (n^2) for bubble sort.

The post Bubble sort in C appeared first on Java2Blog.



This post first appeared on How To Learn Java Programming, please read the originial post: here

Share the post

Bubble sort in C

×

Subscribe to How To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×