You are on page 1of 36

ALGORITHMS

ALGORITHMS
What is an “Algorithm”?

• An algorithm is a step by step method of solving a


problem.
• Algorithm a set of steps to accomplish a task.
EXAMPLE : 01

Step 1: Bike to Station


EXAMPLE : 01

Step 1: Bike to Station

Step 2: Take train


EXAMPLE : 01

Step 1: Bike to Station

Step 2: Take train

Step 3: Walk to office


Example

How to Cook?
Designing an Algorithm
Problem
I’m hungry and I need to have maggi

Constraints
I have only 2 packets

Input
Ingredients

Output
Cooked maggi and I will be full

Solution
Cook and Eat maggi
Some operations that we do our day in day out with our data
are
• Search − Algorithm to search an item in a data structure.

• Sort − Algorithm to sort items in a certain order.

• Insert − Algorithm to insert item in a data structure.

• Update − Algorithm to update an existing item in a data structure.

• Delete − Algorithm to delete an existing item from a data


COMPUTER SCIENCE

Start with input data

Do complex Calculation

Stop when we find answer


A few Examples…

Let's talk about a famous algorithms.


EXAMPLE : 03

• How does Google


Hangouts transmit live
video across the Internet
so quickly?
Algorithm ArrayList
ARRAY LIST
Add() and remove() method:
Step1: Start

Step 2: Import package :java.util

Step 3: Declare Arraylist with parameter type in angular bracket

Step 4: Create object of Arraylist

Step 5: Add/Store the elements to the ArrayList

Step 6: Display elements of TreeSet

Step 7: call/invoke remove method to remove element at first index position

Step 8: Display the elements after removing

Step 9: Stop
ARRAY LIST
Iterator()
Step1: Start

Step 2: Import package:java.util

Step 3: Declare Arraylist with parameter type in angular bracket

Step 4: Create object of ArrayList

Step 5: Add/Store the elements to the ArrayList

Step 6: Declare the Iterator interface and create an object and call the iterator

Step 7: Using While loop, call the hasNext() to check the next element availability

Step 8: Print the elements in the traverse order

Step 9: Stop
ARRAY LIST
isEmpty() method
Step1: Start

Step 2: Import package:java.util

Step 3: Declare Arraylist with parameter type in angular bracket

Step 4: Create object for ArrayList

Step 5: Check whether the ArrayList is empty or not

Step 6: Call/invoke the empty() through the obj of ArrayList and print to check what it returns

Step 7: Add/Store the elements to the ArrayList

Step 8: Again Call/invoke the empty() through the obj of ArrayList and print to check what it returns

Step 9: Stop

Step 7: Stop
ARRAY LIST
ensureCapacity() method
Step 1: Start

Step 2: Import package:java.util

Step 3: Declare Arraylist with parameter type in angular bracket

Step 4: Create object for ArrayList and declare size

Step 5: Add/Store 4 elements to the ArrayList

Step 6: Using ensureCapacity() method increase the size of the ArrayList instance to hold more elements

Step 7: Add the needed elements

Step 8:Use for-each loop with declaring var of same type followed by obj of ArrayList for traversing

Step 9: Print the elements

Step 10: Stop


Algorithm Queue
Algorithm- offer() operation

Step 1: Start
Step 2: Import package:java.util
Step 3: Declare BlockingQueue with parameter type in angular bracket
Step 4: Create obj of BlockingQueue
Step 5: Insert elements to the BlockingQueue using offer() and check what it
returns(true/false)
Step 6: Display elements of Blocking Queue
Step 7: Try to add more elements and print and check what will return
Step 8: Stop
Algorithm- remove () operation

Step 1: Start
Step 2: Import package:java.util
Step 3: Declare Queue with parameter type with the help of LinkedList
Step 4: Create obj of Queue
Step 5: Insert elements to the Queue using offer()
Step 6: Display elements of Queue
Step 7: call / invoke remove method to delete an element from the head of
queue
//If it performs delete operation successfully, it returns the head element of the
queue. Otherwise it throws java.util.NoSuchElementException.

Step 8: Stop
Algorithm- poll() operation

Step 1: Start
Step 2: Import package:java.util
Step 3: Declare Queue with parameter type with the help of LinkedList
Step 4: Create obj of Queue
Step 5: Insert elements to the Queue using offer()
Step 6: Display elements of Queue
Step 7: call / invoke poll method to delete the element from the head of
queue
//If it performs delete operation successfully, it returns the head element of the
queue. Otherwise it returns “null” value.

Step 8: Stop
Algorithm- peek () operation

