You are on page 1of 7

Chapt

er2-Sor
ti
ng
Sor
ti
ng:Iti
sthepr
ocessofar
rangi
ngel
ement
sofl
i
sti
nascendi
ngordescendi
ngor
der
.

Ty
pesofsor
ti
ng:
1.Int
ernalsort
ing:t
hesesor ti
ngt echniquesdoesnotr equi
reext
ramemor yspacetoper form
sorti
ngoperati
ononl i
stelement s.Itperfor
mschangesofelementspositi
onsintheoriginal
l
ist. Exampl e:Bubblesor t
2.Externalsor
ti
ng:thesesor t
ingt echniquesrequi
resextr
amemor yspacetoperform sorti
ng
operati
ononl i
stelements.I tperformssor t
ingandsor t
eddataisstor
edinext r
amemor y
space. Example:Mer gesor t

Ty
pesofsor
ti
ngt
echni
ques/
met
hods:
1.Bubblesort
2.Sel
ectionsort
3.I
nserti
onsort
4.Radixsort
5.Quicksort

Bubbl
eSor
t:Inbubbl
esor
t,sor
ti
ngbegi
nswi
th0thel
ement&i
tcompar
eswi
th1stel
ement
i
nthelist.If0th el
ementi sgreaterthan1stelementthennumber sar
einterchanged.I
nt hi
sway ,al
l
el
ement sar ecompar edwi thitsnextel
ement&i nter
changedifr
equir
ed.Attheendof1stpass, the
l
argestelementf r
om thelistisplacedatthel astposit
ion.I
npass2,againcompar i
sonst artswith
0&2ndl argestnumberi splacedatitsposit
ion.Inev er
ypassnumberofcompar i
sonsar er educed
byoneasanumberpl acedati tsfi
nalposit
ionf r
om bot t
om i
snotcompar edinnextit
eration.This
pr
ocesscont i
nuest i
l
l l
isti
ssor tedinorder
.

Exampl
e:I
nputl
i
st23,
15,
29,
11,
1&no=5(
numberofel
ement
sinl
i
st)

Pass1:

Pass2:

Pass3:
Pass4:

Sor
tedli
st:1,11,15,
23,29
Tot
alcomparisons=10
Tot
alexchanges=8

Al
gor
it
hm:
St
ep1:St art
St
ep2:Decl arev ar iables–ar r
aya[10],
count
erv
ari
abl
ei&j
,tempor
aryv
ari
abl
e-t
emp
St
ep3:Acceptar rayel ements
St
ep4:Initi
alizei to0
St
ep5:Repeatst ep6t il
li
<5
St
ep6:Initi
alizej to0
St
ep7:Repeatst ep8t il
lj
<=4-i
St
ep8:Compar e: i
fa[ j]>a[j
+1]then
Per f
ormi nter changingofelements:
Sett emp=a[ j]
Seta[ j
]=a[j+1]
Seta[ j
+1] =temp
Incr ementj by1andgot ost ep7.
Ot her wise
Incr ementi by1andgot ost ep5.
St
ep9:Di splaysor tedar r
ayelement s
St
ep10: Stop

Note:Twocount
erv
ari
abl
esar
eused:ii
sforcount
ingpassesandjf
orcompar
isonofar
ray
el
ements.

