Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

OBJECT-ORIENTED

PROGRAMMING
Fall- 2020

Dr. Sadaqat Jan


sadaqat@uetmardan.edu.pk

Standard Template Library

University of Engineering
Mardan & Technology
Department of Computer Software Engineering
• Selection sort
is a simple sorting algorithm in which the list is divided into two parts,
the sorted part at the left end and the unsorted part at the right end.
Initially, the sorted part is empty and the unsorted part is the entire list.
• The smallest element is selected from the unsorted array and swapped
with the leftmost element, and that element becomes a part of the
sorted array. This process continues moving unsorted array boundaries
by one element to the right.
Bubble Sort Algorithm
• Bubble Sort is an elementary sorting algorithm, which works by repeatedly exchanging adjacent elements,
if necessary. When no exchanges are required, the file is sorted.
• We assume list is an array of n elements. We further assume that swap function swaps the values of the
given array elements.
• Step 1 − Check if the first element in the input array is greater than the next element in the array.
• Step 2 − If it is greater, swap the two elements; otherwise move the pointer forward in the array.
• Step 3 − Repeat Step 2 until we reach the end of the array.
• Step 4 − Check if the elements are sorted; if not, repeat the same process (Step 1 to Step 3) from the last
element of the array to the first.
• Step 5 − The final output achieved is the sorted array.
• Insertion Sort Algorithm

• Step 1 − If it is the first element, it is already sorted. return 1;


• Step 2 − Pick next element
• Step 3 − Compare with all elements in the sorted sub-list
• Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted
• Step 5 − Insert the value
• Step 6 − Repeat until list is sorted
Standard Template Library

Standard Template Library (STL) mainly consists of three components which are mentioned
below:

1) Containers
Containers are class templates which organizes stored data in memory. There
are various types of container class available like Array, Stack, Vector, List,
Queue etc.

2) Algorithms
Algorithms are various processes that act on containers. These processes
include different sortings, copy, search, merge, modify etc.

3) Iterators
Iterators are a generalization of the concept of pointers: they point to elements
in a container. It can perform the operations like begin, end, next, prev etc.
Standard Template Library

Containers

(Sequence Containers)

a) vector container: Relocating, expandable array, Quick


random access by index number

b) list container: Doubly linked list, insertions

c) Deque: Like vector, but can be accessed at either


end, addressing LIFO FIFO problem, double
ended queue.
Standard Template Library

Containers

(Associative Containers)
Associative Containers use keys to access data. Keys are numbers assigned by containers.

a) Set: Stores only the key objects Only one key of each value allowed
b) Multiset: Stores only the key objects Multiple key values allowed
c) Map: Associates key object with value object Only one key of each value allowed
d) multimap Associates key object with value object Multiple key values allowed
Standard Template Library

Container functions

a) size() Returns the number of items in the container


b) empty() Returns true if container is empty
c) max_size() Returns size of the largest possible container
d) begin() Returns an iterator to the start of the container, for iterating forwards through the container
e) end() Returns an iterator to the past-the-end location in the container, used to end forward iteration
f) rbegin() Returns a reverse iterator to the end of the container, for iterating backward through the container
g) rend() Returns a reverse iterator to the beginning of the container; used to end backward iteration
Standard Template Library

Container Adapters

(Special-purpose containers have simpler interfaces than the more general containers)

a) Stack: Insert (push) and remove (pop) at one end only


b) Queue: Insert (push) at one end, remove (pop) at other
c) Priority queue: Insert (push) in random order at one end, remove (pop) in sorted
order from other end
Standard Template Library

Algorithms

a) find: Returns first element equivalent to a specified value


b) count: Counts the number of elements that have a specified value
c) equal: Compares the contents of two containers and returns true if all
corresponding elements are equal
d) search: Looks for a sequence of values in one container that corresponds with
the same sequence in another container
e) copy: Copies a sequence of values from one container to another (or to a
different location in the same container)
Standard Template Library

Algorithms cont…

f) swap: Exchanges a value in one location with a value in another


g) iter_swap: Exchanges a sequence of values in one location with a sequence of
values in another location fill Copies a value into a sequence of locations
h) sort: Sorts the values in a container according to a specified ordering
i) merge: Combines two sorted ranges of elements to make a larger sorted range
j) accumulate: Returns the sum of the elements in a given range
k) for_each: Executes a specified function for each element in the container
Standard Template Library

Iterators
Similar to pointers, iterators are entities that are used to access individual data items (elements) in a container.

a) Random access: Read and write

b) Bidirectional: Read and write

c) Forward: Read and write

d) Output: Write only

e) Input: Read only

You might also like