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

c02 s1 Collections Solid

Uploaded by

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

c02 s1 Collections Solid

Uploaded by

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

Time Complexity Examples

 O (1) Constant Time


 Eg: Finding the median value

{1, 3, 5, 7, 9, 11, 13}

Median value = array[length / 2] = array[3] =
7
 O (N) Linear Single Looping
 Eg: Finding the sum

{1, 3, 5, 7, 9, 11, 13}

You should read all values and sum them…
1
Time Complexity Examples
 O (logN) Binary Search
 Eg: Searching any item in sorted
list

2
Time Complexity Examples
 O (N^2) Bubble Sort

3
Time Complexity Notations

1- Big-O Notation (O-notation)


Worst Case
2- Omega Notation (Ω-notation)
Best Case
3- Theta Notation (Θ-notation)
Average Case
Searching in Array

Sample Array {9, 8, 10, 4, 2} N=5


Best: Search 9 -> Ω(1)
Worst: Search 2 -> O(N)
Average: Search 10 -> Θ(N/2) ->
Θ(N)

5
Why Big-O??
• There are 2 eggs.
• Eggs can be used again until it is broken.
• What is the minimum attempt count to find which
floor breaks an egg?

6
Why Big-O??
Algorithm 1:
•Start from floor 1.
•Drop egg.
•If it doesn’t break, go to upper floor.

•Best Case: If the answer is 1st floor: 1 attempt.


•Worst Case: If the answer is 100th floor: 100 attempts.
•Average Case: If the answer is 50th floor: 50 attempts.

•Just one trying per floor…


•Expected attempt count differs from 1 to 100…

7
Why Big-O??
Algorithm 2:
•Try middle floor.
•Drop egg.
•If it doesn’t break, try new middle floor.
•If it breaks, use algorithm 1 between last two attempts.

•Eg:
• Drop at 50th floor. -> NOT BROKEN
• Drop at 75th floor. -> BROKEN
• Use Algorithm 1 between 51-74 floor.

•Best Case: If the answer is 1st floor: 2 attempts. {50, 1}


•Worst Case: If the answer is 49th floor: 50 attempts. {50, 1,
2,..,49}
•Average Case: 19.10 ~= 19 attempts

8
•Expected attempt count differs from 2 to 50
Why Big-O??
Algorithm 3:
•Try {14, 27, 39, 50, 60, 69, 77, 84, 90, 95, 99} floors until
egg breaks.
•If it breaks, use algorithm 1 between last 2 attempts.

•Eg:
• Drop at 14th floor. -> NOT BROKEN
• Drop at 27th floor. -> NOT BROKEN
• Drop at 39th floor. -> BROKEN
• Use Algorithm 1 between 28-38 floor.

9
Why Big-O??
All possible solutions for algo-3

• Best Case: If the answer is 1th floor: 2 attempts. {14,


1}
• Worst Case: For almost any floor: 14 attempts.
• Average Case: 10.35 ~= 10 attempts.

10
Why Big-O??
Results of all algorithms.

11
Why Big-O??
Comparison

Almost all algorithms try to minimize the worst case!

Omega and Theta notations don’t show anything markable!

So, Big-O notation is used.

12
Collections

13
Read/Write Time
Complexity

Read Time: Big-O complexity of


retrieving item.
Write Time: Big-O complexity of
adding new element to the
collection.

14
Array

15
Linked List

16
Array vs Linked List

• If fast reading necessary, select array.


• If fast writing necessary, select linked list.

17
HashMap
• HashMap is combination of
Array and Linked List
• If the inner table is big
enough, read time can be
O(1)
• If the elements have same
hash number, reading and
writing may be O(N)

• If both reading and writing is


important, hashmap can be
selected.

18
Queue and Stack
• Queue and Stack is
derived from
LinkedList.
• Each element shows
the next one.
• Also, array based
implementations exist.

• Both reading and


writing if fast as O(1)
• However random
access is NOT
possible.

19
Tree
• Tree is enhanced version of
LinkedList. In linked list, each
element shows next. In tree,
each element may have
more than one pointer.
• For standard version, binary
tree, all nodes have to
pointers. The left one points
smaller nodes, the right one
points bigger nodes.
• There are also different tree
types based on children
count;
• Binary: has two children
per node
• Ternary tree: 3 children
• N-ary: random number
of children

20
Comparison of Collections

21
Demo Solid Project
You can download the whole project from google
drive.
Please extract it.
Open pom.xml file in IntellijIdea.
Then you can run SolidMain and NonSolidMain
classes.

22
Non Solid Example

23
Solid Example

24
Solid and NonSolid Output

They are same!

25
What is SOLID
SOLID is an acronym that stands for five key design principles.
The principles provide markable benefits to software.

Single responsibility principle


Open-closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle

26
Single responsibility
“A class should have one, and only one, reason to change.”

Each class should have one responsibility.

27
Open-closed
“You should be able to extend a class’s behavior without modifying
it.”

The code should be open for extension and close for modification.
While adding new features, just add new files. Don’t modify
existing files.

28
Liskov substitution
This is the most difficult to understand principle!
It is named for Barbara Liskov

“What is wanted here is something like the following substitution


property: if for each object O1 of type S there is an object O2 of
type T such that for all programs P defined in terms of T, the
behavior of P is unchanged when O1 is substituted for O2 then S is
a subtype of T.”

The point of the Liskov substitution principle is to ensure that


derived classes extend the base class without changing behavior.

29
Interface segregation
“Make fine grained interfaces that are client-specific. Clients
should not be forced to implement interfaces they do not use.”

The interface segregation principle for SOLID programming states


that it is better to have many smaller interfaces than a few bigger
ones.

30
Dependency inversion
“High level modules should not depend upon low level modules.
Both should depend on abstractions…abstractions should not
depend on details. Details should depend upon abstractions.”

You should “depend on abstractions, not on concretions” when


developing software

31
Why is it NonSolid?
Single responsibility principle: Employee class has 3
responsibilities.
1- It is data object. (DTO)
2- It has “work” logic.
3- It has “calculateSalary” logic.

32
Why is it NonSolid?
Open-closed principle: In order to add new department or to
change department behavior, Employee class has to be modified!
In solid version, changing ITEmployee class is enough.

33
Why is it NonSolid?
Liskov substitution principle: There is no inner interface for
Employee class.
Also sub classes must not change Employee behaviour.

34
Why is it NonSolid?
Liskov substitution principle 2nd example

Ostrich is a bird, but it can't fly.


Ostrich class is a subtype of class Bird, but it shouldn't be able to
use the fly method, that means we are breaking the LSP principle.

35
Why is it NonSolid?

36
Why is it NonSolid?
Interface segregation principle: There is no interface at all!

If there is only one interface with “work” and “calculateSalary”


methods, it would break the rule!

37
Why is it NonSolid?
Interface segregation principle 2nd example

In first example, there is only one big interface which contains 3


different behaviors. While adding a new tax calculation type, we
have to write empty methods for other three methods.
In the segregated version, there are 3 small interfaces. You can
easily add DatabasePersistor with only “save” and “load” methods.

38
Why is it NonSolid?
Dependency inversion principle: Employee class has work and
calculateSalary logic. All different flows should be separated
classes.
Employess class is dependent to department behaviors.

39

You might also like