Sunteți pe pagina 1din 7

bubble sort

The Bubble Sort


Te bubble sort !s o"e o# te s!$%lest sort $eto&s 'ou ()" use* T!s $eto& +or,s +ell
#or s!$%le &)t) stru(tures or !# te &)t) set to be sorte& !s $ore or less )lre)&' sorte&*
Te bubble sort !s -er' !"e##!(!e"t #or ) .e"er)l &)t) set* /" te bubble sort )l.or!t$0
su((ess!-e s+ee%s )re $)&e trou. te re(or&s to be sorte&* 1ur!". e)( s+ee%0 te
)l.or!t$ (o$%)res te ,e' to te &)t) ele$e"ts )"& s+)%s te ele$e"ts !# te' )re "ot
!" te &es!re& or&er* 2+)%%!". o((urs o"l' )$o". (o"se(ut!-e ele$e"ts o# te &)t) stru(ture*
3s ) result0 o"l' o"e ele$e"t !s %l)(e& !" !ts sorte& %l)(e )#ter e)( s+ee%* Te
sorte& ele$e"ts )re "ot "ee&e& #or (o$%)r!so" !" su((ess!-e s+ee%s* 4ere !s te
%seu&o(o&e #or bubble sort!". )" )rr)' o# n ele$e"ts !" )s(e"&!". or&er5
For iteration = 0 to (n-1)
Begin
For I = 0 to (n -1 - iteration)
Begin
if array[i] > array[i+1] then
swap array[i] and array[i+1]
end
end
3 677 !$%le$e"t)t!o" o# t!s )l.or!t$ !s so+" !" 8!st!". 9:*9*
LISTING 12.1. BUBBLE SORT IMPLEMENTATION
// Program: bubble_sort.cpp
// Author: Megh Thakkar
// Purpose: Sort an array of n elements in ascending order
// using the bubble sort method
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream.h>
class sort {
private:
int *X; //List of data elements
int n; //Number of elements in the list
public:
sort (int size) { X = new int[n=size]; }
~sort( ) { delete [ ]X; }
void load_list (int input[ ] );
void show_list (char *title);
void bubble_sort( int input[ ]);
};
void sort::load_list(int input[ ])
{
for (int i = 0; i < n; i++)
X[i] = input[i];
}
void sort::show_list( char *title)
{
cout << \n << title;
for (int i = 0; i < n; i++)
cout << << X[i];
cout << \n;
}
void sort::bubble_sort( int input[ ])
{
int swapped = 1;
char *title;
load_list(input);
show_list(List to be sorted in ascending order using bubble sort);
// The FOR loop is executed once for each element in the array.
// At the end of each iteration, one element will be bubbled to
// its correct position and is not considered for further iterations
// of the loop.
for ( int i = 0; i < n && swapped == 1; i++)
{
// If at the end of an iteration there was no swapping done
// then it indicates that the list is sorted as desired and
// there is no need to do more iterations.
swapped = 0;
for (int j = 0; j < n-(i+1) ; j++)
// if X[j] > X[j+1] then it indicates that they are out of order.
// Therefore swap them.
if ( X[j] > X[j+1] )
{
int temp;
temp = X[j];
X[j] = X[j+1];
X[j+1] = temp;
swapped = 1;
}
}
show_list(List sorted in ascending order using bubble sort);
}
//main( ) : Test driver for bubble sort
void main(void)
{
// Create a new object which has the method bubble_sort
sort sort_obj(5);
static int unsorted_list[] = {54,6,26,73,1};
sort_obj.bubble_sort(unsorted_list);
}
3s 8!st!". 9:*9 so+s0 te $);!$u$ "u$ber o# s+)%s o((urs +e" te l!st !s sorte& !"
te re-erse or&er o# te &es!re& sort!". or&er* /" oter +or&s0 $);!$u$ s+)%s o((ur !#
'ou +)"t to sort te l!st !" )s(e"&!". or&er but te l!st !s (urre"tl' sorte& !" &es(e"&!".
or&er*
Analysis of the Bubble Sort
Te bubble sort )s se-er)l ()r)(ter!st!(s5
< 3#ter e)( !ter)t!o"0 o"l' o"e &)t) ele$e"t !s %l)(e& !" !ts %ro%er sorte& %os!t!o"*
< Te bubble sort &e%e"&s o" te (o$%)r!". )"& s+)%%!". o# consecutive &)t) ele$e"ts*
< /" e)( e;e(ut!o" o# te !""er loo%0 tere )re )t $ost (n-iteration-1) "u$ber o#
s+)%s*
< Te +orst=()se s(e")r!o !s +e" te &)t) ele$e"ts )re re-erse or&ere&*
< Te best=()se s(e")r!o !s +e" te &)t) ele$e"ts )re almost sorte& !" te (orre(t
or&er*
< Te bubble sort )s ) s!$%le !$%le$e"t)t!o"*
>?@ A BC AD
Te bubble sort .ets !ts ")$e be()use )s ele$e"ts )re sorte& te' .r)&u)ll' EbubbleE For
r!seG to te!r %ro%er %os!t!o"s0 l!,e bubbles r!s!". !" ) .l)ss o# so&)* Te bubble sort
re%e)te&l' (o$%)res adjacent elements o# )" )rr)'0 st)rt!". +!t te #!rst )"& se(o"&
ele$e"ts0 )"& s+)%%!". te$ !# te' )re out o# or&er* 3#ter te #!rst )"& se(o"& ele$e"ts
)re (o$%)re&0 te se(o"& )"& t!r& ele$e"ts )re (o$%)re&0 )"& s+)%%e& !# te' )re out
o# or&er* T!s %ro(ess (o"t!"ues u"t!l te e"& o# te l!st !s re)(e&*
He" te e"& !s re)(e&0 te bubble sort retur"s to ele$e"ts o"e )"& t+o )"& st)rts te
%ro(ess )ll o-er ).)!"* 2o0 +e" &oes !t sto%I The bubble sort knows that it is
finished when it examines the entire array and no "swaps" are needed (thus the list
is in proper order). Te bubble sort ,ee%s tr)(, o# te o((urre"(e o# s+)%s b' te use
o# ) #l).*
Te t)ble belo+ #ollo+s )" )rr)' o# "u$bers be#ore0 &ur!".0 )"& )#ter ) bubble sort #or
descending or&er* 3 E%)ssE !s &e#!"e& )s o"e #ull tr!% trou. te )rr)' (o$%)r!". )"& !#
"e(ess)r'0 s+)%%!".0 adjacent ele$e"ts* 2e-er)l %)sses )-e to be $)&e trou. te
)rr)' be#ore !t !s #!")ll' sorte&*
Array at beginning: JK LM NL JL MK M9
After ass !": JK NL JL MK M9 LM
After ass !#: JK JL MK M9 NL LM
After ass !$: JL MK M9 JK NL LM
After ass !%: MK M9 JL JK NL LM
After ass !& (done): MK M9 JL JK NL LM
Te bubble sort !s )" e)s' )l.or!t$ to %ro.r)$0 but !t !s slo+er t)" $)"' oter sorts*
H!t ) bubble sort0 !t !s )l+)'s "e(ess)r' to $),e o"e #!")l E%)ssE trou. te )rr)' to
(e(, to see t)t "o s+)%s )re $)&e to e"sure t)t te %ro(ess !s #!"!se&* /" )(tu)l!t'0
te %ro(ess !s #!"!se& be#ore t!s l)st %)ss !s $)&e*
// Bubble Sort Function for Descendin !rder
void bubble_sort(apvector <it! "arra#$
%
it i& '& (la) * +, // set fla to 1 to bein initial "ass
it te-p, // holdin #ariable
it arra#Le)t. * arra#/le)t.( $,
(or(i * +, (i <* arra#Le)t.$ "" (la), i00$
%
(la) * 1,
(or ('*1, ' < (arra#Le)t. 2+$, '00$
%
i( (arra#3'0+4 ! arra#3'4$ // ascendin order si$"ly chanes to %
%
te-p * arra#3'4, // s&a" ele$ents
arra#3'4 * arra#3'0+4,
arra#3'0+4 * te-p,
(la) * +, // indicates that a s&a" occurred.
5
5
5
retur, //arrays are "assed to functions by address' nothin is returned
O C ?P C Q A
!"t $)!"FG
R
()r S"o$)T:UUVWXX%!"),)s $e o"o$)t)
!"t TY!rosT:UUVWXX%!"),)s $e tY!ro
!"t !0ZWXX$et)bl!tes )l.or!t$ou
!"t te$%WXX$et)bl!t! .!) to s+)%F[)"t!$et)tes!G tou tY!rou
()r ")$eW XX$et)bl!t! .!) to s+)% tou o"o$)tos

#or F![UW!\:UUW!77GXXe$#)"!s! %!"),-"
R
%r!"t#FE]J&E0S"o$)T!VGW
%r!"t#FE]J&E0TY!rosT!VGW
^

#orF![UW!\:UUW!77G XXbubble sort .!) t) o"o$)t)
R
#orFZ[UWZ\!WZ77G
R
!#Fstr($%FS"o$)T!V0 S"o$)TZVG _ UG XXsu.,r!"e! %!o str!". %)e! %r-to
R XXs)" ") .r)#)$e !# Fo"o$)T!V_o"o$)TZVG
te$%[S"o$)T!VW XXs+)% o"o$)t-"
S"o$)T!V[S"o$)TZVW
S"o$)TZV[te$%W

