Вы находитесь на странице: 1из 30

ABSTRACT

This project deals with one of the sorting techniques, which is BUBBLE
SORT. It is priaril! concerned with ai of siplif!ing it"s prograing
ipleentation, and o#jecti$el! aied at a%ing articulation and through
anal!sis on certain ajor characteristics of each Bu##le Sorting techniques
to show how it wor%s so that desired choice can #e ade at an! point in
tie.
There is no sorting ethod that is #est in all aspect, each has its ad$antages
and disad$antages depending on the nature of input data, coputer
hardware and prograing strength. &ence an attept is ade to show the
area the sorting technique wor%s #est, so that an intelligent choice can #e
ade at an! gi$ing situation.

Sorting is the uni$ersal fundaental acti$it! and its iportance in coputer
science can ne$er #e o$er ephasi'ed
1
TABLE OF CONTENT
Title page I
Certification II
Dedication III
Acknowledgement IV
Abstract V
Table of content VI

CHAPTER OE
1!" Introd#ction 1
1!1 Ob$ecti%e Of &t#d' (
1!( &ignificance Of &t#d' (
1!) Definition Of Tec*nical Terms )
1!+ Detac*ed ke' )
1!, Comp#ter arc*itect#re +
1!- Vol#me of data to be sorted +
1!. Ordering of t*e data +
CHAPTER T/O
(!" 0iterat#re Re%iew -
(
CHAPTER THREE
)!" Algorit*m And Design 1
)!1 C*aracteristics of an Algorit*m 1
)!( 2#bble &orting 3
)!) Algorit*m for 2#bble &ort 1"
)!+ Impro%ed 2#bble &ort 11
)!, Algorit*m for Impro%ed 2#bble &ort 11
CHAPTER 4O5R
+!" Coding And Implementation 1)
+!1 Amo#nt of Information 1)
+!( Anal'sis Of 2#bble &ort 1,
+!) Program Implementing 2#bble &ort 11
CHAPTER 4IVE
,!" Concl#sion ("
,!1 Recommendations (1
RE4ERECE&
APPEDI6
)
CHAPTER ONE
INTRODUCTION
7ost data processing is concerned wit* sorting and searc*ing of data!
&orting data re8#ires p#tting t*e raw data in some predetermined order9
w*ic* mig*t be Alp*abetical if t*e data is set of words9 or it mig*t be in
ascending or descending order of se8#ence if t*e data is #merical!
&earc*ing means finding a specific item of data from a list of data items!
T*e tec*ni8#es #sed for t*ese processes depend %er' m#c* on t*is wa' t*e
data sorted9 and on t*e arc*itect#re of t*e comp#ter data to be sorted can be
in t*e main memor'9 magnetic disk:tape or e%en a tape streamlines!
Efficient sorting or searc*ing Algorit*m to #se!
T*ere are se%eral met*ods of sorting9 b#t one of t*e most pop#lar 7et*od is
called 25220E &ORT! T*e name dri%en from t*e fact t*at t*e sorted items
flow to t*e top of t*e list like a b#bble in water!
+
OBJECTIVE OF STUDY
T*is st#d' is aimed at;
<1= Determining t*e efficienc' of t*is tec*ni8#e
<(= Determining it>s effecti%eness #nder t*e following captions
<a= Vol#me of data
<b= Identif' t*e most efficient or appropriate tec*ni8#e
<c= Identif' t*e %ario#s t'pes of b#bble sorting tec*ni8#es
SIGNIFICANCES OF STUDY
T*is is as a res#lt of researc* work9 w*ic* will be %er' *elpf#l to t*e
organi?ation9 indi%id#al9 e! t! c! and effecti%e sorting tec*ni8#es leads to
effecti%e eas' work!
T*is #ltimate aim of e%er' s'stem is on *ow to get t*e best of sorting
tec*ni8#es t*at will *elp ser%e a %er' #sef#l p#rpose to s#c* s'stem and
acc#rac'! T*e impro%ed sorting tec*ni8#e en*ances t*e le%el of prod#cti%it'
in s#c* a s'stem!
,
T*is will also be of aid to sc*ools since an impro%ed tec*ni8#es increases
t*e acc#rac' and time spend on processing res#lt in sc*ools and processing
$obs!
DEFINITION OF TECHNICAL TERMS
DETACHED KEYS
In most application t*e information content of t*e records is large! On t*e
ot*er *and9 t*e ke' itself is #s#all' 8#ite s*ort9 t*at we can considerabl'
impro%e on t*e efficienc' of o#r Algorit*ms b' detac*ing t*e ke' from t*e
record and not s#bse8#entl' mo%ing t*e records d#ring t*e process of
algorit*ms! If t*e ke' is detac*ed from t*e information part of t*e record9 it
m#st be a#gmented b' t*e address of it>s associated information! T*en t*e
sorting met*od is carried o#t on t*e a#gmented ke's and t*e information
part of eac* record remains in t*e same place in t*e memor'! T*e store ma'
allow rapid access e8#all' 8#ickl' to an' of t*e records fast9 random access
e! g core stores!
0ess rapid access ma' be possible again random or nearl' so9 for e@ample
wit* t*e magnetic dr#m or disc stores!
-
Access ma' be possible onl' to records in a definite se8#ence as on t*e
magnetic tape!
VOLUME OF DATA TO BE SORTED
T*e %ol#me of data to be stored for sorting if %er' large will limit t*e tape of
storage t*at can be #sed9 since onl' t*e lower backing stored will be
s#fficientl' large!
COMPUTER ARCHITECTURE
T*e comp#ter arc*itect#re *as it appears to t*e #ser9 also determine t*e
sorting met*od9 t*e important effect are closel' connected wit* t*e pre%io#s
factors of comp#ter storage9 t*e wa' storage is connected and organi?ed
bot* t*e *ardAware and operating s'stem! Rele%ant considerations are central
processing speed relati%e to store access time9 w*et*er t*e operating s'stem
is #sing paging or anot*er storage allocation met*od and w*at specific
algorit*ms are emplo'ed!
THE ORDERING OF THE DATA
.
&ome of t*e sorting met*od performance is considerabl' impro%ed if t*e
data are alread' partiall' order! It is t*erefore #se f#ll to know w*et*er t*e
data will be random9 or partiall' ordered9 or consist of se%eral ordered
s#bse8#ences!

<1= To pro%ide fast access to t*e information b' gi%ing an appropriate ke'
<(= To allow for an orderl' presentation of information w*en prod#cing
reports!
1
CHAPTER TWO
LITERATURE REVIEW
Alread'9 a lot *a%e been done on 2#bble sort9 t*ing or books t*at can *elps
to a good or better anal'sis on 25220E &ORT!
In 2A&IC PROBRA77IB9 writing b' 2! C Holmes <p! 1+(A1,)=! He
described 2#bble &ort9 to be starring at position9 w*ere one ad$acent pairs of
ke's are compared9 if t*e' are not in order9 t*e ke' position are swap and
t*en mo%ed to t*e ne@t ke' poison9 t*is proced#re is repeated #ntil
e%ent#all' last pair of ke' on t*e table are compared! He f#rt*er described
b#bble sort as been pop#lar and t*at it is #sed for sorting small amo#nt of
data <less t*an ," items=!
In DATA &TR5CT5RE9 written b' Edward 7! Reingold and /ilfred C!
Hanson <p! )1(A)3+=! DT*e a#t*ori?es described b#bble sort as well known
b#t inefficient in f#rt*er description of b#bble sort t*e' t*at b#bble sort is an
ob%io#s to s'stematicall' interAc*ange Eo#tA of Eorder part of element b'
scanning ad$acent pairs of element from left to rig*t9 repeatedl' inter
c*anging t*ose f#nd o#tAof Eorder! T*e' also p#t it t*at anal'sis of t*e
3
b#bble sort depends on t*e n#mber of passes9 t*e n#mber of comparisonsF!
F6CGAH6C I I
nF
and t*at t*e interc*ange D6$GAH6$ ICF t*e' state t*at t*e
n#mber of interc*ange )n:J d$ and to it>s left; " in it>s best
)n<nA1=:( in t*e worst case
)n<nA1=:+ on t*e a%erage
&till on anal'sis9 t*e' stated t*at t*e n#mber of passes for b#bble sort is one
pl#s t*e largest d$; in t*e bestAcase worst case!
PA&CA0 P05& DATA &TR5CT5RE& b' ell Dale and &#san C! 0ill'9
p#t t*#s abo#t b#bble sort9 t*at b#bble sort is a selection sort t*at #ses a
different sc*eme for finding t*e minim#m <or ma@im#m= %al#e! Eac*
iteration p#ts t*e smallest #nsorted element in its correct place9 b#t it also
makes c*ange in t*e location of t*e ot*er element in t*e arra'! T*e first
iteration will p#t t*e smallest element in t*e arra' in t*e position! T*e'
started wit* t*e element in t*e nt* position and compares s#ccessi%e pairs
In elements swapping w*ere%er t*e bottom element of t*e plain is smaller
t*an t*e one abo%e it! T*e ne@t iteration p#ts t*e smallest element in t*e
#nsorted part of t*e arra' in to t*e second position #sing t*e same
tec*ni8#es!
1"

