## INDEX

### BUBBLE SORT USING C PROGRAM

Source code of simple bubble sort implementation using array ascending order in c programming language

#include<stdio.h>
int main(){

int s,temp,i,j,a[20];

printf("Enter total numbers of elements: ");
scanf("%d",&s);

printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);

//Bubble sorting algorithm
for(i=s-2;i>=0;i--){
for(j=0;j<=i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

printf("After sorting: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);

return 0;
}

Output:
Enter total numbers of elements: 5
Enter 5 elements: 6 2 0 11 9
After sorting:  0 2 6 9 11

5. Write a c program for heap sort.
7. Write a c program for shell sort.

1. If i wud write the loop as
for(i=0 to <s)
{ for(j=0 to<s-i)
{ j=i +1;
swap(a[j],a[j+1]);
}
)
den wud it give desired result?

1. no i think

2. yes it would give the desired result, but it will also increase the number of comparisons.
run your code with an example and the given code with the same example, you'll see the difference

1. Can u please explain how would it give the result that too when there is no use of if condition made?

3. Great tutorial
Tanx :)

4. Why this is used?

for(i=s-2;

1. s-2 is the second last element of d array ... and we are checking it out with j+1. that's why we have used s-2

2. become a contributor of ISeenLab and help us to educate our friends globally

5. but the first loop start at the end of the array, if you say s-2 how you will reach the last element in taht array

1. yeah absolutely right how to do for last element

2. ya i too have the dougt abt the same

6. in assending or desssinding order???

7. can you explain me this program in details?

1. hi ,what u want to ask in this prog,i can explain if you are interested..

8. Can u tell me the time complexity for the program "Bubble sort using c program"???

1. It remains same Big O(n^2)..U can run the program for 30 elements with a loop Counter to get a clear idea..Below 30 results will be confusing

9. this code is wrong ...atleast write exact code :(
Use for(i=0;i<s-1;i++)
{
for(j=0;j<s-i-1;j+)

10. i cant understand this s-2 and j+1.. could u pls explain me

1. i=s-2 marks the second last element and when j+1 reaches to i it checks the next value(last value) in array so that gets compared too..

11. can u explain me the algorithm of this bubble sort

12. this program is using arrays... what can i do for a bubble sort program using functions??

13. hey,.. this program is using array,.. what can i do to use bubblesort using functions..???

14. hi can u explain me the details of this code. I'm very interested in programming.I ll be very pleased if u help me !!

15. for those who don't understand s-2 concept, here it is. The total no. of elements is stored in variable S (let S=5). But in array the first element is stored in position a[0]. Hence the last element (i.e. 5th element) is stored in a[4]. Now i=S-2 ==> i=a[3].. Then if j<=i, then when j becomes equal to i (i.e. a[3]), the term j+1=[4] (i.e. the last term.

16. i cant understand the loop hre

17. This comment has been removed by the author.

18. This comment has been removed by the author.

19. how to display my output by showing my each iteration process..???

20. understand the whole program plzzzz

21. Easy to understand. ...

22. how to generate floyd triangle using while and if in c