Step 1: Start
Step 2: Import package:java.util
Step 3: Declare Queue with parameter type with the help of LinkedList
Step 4: Create obj of Queue
Step 5: Insert/add elements to the Queue using add()
Step 6: Display elements of Queue
Step 7: Call / invoke peek method to retrieve the element(s) from the head of
Queue without removing
Step 8: Stop
Algorithm Tree
TREE SET
TREE SET
Add() method:
Step1: Start
Step 2: Import treeset package from library
Step 3: Declare TreeSet using its syntax and Create object of TreeSet
Step 4: Using add() method, Add/Store the elements to the TreeSet
Step 6: Display elements of TreeSet
Step 7: Elements will print in the Ascending order
Step 8: Stop
TREE SET
Iterator()
Step 1: Start

Step 2: Import all TreeSet package from library

Step 3: Declare Treeset using its syntax and Create object of TreeSet

Step 4: Add/Store the elements to the TreeSet

Step 5: Declare another object to print the elements in descending order which is of NavigableSet type, using
descendingSet() method

Step 6: Declare the iterator using its syntax and create an object for iterator

Step 7: Using While loop, call hasNext() to check the next element availability

Step 8: Elements will print in descending order

Step 9: Stop
TREE SET
pollLast() method
Step 1: Start
Step 2: Import all TreeSet package from library
Step 3: Declare Treeset with parameter type and Create object of TreeSet
Step 4: Add/Store the elements to the TreeSet
Step 5: Use pollLast() method to retrieve and print the highest/last element and remove
it
Step 6: Then print the rest of the elements
Step 7: Stop
TREE SET
pollFirst() method
Step 1: Start
Step 2: Import all TreeSet package from library
Step 3: Declare Treeset using its syntax and Create object of TreeSet
Step 4: Add/Store the elements to the TreeSet
Step 5: Use pollFirst() method to retrieve and print the lowest/First element and remove
it
Step 6: Then print the rest of the elements
Step 7: Stop
TREE SET
Sorted set
Step1: Start
Step 2: Import TreeSet package from library
Step 3: Declare TreeSet using its syntax and Create object for TreeSet of Sorted set
type
Step 4: Using add() method to add/Store the elements to the TreeSet
Step 5: Display elements of TreeSet in natural ordering
Step 6: Stop
TREE MAP
Algorithm- put() operation

Step 1: Start
Step 2: Import package:java.util
Step 3: Declare TreeMap with parameter type in angular bracket
Step 4: Create obj of TreeMap
Step 5: Add/store elements using put() to the TreeMap along with key value pairs for
Key-Value mapping
Step 6: Declare for-each loop for traversing
Step 7: Display elements of TreeMap using getKey() and getValue() to read Key-Value
mapping
Step 8: Stop
Algorithm- remove () operation

Step 1: Start
Step 2: Import package:java.util
Step 3: Declare TreeMap with parameter type in angular bracket
Step 4: Create obj of TreeMap
Step 5: Add/store elements to the TreeMap along with key value pairs for Key-Value
mapping
Step 6: call / invoke remove method to remove the element specifying key value
Step 7: Declare for-each loop for traversing
Step 8: Display elements of TreeMap using getKey() and getValue() to read Key-Value
mapping
Step 8: Stop
Algorithm- descendingMap, headMap, tailMap and subMap

Step 1: Start
Step 2: Import package:java.util
Step 3: Declare NavigableMap with parameter type in angular bracket
Step 4: Create obj of TreeMap
Step 5: Add/store elements using put() to the TreeMap along with key value
Step 6: call / invoke the descendingmap method to display the element in descending order
Step 7: call / invoke the headmap method to display the key-value pairs whose keys are less than or equal to
specified key
Step 8: call / invoke the method to display key-value pairs whose keys are greater than
or equal to specified key
Step 9: call / invoke the method to display key-value pairs whose keys range from fromKey
to toKey
Step 10: Stop
Algorithm- SortedMap

Step 1: Start
Step 2: Import package:java.util
Step 3: Declare SortedMap with parameter type with the help of TreeMap
Step 4: Create obj of SortedMap
Step 5: Add/store elements using put() to the TreeMap along with key value
Step 6: Call / invoke the headmap method to display the key-value pairs whose keys are strictly
less than the specified Key.
Step 7: Call / invoke the tailmap method to display key-value pairs whose keys are greater than
or equal to the specified Key.
Step 8: Call / invoke the submap method to display key-value pairs whose keys range from
fromKey(starting key), inclusive, to toKey, exclusive(last key).
Step 9: Stop
THANK YOU

You might also like