We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
anon? C exer'ses: Bubble sor algorthm -w3resource
w3resource
C Exercises: Bubble sort algorithm
Last update on January 25 2017 11:38:59 (UTC/GMT +8 hours)
C Programming Searching and Sorting Algorithm: Exercise-3 with
Solution
Write a C program to sort a list of elements using the bubble sort algorithm.
Note: Bubble Sort works by repeatedly swapping the adjacent elements if they are in wrong order.
Pictorial presentation - Bubble sort algorithm :
Bubble Sorting
First Pass: Second Pass Third Pass
eee faeces
1)/2/4/]5/8
TO 3aP)
1/2
518
20 590)
1)/2/)/4|5|8
no swaps,
he 2|4/5 8|
5/8
Sample C Code:
Hnclude
void main()
hpiiwmw wresouree.comve-pragramming-exercises/searching-anc-sortinglo-search-and-sarting-exercisa-3.php
usanon? C exer'ses: Bubble sor algorthm -w3resource
int are[10};
Ant iy Jy Ny temps
/* function declaration */
int find.max(int bf10], int Kd
void exchang(int 10), int k);
print#("\nInput no. of values in the array: "):
scant ("a") &N);
print#("\nInput the elements: ");
for (isd; ieN 7 it)
‘
ant (
sd" garelil;
)
/* Selection sorting begins */
exchang (are,N);
printf ("Sorted array :\n");
forind; ie Nz ist)
‘
print ("d\n arr(i}):
)
}
/* faction to find the maximum value */
int find max(int BIO], int k)
ke assy
Af (bE) > imax)
max ~ ji
>
)
return (max):
,
id exchang(int b(10),inz ky
‘
int temp, big, 37
for (
‘
big = Sind max (b,3)7
temp = bIbigh:
b(big] ~ btj1s
DE] = temp;
>
)
Flowchart :
hpiiwmw wresouree.comve-pragramming-exercises/searching-anc-sortinglo-search-and-sarting-exercisa-3.phpanon? C exer'ses: Bubble sort algorthm -w3resource
Care)
1 blo} int &) ‘post
A far semi }
ee eS aemmems me ine ta.
lvoid exchang(int BLA}, int I); ‘Od aun ey
eet vant oust weeny
peace
psintf"\noput the elements: "),
ES
5
ne vee
return)
End
Jexchang(erN;,
printe"Sorted aay \n"),
leo
ie M?,
Yo Yes
Sue | [penn tw as
it
C Code Editor:
1 |Binclude
2 | void main()
(=)
Improve this sample solution and post your code through
qus.
Join our Question Answer community to learn and share your programming knowledge.
Help the communi
PHP: Loop
JavaSerip
: Delete duplicates in an array
Oracle: Round the number
hpiiwmw wresouree.comve-pragramming-exercises/searching-anc-sortinglo-search-and-sarting-exercisa-3.php 35