")$e[TY!rosT!VW XXo%ote )"t!$et)tetou$e sto!;e!) st) o"o$)t)***
TY!rosT!V[TY!rosTZVW XX)ll)You$e ,)! )ut) sto tY!ro $e to" !&!o tro%o
TY!rosTZV[")$eW
^
^
^

#or F![UW!\:UUW!77GXXe$#)"!s! )l%)b!t!,) t);!"o$!$e"-" %!"),-"
R
%r!"t#FE]J&E0S"o$)T!VGW
%r!"t#FE]J&E0TY!rosT!VGW
^

#orF![UW!\:UUW!77G XXbubble sort .!) to" tY!ro
R
#orFZ[UWZ\!WZ77G
R
!#FTY!rosT!V_TY!rosTZVG
R
te$%[TY!rosT!VW XXs+)% tY!rou
TY!rosT!V[TY!rosTZVW
TY!rosTZV[te$%W

")$e[S"o$)T!VW XXo%ote )"t!$et)tetou$e sto!;e!) sto" tY!ro***
S"o$)T!V[S"o$)TZVW XX)ll)You$e ,)! )ut) sto o"o$) $e to" !&!o tro%o
S"o$)TZV[")$eW
^
^
^

#or F![UW!\:UUW!77GXXe$#)"!s! tel!,-" %!"),-"
R
%r!"t#FE]J&E0S"o$)T!VGW
%r!"t#FE]J&E0TY!rosT!VGW
^
^

S-ar putea să vă placă și