CHAPTER THREE
ALGORITHMS AND DESIGN
One of t*e f#ndamental problems of comp#ter science is ordering a list of
items! T*ere>s a plet*ora of sol#tions to t*e problem9 known as sorting
algorit*ms! &ome &orting algorit*ms are simple and int#iti%e9 s#c* as b#bble
sort! Ot*ers9 s#c* as t*e 8#ick sort are e@tremel' complicated9 b#t prod#ce
lig*tening fast res#lt!
An algorit*m ma' be defined as a finite se8#ence of instr#ction #sed in
sol%ing a problem! In ot*er words9 it ma' be defined as a prKcised stepAb'A
step proced#re #sed in sol%ing a problem!
CHARACTERISTIC OF AN ALGORITHM
An algorit*m possesses t*e following basic c*aracteristicsL
<1= An algorit*m begins wit* instr#ctions to accept inp#t! T*ese inp#ts are
processed s#bse8#ent instr#ctions in t*e algorit*m!
<(= T*e processing r#le specified in t*e algorit*m m#st be prKcised and
ambig#o#s! In ot*er words t*e instr#ctions m#st not be %ag#e! It m#st
11
also be possible to carr' t*em o#t e! g t*e instr#ction FBo to *ellF is
prKcised9 b#t can not be carr'o#t!
<)= T*e total time to carr' o#t all t*e steps in t*e algorit*m m#st be finite!
<+= Eac* instr#ction m#st be s#fficientl' basic s#c* t*at a person wit* paper
and pencils can in principle carr' it o#t in f#t#re time!
<,= An algorit*m m#st prod#ce one or more o#t p#ts <namel' t*e res#lts of
processing t*e inp#t=!
DE&CRIPTIO; t*is per*aps t*e ob%io#s wa' to sort b' e@c*ange9 if sorts
b' compassing M1 and M(9 interc*anging R1 and R( if t*e ke's are not in
order9 repeated passes o%er t*e arra' is made eac* time s*ifting t*e last item
of t*e remaining set to t*e left end of t*e arra'9 if for a c*ange we %iew t*e
arra' %ertical instead of *ori?ontal position and wit* t*e *elp of some
imaginations t*e items as b#bbles in water tank wit* weig*t according to
t*eir ke'9 t*en e@c*ange pass o%er t*e arra' res#lt in t*e ascension of t*e
b#bble to it>s appropriate le%el of weig*t ! Partic#larl'9 t*e smallest ke' is
b#bble to t*e top on t*e first pass! In t*e first passes we start wit*
compassing t*e t* and A1" ke's and e@c*ange t*em if necessar'9 and we
contin#e and e@c*ange t*em if t*e' are in wrong order!
1(
T*#s a ke' is b#bbled #p9 in t*e second pass we work from t*e nt* list to t*e
second ke'9 since t*e first m#st be t*e first pass9 be t*e smallest ke'!
ALGORITHMS FOR BUBBLE SORT
&TEP 1;0OOP9 for k 1nA1
Do &TEP& to .
&TEP (; A&&IB CN.9 I <I=
&TEP ); 0OOP O M9 4OR MNI II TO
DO &TEP + TO
&TEP +; I4 A <C=G6 THE DO &TEP ,
E0&E &TEP -
&TEP ,; A&&IB M NC9 6NA<I=
&TEP -; A <M=NA <I=9 A <I=N6
&TEP .; COTI5E <4OR I AD C=
&TEP 1; ED
1)
IMPROVE BUBBLE SORT
DE&CRIPTIO;
2#bble sort m#st be impro%ed b' remembering w*et*er or not an' e@c*ange
*as taken place d#ring a pass! In general <nA1= are necessar' to ens#re t*at
t*e lists of ke's are completel' sorted! /e can make o#r algorit*ms more
efficient for ot*er data b' incl#ding eac* pass an indicator %ariable <flag= !
t*is flag is set tr#e at t*e start of a pass and c*anges to false at an' e@c*ange
is made!
T*#s we can terminate t*e algorit*m w*ene%er t*e flag is tr#e at te end of a
pass since no e@c*ange *a%e been made and t*erefore alread' ordered! T*is
is ill#strate at were se%en ke's are sorted9 in general 9 si@ passes are
necessar' b#t t*e fift* pass no c*ange took place and
ALGORITHM FOR IMPROVED BUBBLE SORTING
&TEP 1 ; 0OOP O I9 4OR I NI TO &TEP 1 5TI0 A1
DO &TEP ( TO 1"
&TEP (; A&&IB E0&E NME<=9 40AB N TR5E
&TEP ); 0OOP O 69 4OR 6N &TEP 1 5TI0 I I 1
1+
DO &TEP + TO .
&TEP +; I4 MEO& <6A1= G E&E THE
DO &TEP , E0&E -
&TEP ,; A&&IB MEO <6= N E&E9 E&EN MEO <6A1=
&TEP -; A&&IB MEO <6= NMEO <6A1= 40AB N 4A0&E
&TEP .; COTI5E <4OR 6=
&TEP 1; A&&IB MEO <I= NE&E
&TEP 3; I4 40AB THE BO TO &TEP 11
&TEP 1"; COTI5E <4OR 1"=
&TEP 11; ED
1,
CHAPTER FOUR
CODING AND IMPLEMENTATION
A lot of tec*ni8#es for sorting *ad been considered w*ic* confirmed t*at
t*ere are man' wa's to sol%e an' gi%en problem! In comp#ting9 as in most
area of *#man endea%or9 we are concerned wit* sol#tion of problem! T*is
*o#ses certain 8#estions as to w*at we mean b' a good sol#tion to problem!
In t*is case9 good *as bot* 8#alitati%e and 8#antitati%e aspect! T*ere are
often certain est*etics and personal aspect to t*is b#t9 on a more practical
le%el9 we are #s#all' interested in t*e sol#tion t*at is economical in t*e #se
of comp#ting and *#man reso#rces! T*e facts t*at are to be considering
before c*oosing a sorting tec*ni8#es are;
<1= AMOUNT OF INFORMATION: T*ere ma' be small or large! In t*e
case of large amo#nt of information9 t*e e@ternal met*od of sorting will
t*en be most ideal to be #sed! 4or small amo#nt of information it is
better to #se t*e internal sorting met*od! T*e amo#nt of information
goes along to determine w*et*er a simple and ad%ance or radi@ sorting
tec*ni8#e s*o#ld be #sed at a partic#lar point in time!
1-
<(= TIME OF CONSUMPTION: t*e r#nning time of eac* of t*e sorting
will consider t*e time it will take a met*od to complete it sorting or
bring b' comparing *em wit* anot*er!
<)= THE URGENCY: t*e nat#re of application determines t*e t'pe of
sorting t*at is to be applied! In most cases9 t*e appropriate one s*o#ld be
#sed!
<+= THE PARTICULAR LANGUAGE IMPLEMENTATION
ALGORITHM: t*e lang#age t*at is best #nderstood and pop#larl'
known wit*in t*e organi?ation determines t*e sorting tec*ni8#es t*at is
to be c*osen9 e! g 2asic Pascal 4ortran and Cobol!
<,= SORTING ORDER: t*e order of sorting w*et*er descending or
ascending as t*e case ma' be9 determines t*e tec*ni8#e t*at is to be
#sed!
<-= THE CONFIGURATION OF THE COMPUTER: t*at is arc*itect#re
of comp#terL determine t*e sorting tec*ni8#es to be emplo'ed! &ome
1.
compilers *a%e %er' small memor' capacit' in w*ic* case9 one can
begin to consider t*e simple tec*ni8#es rat*er t*an ad%ance one!
T*e 8#alitati%e aspect of a good sorting tec*ni8#e are %er' important b#t are
also necessar' to pro%ide some 8#alitati%e meas#res to complete t*e
e%ol#tion of goodness of sorting tec*ni8#es! P#antitati%e meas#re are
%ariable in t*at t*e' can be #se in awa' of directing predicting t*e
performance of two or more sorting! T*is is important beca#se t*e #se of
sorting tec*ni8#es t*at is more efficient means in comp#ting reso#rces9 will
translate into sa%ing in time and mone'! &ome of t*e 8#antitati%e meas#res
#sed to determine a sorting performance are;
<1= Comp#tational Comple@it'
<(= Order otation
<)= T*e worst and a%erage be*a%ior
ANALYSIS OF BUBBLE SORT
In b#bble sort9 t*e n#mber of passes will be e8#al to t*e ma@im#m n#mber
of places between t*e ke's in!
It>s initial and final position! Here t*e n#mber of p*ases is <nA1=9 t*e n#mber
of comparison is t*e same as in selection sort9 w*ic* is <nA1=! 4or randoml'
11
order data9 t*e aspect n#mber of passes sa%ed are t*ose9 w*ic* contains t*e
fewest n#mber of comparisons9 and t*e partic#lar effect is not large!
T*e main ad%antages of 2#bble &ort is t*at it can be %er' 8#ick wit* data9
w*ic* are nearl' order in t*at no item is far from it>s initial position! T*e
n#mber of interc*ange re8#ired b' 2#bble sort is consider more t*an t*e
selection met*od! 4or eac* pass t*e e@pected n#mber of interc*ange will be
appro@imatel' *alf t*e n#mber of comparison! 4or nA1 passes9 t*is gi%es t*e
n#mber of interc*ange as n <nA1=:+! 2#bble sort re8#ires %er' little e@ternal
storage space
7mimN19 m a%erage N Q <n
(
An=
7ini@ N ):( <n
(
A(
"
=

T*e anal'sis in%ol%es t*at of impro%ed b#bble sort! T*e least n#mber of
comparison Cmin N nA1 for t*e s*aker sort!
T*e impro%ed b#bble sort and s*akers sort do in no wa' effect t*e n#mber
of e@c*angeL t*e' onl' red#ce t*e n#mber of red#ndant do#ble c*ecks!
5nfort#natel'9 an e@c*ange of two items is generall' a m#c* more costl'
13
operation t*an comparison of ke's! T*#s9 t*e impro%ed b#bble sort and
s*aker sort *a%e a m#c* less pro#d effect t*an one wo#ld initiall' e@pect!
("
PROGRAM IMPLEMENTING BUBBLE SORT.
C PROBRA7 TO DE7O&TRATE 25220E &ORT
C TECHIP5E IT &ORT& THE ITE7 I EITHER
C A&CEDIB OR DE&CEDIB ORDER
C PROBRA77ER; OMORIE 20E&&IB BOJI
DI7E&IO CA72 <,"=
/RITE <R9 R= SHO/ 7AO ITE7& DO OO5 /AT TO &ORT>
READ <R9 1"=
/RITE <R! R=> MEO& I THE ITE7&
READ <R9! 1"=< CA72 <1=9 1N19=
1" 4OR7AT <1+=
C &ORT O5T THE 572ER 2O CO7PARI&O
DO 1"" I N19 A1
0 N I I1
DO ("" CN 09
I4 <CA72 <I=! 0T! CA72 <C== BO TO (""
0 CA72 N CA72 <I=
CA72 <C=NCA72 <I=
CA72 <C=N0CA72
("" COTI5E
1"" COTI5E
(1
/RITE <R9 R= S/HAT ORDER DO OO5 /AT TO &ORT T>
/RITE <R9 R=> P5T 1 4OR A&CEDIB AD ( 4OR DE&CEDIB>
READ <R9 1= C
I4 <C!P!1= THE
BO TO 1+(
E0&E
BO TO 1+3
ED I4
133 /RITE <R9 R=> THE 572ER I A&CEDIB ORDER ARE>
/RITE <191+"= <CA72 <1=9 IA19=
1+" 4OR7AT <,"9 1+=
BO TO 1""
ED
((
CHAPTER FIVE
CONCLUSION
/it* t*e knowledge of t*e c*aracteristic and performance of 2#bble sort9 an
intelligent c*oice for a pop#lar application so t*at bo#t mone' and time are
optimi?ed!
In 2#bble &ort9 one can obser%e 8#ickl' t*at t*e best case for 2#bble &ort
occ#rs w*en t*e list is read' in order9 w*en 2#bble sort will onl' do <nA1=
comparison of ke's! T*is is a %er' eas' sorting met*od w*ic* instead of
#sing two pointer to mo%e t*ro#g* t*e list9 it #ses onl' one and it proceeds
b' starting at one end and mo%ing forward comparing ad$acent pairs of ke's
#ntil it finds a pair of order9 it t*en swaps t*ese pairs of items and start
mo%ing t*e ot*er wa'! It contin#es to swap pairs #ntil it finds a pair in t*e
correct order!
()
RECOMMENDATION
At t*is point9 t*e a#t*or recommended t*at since 2#bble &ort w*ic* is of
greater performance in man' aspect is rec#rsi%e9 attention s*o#ld be drawn
implementing rec#rsion in lang#ages like 4ORTRA9 CO2O0 and 2A&IC
t*at do not s#pport rec#rsion9 so t*at after c*oosing a partic#lar tec*ni8#e
<2#bble &ort=9 t*at fits t*e config#ration of data or so a*ead9 one is not
f#rt*er restricted b' lang#age implementation!
Also9 since &orting #ni%ersall' perform f#ndamental acti%it'9 comp#ter
designers s*o#ld incl#de a b#iltAin smallest operation t*at operates at *ig*
speed and conse8#entl' makes some comp#ters and rat*er t*an restricting
t*em from c*oosing a partic#lar *ardAware for a partic#lar sorting met*od!
(+
REFERENCES
KRUSE, ROBERT L. <1313= Data &tr#ct#re and program design
(
nd
Edition P#blis*er!
PrenticeA*all of India
<p! (()A(-.=
KNUTH, DONALD E. <13.+= T*e art of comp#ter programming
Vol#me )
P#blis*er; AddisionA/esle'
P#blis*ing Comp#ter <p! .)A).3=
HOLMES, B. J <1313= 2asic programming )
rd
Edition
P#blis*er; DP P#blication limited
<p! 1+1A1+3=!
PAGE, E. S AND WILSON L. B Information representation and
<13.3= 7anip#lation in a comp#ter (
nd

Edition !
P#blis*er Cambridge #ni%ersit' press
(,
<p! 31A1"+=!
BROMEY, R. G. <1313= How to sol%e it b' comp#ter )
rd

Edition P#blis*er Prencie of India
<P! ))A )-=
NIRTH, NIKLAUS <1311= Algorit*ms IData &tr#ct#re N
Programs9 P#blis*er prentice Hall of
India <p! ,-A3)=
TREMBLAY J. P. & SORENSON an introd#ction to data &tr#ct#re wit*
P! B <13.-= application 1
st
Edition9 7! C BrawA
Hill book compan' <P! +-,A+11=
(-
APPENDIX 1
FLOW CHART FOR BUBBLE SORT
(.
&TART
0OOP O I
IN19(UA1
INI I1
MEO<6= NA<1=
0OOP O M
MNIII9II(9U
CNCII1
CO7PARE A <C= /ITH 6
A C
2
O
OE&
O
OE&
O
OE&
(1
A
A
I&
A<C=N6V
I&
CNV
C MN C9 6 NA <C=
A <M= NA <I=9 A <I= N 6
2
&TOP
I&
INA1V
APPENDIX 2
FLOW CHART FOR IMPROVED BUBBLE SORT
(3
&TART
0OOP O I
IN19(UsA1
E&ENMEO <=
&ET 40AB
2
0OOP O 6
6N9 A19UII1
6 N 6A1
CO7PARE MEO <6A1= /ITH E&E
A2
C2
OE&
O
O
OE&
O O
OE& OE&
)"
2
&TOP
A
2
I&
MEO<6A1=G 6V
MEO <6= N E&E
E&E NMEO <6A1=
I&
I N A1V
I&
40AB &ETV
MEO <I= E&E
I&
6 NI I 1V
C
2
MEO<6= N MEO<6A1=
40AB DI&A20ED

Вам также может понравиться