Here you will learn about program for bubble sort in C.
Bubble sort is a simple sorting algorithm in which each element is compared with adjacent element and swapped if their position is incorrect. It is named as bubble sort because same as like bubbles the lighter elements come up and heavier elements settle down.
Both worst case and average case complexity is O (n2).
Bubble Sort in C
#include<stdio.h> int main() { int a[50],n,i,j,temp; printf("Enter the size of array: "); scanf("%d",&n); printf("Enter the array elements: "); for(i=0;i<n;++i) scanf("%d",&a[i]); for(i=1;i<n;++i) for(j=0;j<(n-i);++j) if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } printf("\nArray after sorting: "); for(i=0;i<n;++i) printf("%d ",a[i]); return 0; }
Output
Enter the size of array: 4
Enter the array elements: 3 7 9 2
Array after sorting: 2 3 7 9
Comment below if you have any doubts related to above program for bubble sort in C.
sir, i want to know what ide you use? can you send me link to down load it
I use codeblocks, download it from link given below:
http://thecrazyprogrammer.com/2013/06/gcc-compiler-download-codeblocks-1211.html
i++ and j++ can be used instead of ++i and ++j…?
Yes you can use, both will work same in this case.
yes you can
Yes
Sir can I get algorithm for this program?
Why we use return 0 ?
If we will not use it.what changes may occur in program?
We have write the written type of main as “int” so at last it should write something value ,,,so the return 0 is written.
If u use written type of main as “void main” then “getch()” will work fine
Can you help me sir? i dont know how to do it
1. Develop a program that can sort number up to 100 000 number using bubble sort algorithm
and calculate their complexity for BEST CASE scenario. The program should follow the
guidelines below:
i) The program will generate 100 000 number from 1 until 100 000 in ascending order,
and display all the numbers. All the numbers will be stored in one array.
ii) The program will sort the number section by section start from the first 10 000
numbers, then the first 20 000 numbers, then the first 30 000 numbers, until all the
100 000 numbers sorted. Use function looping for these operations.
iii) For each section, calculate time complexity in seconds, total number of comparison,
and total number of steps for the algorithm needed to solve the sorting problem.
iv) Lastly generate graph of time complexity, total number of comparison, and total
number of steps versus total of number to be sorted.
Sir algorithms of some basic c programming
Sir ,
How to write in c programming language sandip kumar ghatak to convert s k ghatak
print element which is grater than previouse
input: 1,2,5,-1,0,45,39,15,45,45
output:1,2,5,45
I want code for this problem,please reply
SIR BEFORE ENTERING THE ELEMENT IN THE ARRAY IT WILL AUTOMATICALLY TAKES VALUE SUCH AS -50-48-46 IN THIS ORDER WHAT CAN I DO?/
declare the array as 0 initially….maybe your declaration could be wrong
Can anyone explain me the logic which were implemented in the sorting?
bubble sort in best case,worst case and avarage case
programme
wrong answer dude
why we use n-i
thank you for the code.