Pr
ogr
am:
#include<stdi o.
h>
#include<coni o.h> Out
put
:
voidmai n()
{ Ent
erfi
venumber sforsor
ti
ng:
8
i
nta[ 5],
i
,j
,t
emp; 4
clr
scr (
); 2
pri
ntf("\nEnterfivenumber
sforsor
ti
ng:
")
; 9
for(
i=0;i
<5;i
++) 1
{ sor
tedorder
:12489
scanf (
"%d",
&a[ i
])
;
}
for(
i=0;i
<4;i
++)
{
for(
j=0;j
<=4-i
;j++)
{
i
f(a[j
]>a[j
+1])
{
temp=a[ j
];
a[j
]=a[j
+1];
a[j
+1]=temp;
}
}
}
pri
ntf("sor
tedorder:
")
;
for
( i
=0;
i<5;
i++)
{
pri
ntf("
\n%d",a[
i]
);
}
getch();
}

Ti
mecompl
exi
ty:
O(n2)

Sel
ect
ionsor
t:-In sel
ect
ion sor
ti
ng,t
o sor
tdat
ain ascendi der0th el
ng or ementi
s
compar edwi t
hal lotherelement sf r
om t helist
.Ifthe0th elementi sgreaterthancompar ed
el
ementt hent heyar einterchanged.I nthisway,t he0th indexelementi scompar edwi t
hal l
el
ement stilllastelementandi frequiredtheninter
changed.Att heendofpass1,t hesmal lest
el
ementi spl acedat0thindexposition.Inthe2ndpass,1sti
ndexposit
ionelementi scomparedwi th
al
lelement splacedi nnextindexposi t
ionsofalist
.Thisprocesscontinuesti
llallel
ement
si nal i
st
areplacedi nsor tedorder.
Exampl e:InputLi st23,15,29,11,1andno=5( numberofel ementsinlist
)
Pass1:

Pass2:

Pass3:

Pass4:

Sor
tedl
i
st:
1,11,
15,
23,
29
Totalexchanges=8
Totalcomparison=10
Note:Twocount ervar
iabl
esar
eused:ii
sfi
xedi
ndexel
ementandji
sal
lar
rayel
ement
spl
aced
nexttoindexposit
ioni
.
Al
gor
it
hm:
St
ep1:St art
St
ep2:Decl arev ar iables–ar raya[
10],
count
erv
ari
abl
ei&j
,tempor
aryv
ari
abl
e-t
emp
St
ep3:Acceptar rayel ements
St
ep4:Initi
alizei to0
St
ep5:Repeatst ep6t il
li
<4
St
ep6:Initi
alizej toi +1
St
ep7:Repeatst ep8t il
lj
<5
St
ep8:Compar e: i
fa[ i]>a[j
]then
Per f
ormi nter changingofelements:
Sett emp=a[ i]
Seta[ i
]=a[j1]
Seta[ j
]=temp
Incr ementj by1andgot ostep7.
Ot her wise
Incr ementi by1andgot ostep5.
St
ep9:Di splaysor tedar r
ayelement s
St
ep10: Stop

Pr
ogr
am :
#include<st di
o.h>
#include<coni o.
h> Ent
erfi
venumber
sforsor
ti
ng:
voidmai n() 56
{ 78
i
nta[ 5],
i,
j,
temp; 34
clr
scr (
); 12
pri
ntf( "
\nEnt erfi
venumber
sforsor
ti
ng:
")
; 7
for(i
=0;i
<5; i
++) sor
tedorder
:
{ 7
scanf (
"%d" ,
&a[i]
); 12
} 34
for(i
=0;i
<4; i
++) 56
{ 78
for(j
=i+1;j<5;j
++)
{
i
f(a[i
]>a[j]
)
{
temp=a[ i];
a[i
]=a[j]
;
a[j
]=temp;
}
}
}
pri
ntf( "
sor tedorder
:"
);
for(i
=0;i
<5; i
++)
{
pri
ntf("\n%d" ,
a[i
])
;
}
getch();
}
Ti
mecompl
exi
ty:
O(n2)

I
nser
ti
onsor
t:Inserti
onsor
tisper
for
medbyi
nser
ti
ngapar
ti
cul
arel
ementatappr
opr
iat
e
posit
ion.I
nt hismethod,t he1stpassst ar
tswithcompar sonof1stel
i ementwi h0thel
t ement .If0th
el
ementi sgr eat
ert han1stel ementt hen0th el
ementi sshif
tedby1stposi ti
ont oit
sr ght&1st
i
t
h
posit
ionelementi sinsert
edi n0 posi ti
on.Inthenextpass,thecompar isonstartsf
rom 2ndindex
posit
ion.I
neachpass, anel ementi scompar edwi t
hal l
elementspl
acedbef oreiti
nalist
.
Int hi
smet hodi f0th indexel ementisgr eaterthancompar edelementt henallelement s
betweent hem areshiftedby1i ndexpositi
ont otheirri
ght&thencompar edelementisinserted.
Thisprocedurerepeatst i
l
l all
el ementsinalistisplacedinsort
edorder.
Example:InputList23,15, 29, 11,1

Pass1:
I
terat
i e1sti
on1:Compar ndexel
ementwi
th0th i
ndexel
ement
.23>15soshi
ft0th i
ndexel o1st
ementt
el
ement
.

23 15 29 11 1

15 23 29 11 1

Pass2:
I
terat
ion1: e2ndi
Compar ndexel
ementwi
th0thi
ndexel
ement
.15<29sonoshi
ftoper
ati
on.

15 23 29 11 1

I
ter
ati
on2: e2ndi
Compar ndexel
ementwi
th1sti
ndexel
ement
.23<29sonoshi
ftoper
ati
on.

15 23 29 11 1

Pass3:
I
terat
ion1: e3rdi
Compar ndexel
ementwi
th0thi
ndexel
ement
.15>11soshi
ftal
lel
ement
sfom 0thi
r ndext
o
2ndi
ndext
othei
rri
ght
.

15 23 29 11 1

11 15 23 29 1

I
ter
ati
on2: e3rdi
Compar ndexel
ementwi
th1sti
ndexel .15<11so nos
ement hi
ftoper
ati
on

11 15 23 29 1

I
ter
ati
on3: e3rdi
Compar ndexel
ementwi
th2ndi
ndexel
ement
.23<29sonoshi
ftoper
ati
on.

11 15 23 29 1

Pass4:
I
terat
ion1: e4thi
Compar ndexel
ementwi
th0thi
ndexel
ement
.11>1soshi
ftal
lel
ement
sfom 0thi
r ndext
o
3rdi
ndext
othei
rri
ght
.

11 15 23 29 1
1 11 15 23 29

I
ter
ati
on2,3, e4thi
4:Compar ndexel
ementwi
th1st,
2nd,3rdi
ndexel
ement
s.Al
lar
elesst
han29so
noshi
ftoper
ati
on.
Sosort
edli
st=1,
11,
15,
23,
29

Sor
ted
1 11 15 23 29

Al
gor
it
hm
2→ 25 23 29 11

11 15 23 29

23 15 29 25 1

15 23 29 25 1

15 23 25 29

Pr
ogr
am

#incl
ude<st di
o.h>
#incl
ude<coni o.h>
Voidmain()
{
iata[5]
,i,
j,k,c,e,ex=0,
co=0, temp=0;
pri
ntf(“
Enter5number sforsor
ti
ng:”
);
for(
i=0; i
<5; i++)
{
Scanf(“%d”,&a[ i]
);
}
/*Logicforsorting*/

for(i=1;i<5; i++)
{c-e=0;
for(j=0;j<i,j
++)
{c++;
i
f( a[
i]<a[
j])
{e++;
temp=a[ i]
;
for(k=i;k>=j ;k--)
{
a[k]=a[k-1]
;
}
a[k]=a[k-1]
;
}
a[k+1]=temp;
}
}
ex=ex+e;
co=co+c;
}
pri
ntf(“SortedLi st:
”)
;
for(i=0;i<5; i++)
{
pri
ntf(“%d”, a[i
j)
;
}
pri
ntf(“Total numberofcomparison:%d”,
(0);
pri
ntf(“Total numberofexchanges:%d”
,ex);
getch();
}

You might also like