Você está na página 1de 17

Sorting Techniques

http://www.iitg.ernet.in/psm/indexing_ma513/y09/index.htm
/! "u##e sort. !/
$incude %stdio.h&
$incude %conio.h&
'oid main( )
*
int a+50,-n- i- .- temp /
crscr( ) /
print0 ( 12ow 3any 4um#ers 5) /
scan0(56d7-8n)/
print0 ( 19nter the 9ements:n1) /
0or ( i ; 0 / i %; < / i== )
scan0(56d7-8a+i,)/
0or ( i ; 0 / i % n>1 / i== )
*
0or ( . ; 0 / . %; (n > 1) > i / .== )
*
i0 ( a+., & a+. = 1, )
*
temp ; a+., /
a+., ; a+. = 1, /
a+. = 1, ; temp /
?
?
?
print0 ( 19ements 0ter sorting::n1) /
0or ( i ; 0 / i % n>1 / i== )
print0 ( 16d:t1- a+i, ) /
getch( ) /
?
Sorting Techniques
/! @nterchange Sort. !/
$incude %stdio.h&
$incude %conio.h&
'oid main( )
*
int a+50,-n- i- .- temp /
crscr( ) /
print0 ( 12ow 3any 4um#ers 5) /
scan0(56d7-8n)/
print0 ( 19nter the 9ements:n1) /
0or ( i ; 0 / i %; < / i== )
scan0(56d7-8a+i,)/
0or ( i ; 0 / i % n>1 / i== )
*
0or ( . ; i = 1 / . % n>1 / .== )
*
i0 ( a+i, & a+., )
*
temp ; a+i, /
a+i, ; a+., /
a+., ; temp /
?
?
?
print0 ( 1:n:nArray a0ter sorting::n1) /
0or ( i ; 0 / i % n>1 / i== )
print0 ( 16d:t1- a+i, ) /
getch( ) /
?
Sorting Techniques
/! Seection Sort !/
$incude%stdio.h&
'oid main()
*
int a+100,- n- i- .- max- temp/

crscr()/

print0(12ow many 1)/
scan0(16d1-8n)/

print0(19nter 4um#ers :n1)/
0or( i ; 0/ i % n/ i== )
scan0(16d1- 8a+i,)/
0or (i ; 0/ i %; n>1/ i==)
*
max ; i/
0or( . ; i = 1/ . % n > 1/ .==)
i0(a+., % a+max,)
max ; ./
temp ; a+i,/
a+i, ; a+max,/
a+max, ; temp/
?
print0(1Sorted 4um#ers :n1)/
0or( i ; 0/ i % n/ i==)
print0(16d:n1-a+i,)/
getch()/
?
Sorting Techniques
/! @nsertion sort. !/
$incude %stdio.h&
$incude %conio.h&
'oid main( )
*
int a+100,-n- i- .- B- temp /
crscr( ) /
print0(12ow many 1)/
scan0(16d1-8n)/

print0(19nter 4um#ers :n1)/
0or(i;0/i%n/i==)
scan0(16d1-8a+i,)/
0or ( i ; 1 / i %;n>1 / i== )
*
0or ( . ; 0 / . % i / .== )
*
i0 ( a+., & a+i, )
*
temp ; a+., /
a+., ; a+i, /
0or ( B ; i / B & . / B>> )
a+B, ; a+B > 1, /
a+B = 1, ; temp /
?
?
?
print0 ( 1:n:nArray a0ter sorting::n1) /
0or ( i ; 0 / i %; n>1 / i== )
print0 ( 16d:n1- a+i, ) /
getch( ) /
?
Sorting Techniques
/! @nsertion Sort !/
$incude%stdio.h&
'oid insertionSort(int a+,-int n)
*
int .- p- temp//
0or( . ; p/ p % n/ p==)
*
temp ; a+p,/
0or( . ; p/ . & 0 88 temp % a+ . > 1 ,/ .>>)
a+ . , ; a+ . > 1 ,/
a+ . , ; temp/
?
?
'oid main()
*
int a+100,- n- i/
crscr()/
print0(12ow 3any eements : 1)/
scan0(16d1-8n)/
print0(19nter the 9ements :n1)/
0or( i ; 0/ i % n/ i== )
scan0( 16d1- 8a+i, )/
insertionSort( a- n )/
print0(1Sorted 9ements are:n1)/
0or( i ; 0/ i % n/ i== )
print0( 16d:n1- a+i, )/
?
Sorting Techniques
/! CuicB Sort !/
$incude%stdio.h&
'oid quicB(int a+,-int #-int u#)
*

int Bey- i- .- temp- 0ag/
i0( # % u# )
*
Bey ;a+#,/
i ; #/
. ; u#=1/
0ag ; 1/
whie( 0ag ;; 1 )
*
i==/
whie( a+i, % Bey )
i==/
.>>/
whie( a+., & Bey )
.>>/
i0( i % . )
*
temp ; a+i,/
a+i, ; a+.,/
a+., ; temp/
?
ese
0ag ; 0/
?
temp ; a+#,/
a+#, ; a+.,/
a+., ; temp/
quicB(a-#-.>1)/
quicB(a-.=1-u#)/
?
?
Sorting Techniques
'oid main()
*
int a+100,- n- i/
crscr()/
print0(12ow many 1)/
scan0(16d1-8n)/
print0(19nter 4um#ers :n1)/
0or( i ; 0/ i % n/ i== )
scan0(16d1-8a+i,)/

quicB(a- 0- n>1)/

print0(1Sorted 4um#ers :n1)/
0or( i ; 0/ i % n/ i== )
print0(16d:n1-a+i,)/
?
Sorting Techniques
/! 2eap Sort. !/
$incude %stdio.h&
$incude %conio.h&
'oid 2eap ( int a+ ,- int n)/
'oid Ad.ust ( int a+ ,- int i- int n) /
'oid main( )
*
int p+100,- n- i - count/
crscr( ) /
print0(12ow many 1)/
scan0(16d1-8count)/
print0(19nter 4um#ers :n1)/
0or( i ; 0/ i % count/ i== )
scan0(16d1-8p+i,)/
2eap ( p- ( count > 1 ) ) /
print0 ( 1Sorted 9ements :n1 ) /
0or ( i ; 0 / i %; count > 1 / i== )
print0 ( 16d:t1- p+i, ) /
getch( )/
?
'oid 2eap ( int a+ ,- int n )
*
int i- temp /
0or ( i ; n / D / i &; 0 / i>> )
Ad.ust( a - i - n)/
0or ( i ; ( n > 1 ) / i &; 0 / i>> )
*
temp ; a+ i = 1 ,/
a+ i = 1 , ; a+ 0 ,/
a+ 0 , ; temp/
Ad.ust(a - 0 - i)/
?
?
Sorting Techniques
'oid Ad.ust ( int a+ ,- int i - int n )
*
int .- B- r- 0ag /
0ag ; 0/
r ; B ; a+ i ,/
. ; D ! i/
whie ( (. %; n) 88 (0ag ;; 0) )
*
i0 ( (. % n) 88 (a+ . , % a+ . = 1, ) )
.== /
i0 ( B &;a+ . , )
0ag ; 1 /
ese
*
a+ . / D , ; a+., /
. ; D ! . /
?
?
a+ . / D , ; r /
?
Sorting Techniques
/! 2eap Sort. Ei00erent 3ethod!/
$incude %stdio.h&
$incude %conio.h&
'oid maBeheap ( int + ,- int ) /
'oid heapsort ( int + ,- int ) /
'oid main( )
*
int arr+100,- n- i /
crscr( ) /
print0(12ow many 1)/
scan0(16d1-8n)/
print0(19nter 4um#ers :n1)/
0or( i ; 0/ i % n/ i== )
scan0(16d1-8a+i,)/
maBeheap ( a- n ) /
heapsort ( a- n ) /
print0 ( 1Sorted 9ements :n1 ) /
0or ( i ; 0 / i %; n>1 / i== )
print0 ( 16d:t1- a+i, ) /
getch( )/
?
'oid maBeheap ( int x+ ,- int n )
*
int i- 'a- s- 0 /
0or ( i ; 1 / i % n / i== )
*
'a ; x+i, /
s ; i /
0 ; ( s > 1 ) / D /
whie ( s & 0 88 x+0, % 'a )
*
x+s, ; x+0, /
s ; 0 /
0 ; ( s > 1 ) / D /
?
x+s, ; 'a /
?
Sorting Techniques
?
'oid heapsort ( int x+ ,- int n )
*
int i- s- 0- i'aue /
0or ( i ; n > 1 / i & 0 / i>> )
*
i'aue ; x+i, /
x+i, ; x+0, /
0 ; 0 /
i0 ( i ;; 1 )
s ; >1 /
ese
s ; 1 /
i0 ( i & D 88 x+D, & x+1, )
s ; D /
whie ( s &; 0 88 i'aue % x+s, )
*
x+0, ; x+s, /
0 ; s /
s ; D ! 0 = 1 /
i0 ( s = 1 %; i > 1 88 x+s, % x+s = 1, )
s== /
i0 ( s & i > 1 )
s ; >1 /
?
x+0, ; i'aue /
?
?
Sorting Techniques
/! F29GH10.F: 3erge Sort. !/
$incude %stdio.h&
$incude %conio.h&
'oid main( )
*
int a+5, ; * 11- D- 9- 13- 5I ? /
int #+5, ; * D5- 1I- 1- 90- 3 ? /
int c+10, /
int i- .- B- temp /
crscr( ) /
print0 ( 13erge sort.:n1 ) /
print0 ( 1:nJirst array::n1 ) /
0or ( i ; 0 / i %; < / i== )
print0 ( 16d:t1- a+i, ) /
print0 ( 1:n:nSecond array::n1 ) /
0or ( i ; 0 / i %; < / i== )
print0 ( 16d:t1- #+i, ) /
0or ( i ; 0 / i %; 3 / i== )
*
0or ( . ; i = 1 / . %; < / .== )
*
i0 ( a+i, & a+., )
*
temp ; a+i, /
a+i, ; a+., /
a+., ; temp /
?
i0 ( #+i, & #+., )
*
temp ; #+i, /
#+i, ; #+., /
#+., ; temp /
?
?
?
0or ( i ; . ; B ; 0 / i %; 9 / )
*
i0 ( a+., %; #+B, )
c+i==, ; a+.==, /
ese
c+i==, ; #+B==, /
Sorting Techniques
i0 ( . ;; 5 KK B ;; 5 )
#reaB /
?
0or ( / . %; < / )
c+i==, ; a+.==, /
0or ( / B %; < / )
c+i==, ; #+B==, /
print0 ( 1:n:nArray a0ter sorting::n1) /
0or ( i ; 0 / i %; 9 / i== )
print0 ( 16d:t1- c+i, ) /
getch( ) /
?
Sorting Techniques
/!! 3ergeSort
! @nterna 3ethod that merges two sorted ha'es o0 su#array
! a is an array o0 num#ers
! # is an array to pace the merged resut
! e0tGos is the e0t>most index o0 the su#array
! e0t9nd is the 9nd o0 the 0irst ha0 su#array
! rightGos is the index o0 the start o0 the second ha0
! right9nd is the right>most index o0 the su#array
! tmpGos is the index o0 the temparary su#array L#L
! num9ements is the num#er o0 eements
! to #e merged into singe array
!/
$incude%stdio.h&
'oid 3erge(int a+,- int #+,- int e0tGos- int rightGos- int right9nd)
*
int e0t9nd-num9ements-tmpGos-i/
e0t9nd ; rightGos > 1/
tmpGos ; e0tGos/
num9ements ; right9nd > e0tGos = 1/
// 3ain Moop
whie( e0tGos %; e0t9nd 88 rightGos %; right9nd )
i0( a+ e0tGos , %; a+ rightGos , )
#+ tmpGos== , ; a+ e0tGos== ,/
ese
#+ tmpGos== , ; a+ rightGos== ,/
whie( e0tGos %; e0t9nd ) // Fopy rest o0 0irst ha0
#+ tmpGos== , ; a+ e0tGos== ,/
whie( rightGos %; right9nd ) // Fopy rest o0 second ha0
#+ tmpGos== , ; a+ rightGos== ,/
// Fopy # su#array #acB
0or( i ; 0/ i % num9ements/ i==- right9nd>>)
a+ right9nd , ; #+ right9nd ,/
?
Sorting Techniques
/!!
! @nterna 3ethod that maBes recursi'e cas
! a is an array o0 num#ers
! # is the temparary array to pace merged resuts
! e0t is the e0t>most index o0 the 0irst ha0 su#array
! right is the right>most index o0 the second ha0 su#array
!/
'oid 3ergeSort( int a+,- int e0t- int right )
*
int #+100,-center/
i0( e0t % right )
*
center ; ( e0t = right ) / D/
3ergeSort( a - e0t- center )/
3ergeSort( a- center = 1- right )/
3erge( a- #- e0t- center = 1- right )/
?
?
'oid main()
*
int a+100,- n- i/
crscr()/
print0(12ow 3any eements : 1)/
scan0(16d1-8n)/
print0(19nter the 9ements :n1)/
0or( i ; 0/ i % n/ i== )
scan0( 16d1- 8a+i, )/
3ergeSort( a- 0- n > 1 )/
print0(1Sorted 9ements are:n1)/
0or( i ; 0/ i % n/ i== )
print0( 16d:n1- a+i, )/
?
Sorting Techniques
Hadix Sort
$incude %stdio.h&
$de0ine 3AN 5
$de0ine S2OPGASS
'oid print(int !a-int n)
*
int i/
0or(i;0/i%n/i==)
print0(16d:t1-a+i,)/
?
'oid radixsort(int !a-int n)
*
int i-#+3AN,-m;0-exp;1/
0or(i;0/i%n/i==)
*
i0(a+i,&m)
m;a+i,/
?

whie(m/exp&0)
*
int #ucBet+10,;*0?/
0or(i;0/i%n/i==)
#ucBet+a+i,/exp610,==/
0or(i;1/i%10/i==)
#ucBet+i,=;#ucBet+i>1,/
0or(i;n>1/i&;0/i>>)
#+>>#ucBet+a+i,/exp610,,;a+i,/
0or(i;0/i%n/i==)
a+i,;#+i,/
exp!;10/

$i0de0 S2OPGASS
print0(1:nGASS : 1)/
print(a-n)/
$endi0
?
?

'oid main()
Sorting Techniques
*
int arr+3AN,/
int i-n/

print0(19nter tota eements (n % 6d) : 1-3AN)/
scan0(16d1-8n)/

print0(19nter 6d 9ements : 1-n)/
0or(i;0/i%n/i==)
scan0(16d1-8arr+i,)/


print0(1:nAHHAQ : 1)/
print(8arr+0,-n)/

radixsort(8arr+0,-n)/

print0(1:nSOHT9E : 1)/
print(8arr+0,-n)/
print0(1:n1)/

?
9xchange Sorting
The second class of sorting algorithm that we consider comprises algorithms that sort by exchanging
pairs of items until the sequence is sorted. In general, an algorithm may exchange adjacent elements
as well as widely separated ones.
In fact, since the insertion sorts considered in the preceding section accomplish the insertion by
swapping adjacent elements, insertion sorting can be considered as a kind of exchange sort. The
reason for creating a separate category for insertion sorts is that the essence of those algorithms is
insertion into a sorted list. On the other hand, an exchange sort does not necessarily make use of such
a sorted list.
Bubble ort
!uicksort
"unning Time #nalysis
#$erage "unning Time
electing the %i$ot

Você também pode gostar