0% found this document useful (0 votes)
7 views12 pages

DSA-08-Sorting_Algorithms

The document outlines various sorting algorithms, including Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort, and Counting Sort, along with their time complexities. It also briefly discusses searching techniques for sorted and unsorted arrays, specifically Binary Search and Sequential Search. The content is intended for an undergraduate course on Data Structures and Algorithms at the University of Tehran.

Uploaded by

almudharisaleh8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

DSA-08-Sorting_Algorithms

The document outlines various sorting algorithms, including Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort, and Counting Sort, along with their time complexities. It also briefly discusses searching techniques for sorted and unsorted arrays, specifically Binary Search and Sequential Search. The content is intended for an undergraduate course on Data Structures and Algorithms at the University of Tehran.

Uploaded by

almudharisaleh8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Data Structures and Algorithms

Mohammad GANJTABESH

Department of Computer Science,


School of Mathematics, Statistics and Computer Science,
University of Tehran,
Tehran, Iran.

[email protected]

Dept. of Computer Science


University of Tehran
Sorting Algorithms

Data Structures and Algorithms


Undergraduate course

Mohammad GANJTABESH Dept. of Computer Science


[email protected] University of Tehran
Motivations
I # iii.
:# % oh
-

A- # HE


um

Sort
Alg .

*, -7 i
i-D
_

ATF Tix
(sorted ✓
I ± :S.
6,6¥

1/8
xD
< Gcs !
Bubble Sort =

Bubble sort
ten

for @

( Ain) }


n

o to n -
2) {
1_✓É¥↑j+ % ,

for ( j ← o
to ten -

2) {
if ( A "] > A "+☐){
] Acjti])
swap (Acj
,

}
}
ten ← ten -
1 Time complexity = (n )
}
}
2/8
Selection Sort Ñ
Selection sort ( Ain ) { AIÉ7É
for Lito to n -

2) { index _
min = 2

i min i
_

swap ( Aco ] ,
AG
:nᵈ%mi④
for ( j it , to n
1) I

(
-

if ( Aaj ] < A- [ i _
min ] )t
After first pass
i. min ← j
t.FI
}
} I :-,
swap ( 1- [ ]
i Ali _
min]
)
1
,

, Time Complexity = Off)


3/8
?⃝
Insertion Sort →
-

Insertion sort ( Ain ) }


for ( i ← 1 to n -
1) {
t→
a key

key ← A [i ] 1--1151714012
Pos ← i

while ( pas Yo & A [ pas ]>


I
key)
-

,
A- [ pas] ← A [ Pos -

i
]
Best case
Pos ← pos
!#
-

l :
it
} .
# Agm

}
Alpes ] ← key w.rse.com?F-%
i.
E- y.IM ,
#
#
}
-

O(n4 4/8
Mengesortc.A.si e) { Merge Sort =G_
if(s< e) {
m←
Mengesort
e% ]
( Asim )
IÉÉ
{ §
Mengesort ( Amelie )
recursive
Merge ( A ,s,m e) , approach

¥÷É '
"" ←
tF***D**

|
A[ Sim ]
[←
R← A- [ m -11 , e) }
j←j+i
%
i k←kH
€-2131415161719
← 0
for while
j ← °
} ,/

Keo whitefish){ whihelj CIRI){


j< IRI){

}
while (i< 14 & A[k3←L[ i ]
'
A[k3←Rej]
" ← it '
if ( Lei ] ≤ R[j] } j←j+ ,
" ← K+ '

ACK] ← Lci] } g
Kike ,

} i ← it / 1311 for merge 5/8


Merge Sort ( Analysis) ?
-
É n - tests # Tjb II
}
◦ 4) if
Tcn) =
n= ,

ztcnk) +
° " "

g≈ ✓,,,
t.IE?nTn--iL
4kW r%
,

4¥ #
.

" eh -1
- i

2¥ { =

2T (2k
"

172k) ≤ ) + c.
× - &

2k
[
-

( 2k 2) 2k
-

≤ 2 2T +
c. + c. _

= IT ( 2K -2 ) + 2. c. 2K (Master theorems is -4

2K
22 [ 2T( zk 3) zk 2) + 2C
-


-

+ c.

= 23T (2k 3) -

3. c.
2k
+
:
2k 2k '
k 2k 0(n log!)
€-22k
.
c. =

(1) k c + .

c. =
1- +
-

5/8
if

}
04) n= ,

Tcn )
Quick Sort #£n
=

?
e) {
s
Quick sort (A) s e

Ht
,

A
if (s< e) {
m ← Partition ( A. sie ) { Partition

Quick sort ( A. Sim


Quick Sort ( Aim -11 , e)
-1 )
1÷ÉÑ"
}
} { Quick

sort
{fʰ
complexity :

AtT
Time

Best Tent
: =

217%1+047--0 (
worse : Tcu ) = to) -11-4-11+041 ntogn )
=
.

0 ( ne)

#"ᵗᵈÑ median
based
Average : 0 ( n
logan ) ✓ select
on

Algorithm 6/8
Quick Sort

Partition ( Ais
µ based pivot
, e)

{
first element
Aˢ=gj↑

on

✗ ← ACS] inns ← j
i←s+'b 0¥ .
j¥¥
_ j←e sci-Fi .
* ¥9M
repeat {
while ( Ali ] ( x ) i-iii

while ( Ag ] > xD j←j -1

if(i<j){
Time complexity :

Ali] ) (n )
swap ( Asi] ,

3wntikl.is 's ) ✓
swap ( Acs] , Acj ))
return ( j )
} 6/8
◦ ≤ I <k Counting Sort •

*
=
9k¥ n

tÉ5*
=
A

c.at?ings%casY-c----iii-
{
of ten K
KC is array
.

an

farci ← ◦ to K ) c[i7←° 1É¥¥


for (¥ ← o to n ) 1/0 2
4165948-7
c[A[j ]] -1-1
forties to K) BÉ234ÉÉ5
c[ i ] ← Cci] -11:-D
for (j ← ◦ to n ) } Time complexity
B. [ C [ Acj ]] -

I ] ← ACE ] ;

0( ntk
}
3 C. [ Acj ] ] -
-

7/8
Searching: Sorted vs. Unsorted Arrays

Search
M

Binary Search Sequential

)

Alto ,
±
Afia
Binary Search ( Aisle
2) { Search (Annex
/
sequential
m ← + e) 12 ]
for G' ← ◦ to n -

D}
netwmcm )
ifeng.se, if ( Aci ] = x )
Else if ( x < Acm]) return ti )
return
(Binary search ftp.m-KD }
else return ( -11
return (
Binary search ( Auntie xD ,
}
}
Time complexity → Ollogzh )
Time Complexity :O ( n)
:*
8/8

You might also like