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

Sorting

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 views19 pages

Sorting

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/ 19

DATA STRUCTURE AND ALGORITHMS,LEC-6

TOPICS COVERED:
1.Concepts of Sorting

E
2. Inplace Sorting and Stable Sorting
3.Insertion Sort
4.Selection Sort
5.Time and Space Complexity Analysis

the elements in a particular


I Sorting arranging
older or sequence
Need
restructurethe
array
ascending smallest to largest
smallest
descending largestto

Implacesouting
2 Stablesouting
7 9,6
2,4 gl
5 6 7,9
2,5 stable sorting
In place sorting
which maintains the
It is an algorithm
Deff records with the sameKeysor
relative order of
Values
Sort Bubblesort
En Insertion sort Merge
Iflhn.FI
records with
n tain the relative
same keys or
order
values
of

1 5 2,3 3,3 3,7 6


Ex
I sorting 23910
5,6 7
1 2 3,3 3,3

Ens Selection sort


Insertionsort
each element
It worksiteratively inserting
an unsorted list into its correct
position in
of the list
a souted portion of
1
SIIart with second element ofthearray
in the
and assume first element
is sorted
array element with the first
Comparesecond
the second element is smaller
element if
then swap them
to third element and compare it
More
two element and putatitscorrect
with first
position
Repeat until the is sorted
array
Viswalisation

DII.III.ru.EE
Firstiterativy
11 23
512 10

seconditeration

thinitivation
2
y
u

fourthitration

int aurl int n 4 23


void insertionsort

i it
d
for intis n

over i j
Swap vint key
int j it

5 0 n 2 while 57 0 I WISH
For it arr i
i j j 1
3
I am Litt key
are 1 ey
3
Dryrus
5 2
3
Initial hanad
123 414115 4
firsettf.tk y
j Oam 0J71Erue
we 1 23
5 1 while false

arr o 1
2 3 9

Trio
Inayat
10
Key
jun
1T i is a ample 10
23710
I nyO arr 23 23
0 0
arr10371
am jtD
10 1I51023key
2 2
Ek4

work 5
are B 001 2

5 1
1
arr 2 earn
5 0 e

arr r 5
I S

1251023 key 2
1144k
5 3 any j key
arr 9 or 3

5 2 1072
wor 2
arr 3

5 1 5 2
am 2 world
5 0
172
arr D key

HII
Natement N 1 pass

Tec 0 my
Best case is already sorted
Array n

Arg cases randomly orderedn


worst cases reverse sortedorder 0 n

OFFIE
that
567 9106
you
semplity
AFIIDo
and dry run on insertion sort
show the output each pass
after
10 6
9 2

elements is smalle
number
of
incremental approach

Minimum time O
Already sorted
n

Manimum time 0 m
reverse sorted
comparisonstTIIigalgonium
It souts an
array by repeatedly
the smallest element from
selecting
unsorted portion and swapping
the
it with first unsorted position

Ef in
currentVmin min element
element

element

snap
111
N
122169
current
3
element spinelement
69
111112 25122 1
current
element

a.ME A
ET min element
current
element min
5h11 of
element

Mtg current
element

61 1 1 09
sorted Array

T.PE we find the smallestelement


and swap it with first element
currentelement
Now smallest element is in
my correct order
2 We find the smallest among
element and swap it
remaining
second element
with
all elements
3 Untill we get correctposition
moved to

Functionforselectionorts
int n

Sort int coor


void Selection
o KI 1 it
for int our pos holds themminum
that
1 Assumtion
Ije
cumer
int min idn
In for intj itsisnjjt.DK
min indan
i arr
if Carr
min inden j
33
int temp way i
i min indent
arr arr

I T temp
are Tin inden temp

3
3
T C O n

S C

It is not a stable sortingalgorithm

Prove
small datasets
Bubblesot It works
by repeatedlysnapping
the adjacent elements ifthey are in
wrong
order
1stiteration 1st
largestelement 8
in its correct at
position ynZ
2ⁿᵈ iteration 2nd largestelement LZn
in its correct

YTve sort the array usingmultiple passes After


the first pass the manimum element goestoend
In secondpass the second largest element
will move to second last position
In pass we processonly those elements
2 every
that have already notmoved to correctposition
After K passes the largest K elements must have
been movedto the last k positions
3 In a pass we consider remaining elements
and compare all adjacent and snoop if
element is beforesmaller element
larger
ialaton
t.to 3
it 51 16
3
i 2
1511131.6T
171 57 3 6

3 6
it 1 5

i 2 111315T
Namond
3rᵈP1 P3 5 6

p 1 4 200
1 A

voidswerp int p int g


int temp p
p q
q temp

Swap without using thirdvarial


20
1
208s
intn
void bubblesort int worl
int j
book swapped Dots

forfanti 0 in 1 i t

snapped false OF
force.j 0 5 2 1 150 1
if arr i arr iti h
soap arr s avr i D
true
hopped
3
3
if snapped fake
break
3
3 T.CI O n

S C o 1

based
comparison sorting
algorithm
Best Case Time Complevity

FortdArraywon

Case Time compleuity


Average

Tama old

time compleuity
Worst case

JortdainRevergeorder
0
n2
String and string operations

1 dataypefstring.length1
Chaols.trf Helloworld Mulharat

4 41110
01 239
7111
1 56789 10

stolen Str 10
stfegthdofa.tl
15 string

You might also like