0% found this document useful (0 votes)
80 views15 pages

Data Structure & Algorithms: Sunbeam Infotech

This document provides information about three different sorting algorithms: Bubble Sort, Improved Bubble Sort, and Insertion Sort. It explains the basic steps of each algorithm through pseudocode and examples. Bubble Sort has a time complexity of O(n^2) as it iterates through the list multiple times. Improved Bubble Sort optimizes Bubble Sort by adding a flag to check if a swap occurred, breaking out of the inner loop early if not. Insertion Sort iterates through the list and inserts each element into the sorted portion, having a time complexity of O(n^2) in the worst case but O(n) if the list is nearly sorted.

Uploaded by

kamala thakur
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)
80 views15 pages

Data Structure & Algorithms: Sunbeam Infotech

This document provides information about three different sorting algorithms: Bubble Sort, Improved Bubble Sort, and Insertion Sort. It explains the basic steps of each algorithm through pseudocode and examples. Bubble Sort has a time complexity of O(n^2) as it iterates through the list multiple times. Improved Bubble Sort optimizes Bubble Sort by adding a flag to check if a swap occurred, breaking out of the inner loop early if not. Insertion Sort iterates through the list and inserts each element into the sorted portion, having a time complexity of O(n^2) in the worst case but O(n) if the list is nearly sorted.

Uploaded by

kamala thakur
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/ 15

Data Structure & Algorithms

Sunbeam Infotech

Sunbeam Infotech www.sunbeaminfo.com


Bubble Sort 6 4 28 3 I

ite) {
ian i
;
forcing
-

0 1 2 3 4 5
fooCj=o ; jam -
I;jtt) {
D) I 2 3 4 6 8
iffacj ) > a

D) I
4 9 swap ( ACD
a

innermost
,

)
j jtl i=o : 5 its

} Daca D
i=i : 5 its
i=Cn ④
-
-

§→
is : sites
T N2-2MT I
4 i =3 : 5 its

i=4 : sites
T4
'
n
-

@ -
1) camp

Sunbeam Infotech www.sunbeaminfo.com


Bubble Sort -
improved Bubble Sort

ite) {
ich i
;
foCi=o ;
-

0 1 2 3 4 5
ijjtt) {
fooCj=o;jcm -
I -

ifCacFFaCjtD ) 2 3 I 4 6 8
D) -

swapcacjs
f f
a
,

inner
3
loop
3 i-k-D-cm-DT.e.tl -

ix. : 5
'
Th m n
i. I• :
4
+150
-

i=2
.

:3
i=z : 2
-1=4 : I

Sunbeam Infotech www.sunbeaminfo.com


Bubble Sort -
more improved Bubble Sort

is n i
;
it e) {
forcing
-

0 1 2 3 4 5
Flag false
Swap
=
;
{ 2 4 6 8
for a
;jcm -
I -
ijjtt) I 3

ifCaEjTFa D) E

( a CD a
D) -
f f
swap ,

swap tone :
flag -_

Best cage :
array already
g mmmm sorted .

If wap Flag == false) a


inner loop is executed only
break ; once .

i= n I
3
-

Th n - I
0④
Sunbeam Infotech www.sunbeaminfo.com
Insertion Sort 642831

68 2
I 3 4
0 1 2 3 4 5
~
I 2
a
3 46 8
④ 283 I

9 i=i

-1=2 246
- i=2

S
2468
-1¥ I 2 3 4 i=3

'
23 4 68 23468
^
i=4
8
9 I 234 6
I i=5

9
Sunbeam Infotech www.sunbeaminfo.com
Insertion Sort
is Itzt . .
-
+ G-D
is non D12 - 0 1 2 3 4 5

6 4 28 3 I

7LM 2- m
1 4 6

2
I
⑤ +3 276 ?⑤
4
2%6 ?①
F34 6EO
5 r -

Sunbeam Infotech www.sunbeaminfo.com


Insertion Sort

0 1 2 3 4 5

I 23 4 68

/
Tan
BEI OCD

Sunbeam Infotech www.sunbeaminfo.com


Linked List REFEREES
at runtime
• Linked List is list of items linked together. → can grow / shrink .

sequential access only


• Each item in linked list is called as Node.
.


mmm

overheads
• Each node contains data and →

alloc
mmm

pointer/reference
F
to the-
next node. →
not contiguous
.

• Linked list is linear data structure.

Arrays • Linked list ADT


-

OCD
• addFirst() ✓

① random access →

• addLast() ✓

② fixed size
Ishoink of runtime .
• addAtPos() ✓
cannot grow

-

deleteFirst() ✓

③ contiguous allocation
• deleteLast() ,

overheads • deleteAsPos()
④ no
required

extra space
no
• deleteAll()
-


data elements
other than .

• traverse C) or display C)

Sunbeam Infotech www.sunbeaminfo.com


Linked List
head

• There four types of linked list.


-
③ mode mode mode

• Singly linear linked list - >


d
11
n

20-0
d
223-00
n
d
33
n

q
• Singly circular linked list LEE IE9 3IM
• Doubly linear linked list
-
head

\
-

100

• Doubly circular linked list

\
11 200 22 300 33 I

1002003007ft
y

Class My
list
{
static member
Hoda head ; class head
data { >
d n d n p d n
p
class Mode {
I ' '
20-0 E0223Y 33 I
,
display C)

{
data ,

ops{
type
ad ELI
date
mode next ;
nea '¥ ¥ son

100

{
=
0ps
¥rf
ioa✓
>
300 33
=
22300¥00
11 200 100 100
, ,
-

3; 3 z

Sunbeam Infotech www.sunbeaminfo.com


Linked List -

display

head head tear = head ;





④ 350 ⑤ %

q÷÷i
-

, →•

*
-

train =
trav.net ;

Sunbeam Infotech www.sunbeaminfo.com


Linked List -

add Last C)

head not
100
L
11 200 22300 33 4×57
mum
441 @

IO0 2OQ 300 ↳ QQ


inn

4tour
-
& init it

sina.IE?jde./DFi?wnikfhI iI n'
nude list
empty ifinead - uD
.

① create new

Mode nn= new Mode my


head -_nn ,

② in
:[
node into next of last node

③ add new .
goo
next
-

train . = nm ;

Sunbeam Infotech www.sunbeaminfo.com


Linked List -
add Firste )
nm

③ head

Fido
44
If QQ 11 200 22 300 33 0

↳ QQ IO0 2OQ 300

ahlocdinitnewnodemoden -neIII.
wtlodelraDi@nenwn .gh

¥0
emnnndeaad

node
③ head should be new .

400
head

Sunbeam Infotech www.sunbeaminfo.com


add Atposc ) before
Linked List -
make break

spd :3 odd node beyond pos .

9
head
100
2 3
5
I

11 200 22 300 33
④0 X 7 44 0


¥aIH#④$
' oo z oo ggg 900

g
Stand:[I
↳ ¥
.

I 05¥
③ nn
Sunbeam Infotech www.sunbeaminfo.com
Linked List

HI;D
,
} 3

iqa
" " "°

¥°z ,
33 0

need

↳ 11 → 22 → 33
µT*a
→ 44 → ST
]
9 f
temp tour

Sunbeam Infotech www.sunbeaminfo.com


Thank you!
Nilesh Ghule <[email protected]>

Sunbeam Infotech www.sunbeaminfo.com

You might also like