You are on page 1of 40

Data, information and data structure

• Data is one of the most powerful tools available to any business or organisation
that wants not only to survive but rise to the top in today’s competitive and
challenging world. The more information available, the more options and better
solutions to problems and obstacles open up.
• Data is information optimized for processing and movement, facts and figures stored on computers.
• Data structures are a specific way of organizing data in a specialized format on a computer so that the information can
be organized, processed, stored, and retrieved quickly and effectively. They are a means of handling information,
rendering the data for easy use.
• Data Structure is the systematic way used to organise the data. It helps efficiently organize, manage, and store data.
Data structures
classification Data & Information
• Data structures are broadly classified • Data refers to raw facts or figures. These facts could be
about people, locations, among other things. Data can
into two types: occur in various forms such as numbers, texts, and many
other forms.
• Linear Data Structures: Arrays,
stacks, queues, and linked lists to
organize data sequentially. A • Information can simply be said to be processed data that
has been given meaning. The data has been assigned
linear data structure is a structure in with a context to make it meaningful.
which the elements are stored
sequentially, and the elements are
connected to the previous and the
next element. As the elements are
stored sequentially, so they can be
traversed or accessed in a single run.
• Non-linear Data Structures: Trees
and graphs organize data
hierarchically or interconnectedly,
allowing for complex relationships
between data elements. In which the
data elements are not arranged in a
contiguous manner. As the
arrangement is nonsequential, so the
data elements cannot be traversed or
accessed in a single run.
Linear Data Structures Non-Linear Data Structures
• Array:- In an array, elements in • Graph:-In graph data structure,
memory are arranged in each node is called vertex and
continuous memory. All the each vertex is connected to other
elements of an array are of the vertices through edges.
same type. • Trees :- Similar to a graph, a tree
• Stack:-In stack data structure, is also a collection of vertices and
elements are stored in the LIFO edges. However, in tree data
principle. That is, the last element structure, there can only be one
stored in a stack will be removed edge between two vertices.
first.
• Queue:-Unlike stack, the queue
data structure works in the FIFO
principle where first element stored
in the queue will be removed first.
• Linked List:- In linked list data
structure, data elements are
connected through a series of • https://www.programiz.com/dsa/dat
nodes. And, each node contains a-structure-types
the data items and address to the
Linear Data Structures Non Linear Data Structures

The data items are arranged in sequential order, one after the The data items are arranged in non-sequential order
other. (hierarchical manner).

All the items are present on the single layer. The data items are present at different layers.

It can be traversed on a single run. That is, if we start from the It requires multiple runs. That is, if we start from the first
first element, we can traverse all the elements sequentially in a element it might not be possible to traverse all the elements in
single pass. a single pass.

Different structures utilize memory in different efficient ways


The memory utilization is not efficient.
depending on the need.

The time complexity increase with the data size. Time complexity remains the same.

Example: Arrays, Stack, Queue Example: Tree, Graph, Map


ALGORITHM COMPLEXITY
• An algorithm's space and time • Complexity measures how the
complexity can be used to determine resources (in this example, time)
its effectiveness. fluctuate as the problem grows in
size.
• Algorithmic complexity measures how long
an algorithm would take to complete given • An algorithm may run quickly and
an input of size n. If an algorithm has to show no time difference, but when
scale, it should compute the result within a the input size rises, the program may
finite and practical time bound, even for take longer to execute, become
large values of n. For this reason, sluggish, and perform poorly; here is
complexity is calculated asymptotically as where complexity is assessed
n approaches infinity. While complexity is
usually in terms of time, sometimes
complexity is also analysed in terms of
space, which translates to the algorithm's
memory requirements.
Asymptotic Notations
• Asymptotic notations are the mathematical The running time to perform any operation
notations used to describe the running time of depends on the input size.
an algorithm when the input tends towards a • Usually, the time required by an algorithm
particular value or a limiting value. comes under three types:
• Mathematical way of representing time • Worst case: It defines the input for which the
complexity.or it is used to describe the running algorithm takes a huge time.
time of an algorithm.
• Average case: It takes average time for the
• For example: In bubble sort, when the input program execution.
array is already sorted, the time taken by the
algorithm is linear i.e. the best case. • Best case: It defines the input for which the
algorithm takes the lowest time
• But, when the input array is in reverse
condition, the algorithm takes the maximum
time (quadratic) to sort the elements i.e. the
worst case. There are mainly three asymptotic notations:
• Big-O notation.(worst-case complexity )= the
algorithm's longest amount of time to complete
its operation
• Omega notation(best case complexity ) = It
determines the fastest time that an algorithm can
run.
• Theta notation(average-case complexity )= In
real-world problems, an algorithm does not
perform worst or best, it fluctuates between the
worst-case and best-case,
ARRAYS
• An arrays is defined as collection of homogenous
(similar) data which is stored in contiguous memory
locations or(continuous memory addresses) because
arrays is a linear data structure .
• Arrays can be single(1-D) or multi dimensional(2-D).

1st element last element


Address of an array
10 2 5 11 72

for int 1000 1002 1004 1006 1008

1st element last element

10.5 2.68 58.7 11.6 72.9

for float 1000 1004 1008 10012 10016

1st element last element

Calculating the length of an array


Length = upper_bound –lower_bound+1
Eg. 4-0+1= 5
Declaring Arrays
data-type variable-name[size];
Examples:-
char a[5]; /* char type value array */
float a[9]; /* float type value array */
int a[10]; /* int type value array */

a[0],a[1]…. Are known as index


The index of an array starts from 0 to size-1 means first
element of any array will be stored at a[0] address
and the last element will be at a[size - 1].
Initialization of Array
• After declaration it is necessary to initialize an array.
Otherwise, it will contain garbage value( random
value). An array can be initialized at either compile
time or at runtime. No validity check for array index
not available in c.
Compile time Initialization :- It means that providing
the value to an array in the code, during creation of it.
Syntax:-
data-type arrayname[size] = { list of values };
• int a[4] = { 67, 87, 56, 77 };
• float a[5] = { 23.4, 6.8, 5.5 };
• int marks[4] = { 67, 87, 56, 77, 59 }; // Compile time error .
• If the values are more than the declared array size than the compiler will give an
error.
Example:- //Inserting and Traversing Linear Array
• #include <iostream>
• using namespace std;
• int main() {
• int a[5];
• for (int i=0;i<5;i++)
• {
• cin>> a[i];
• }
• cout << "\nThe elements of array are: "<<endl;;
for (int i = 0; i < 5; ++i) {
• cout << a[i]<<endl;
• }
return 0;
• }
//Program to insert an elements at specific place in array. //Program to delete an elements from an array.
• #include <iostream>
#include <iostream>
• using namespace std;
using namespace std;
• int main()
int main()
• {
{
• int a[10],i,in,e;
int a[5],i,in,e;
• cout<<"enter elements in array";
cout<<"enter elements in array";
• for(i=0;i<5;i++)
for(i=0;i<5;i++)
• {
cin>>a[i];
• cin>>a[i];
cout<<"enter index from where you want to delete
• } elements";
• cout<<"enter elements and index"; cin>>in;
• cin>>e>>in; for(i=in;i<5;i++)
• for(i=5;i>=in; i--) a[i]=a[i+1];
• a[i+1]=a[i]; for(i=0;i<4;i++)
• a[in]=e; cout<<a[i];
• for(i=0;i<6;i++) return 0;
• cout<<a[i]; }
• return 0;
• }
Linear search
A linear search, also known as a sequential search, is
a method of finding an element within a list. It checks
each element of the list sequentially until a match is
found or the whole list has been searched. It works
on both sorted and unsorted list of elements.
Algorithm:-
1. Begin with the leftmost element of array[] and one
by one compare searching element with each
element.
2. If searching element matches with an element then
return the index.
3. If searching element does not match with any of the
elements then return -1.
//ALGORITHM

• Linear Search ( Array A, searching element e, I index of array, n is number of elements in array ).
• Step 1: Set i to 0
• Step 2: if i > n then go till last step
• Step 3: if A[i] = e
• step 4: break;
• Step 5: Set i to i + 1
• Step 6: Print element a found at index i and go to next step
• Step 7: Print element not found
• Step 8: Exit

1.Best Case = o(1)


2.Worst Case = o(n)
3.Average Case = o(n)
Code of linear search
• #include <iostream>
• using namespace std;
• int main() {
• int i,a[10] = {2, 4, 0, 1, 9};
• int n= 2;
• for( i=0;i<5;i++)
• { if (a[i]==n)
• {
• cout<<"element found at index "<<i;
• break;
• }
• }
• if (i!=n)
• cout<<"element not found ";
• return 0;
• }
Binary search
• A Binary Search is a sorting algorithm, that is used to
search an element in a sorted array.
• A binary search technique works only on a sorted
array, so an array must be sorted to apply binary
search on the array.
• It is a searching technique that is better then the liner
search technique as the number of iterations
decreases in the binary search.
//ALGORITHM • Best Case Time Complexity
1. Let start = 0 and end = n-1.
=O(1)
2. If (end < start), then stop: element is not • Average Case Time =O(logN)
present in array.
3. Return -1. • Worst Case Time = O(logN)
4. Compute mid it is the average
of end and start.
• Space Complexity = O(1)
5. While(start<=end)
6. If array[mid] equals element, then stop!
7. Return mid.
8. If array[mid] < element , then set start =
mid + 1.
9. If array[mid] >element. Set max = mid - 1.
10. End while
11. Go back to step 2.
• #include <iostream> • else if (a[mid] < x)
• using namespace std; • {
• int main() { • start = mid + 1;
• int a[] = {3, 4, 5, 6, 7, 8, 9}; • }
• int x, n=7; • else
• cout<<"enter element to search"; • {
• cin>>x; • end = mid - 1;
• int start=0, end=n-1; • }
• while (start <= end) { • }
• int mid = (start + end)/ 2; • if (start>end)
• if (a[mid] == x) • {
• { • cout<<"element not found";
• cout<<"element found at index"<<mid; • }
• break; • return 0;
• } • }
Sorting
• A sorting algorithm is used to arrange elements of an array/list
in a specific order.
Types of sorting algorithms
1. Bubble sort
2. Selection sort
3. Insertion sort
Bubble sort
• It compares two adjacent
elements and swaps them until it
gets sorted.
• //ALGORITHM
Time Complexity
1. Declare variables a[], j,i
2. Insert element in array a[10]. Best O(n)
3. FOR loop : i = 0 to n – 1 // outer loop Worst O(n2)
4. FOR loop : j = 0 to n -i- 1 // inner loop
if ( a[j]>a[j+1] ) then Average O(n2)
swap a[j] & a[j+1] Space Complexity O(1)
End For loop
5. End For loop
6. Display sorted array.
7. end
• #include <iostream> • {
• using namespace std; • s=a[j];
• int main() • a[j]=a[j+1];
• { • a[j+1]=s;
• int i,j,s,a[10]; • }
• • }
• for(i=0;i<4;i++) • }
• { • cout<<"the sorted array is"<<endl;
• cin>>a[i]; • for(i=0;i<4;i++)
• } • {
• for(i=0;i<4-1;i++) • cout<<a[i]<<endl;
• { • }
• for(j=0;j<4-1;j++) •
• { • return 0;
• if(a[j]>a[j+1]) • }

Selection sort
• It works by repeatedly selecting //ALGORITHM
the smallest (or largest) element • Declare variables a[], j,i
from the unsorted portion of the • Insert element in array a[10].
list and moving it to the sorted • for i=0 to n-1 // outer for loop
portion of the list. • min = i // set min value to i
• for j=i+1 to n // inner loop
• if arr[j] <arr[min] then // check which element is smaller
Time Complexity • min=j // store index of smallest element to min
Best O(n2)
• end for // inner loop
• if (min != i ) // swap if min does not match to i
Worst O(n2)
• swap (arr[min] , arr[i] ) // swapping
Average O(n2) • end for // outer for loop
Space Complexity O(1) • Display sorted array.
• end
• #include <iostream> • if (min != i) {
• using namespace std; • int temp = a[min];
• int main() { • a[min] = a[i];
• int a[5]; • a[i] = temp;
• cout << "Enter elements " << endl; • }
• for (int i = 0; i < 5; i++) { • }
• cin >> a[i]; • cout << "SORTED ARRAY: " << endl;
• } • for (int i = 0; i < 5; i++) {
• cout << a[i] << " ";
• for (int i = 0; i < 4; i++) { • }
• int min = i; • return 0;
• for (int j = i + 1; j < 5; j++) { • }
• if (a[j] < a[min]) {
• min = j;
• }
• }
Insertion sort
• It places an unsorted element at • //Algorithm
its suitable place in each • Declare variables a[], j,i
iteration.
• Insert element in array a[10].
• Consider first element as sorted
• i←1
Time
Complexity • j ← i-1;
Best O(n)
• while j > =0 and array[j - 1] > array[j]
Worst O(n2)
• swap array[j - 1] and array[j]
Average O(n2)
• j←j-1
Space
O(1)
Complexity
• end while

• i←i+1

• end while

end
• #include <iostream> • while(j>=0&&a[j]>s)
• using namespace std; • {
• int main() • a[j+1]=a[j];
• { • j--;
• int i,j,s,a[10]; • }
• • a[j+1]=s;
• for(i=0;i<4;i++) • }
• { • cout<<"the sorted array
• cin>>a[i]; is"<<endl;
• } • for(i=0;i<4;i++)
• for(i=1;i<4;i++) • {
• { • cout<<a[i]<<endl;
• s=a[i]; • }
• j=i-1; •
• return 0;
•}
Two dimensional Arrays
• It is also known as multidimensional array. It contains
a row index and a column index. Both the row's and
column's index begins with 0.
• Just like a one-dimensional array, Compile time and
runtime initialization of two dimensional array is
possible.
Declaration:-
data-type array-name[row-size][column-size]
Example:-
int a[3][4];
Compile-time initialization
Examples:-
int a[2][2] = {1, 2, 3, 4}; // {{1, 2},{3, 4}}
Int a[2][3] = {1, 2, 3, 4}; // {{1, 2, 3},{4}}
int a[2][4] = {1, 2, 3, 4}; // {{1,2,3,4}}
First of all the values are stored in first row, and then if
there is any extra value, it goes to next row.
Example:-
int a[][3] = {
{0,0,0},
{1,1,1}
}; // no any values is assigned to row in array. It means
that initialization of any number of rows. But must
specify number of columns, else it will give a compile
time error.
We can also initialize 2D arrays like this
int a[2][4] = { {0, 1, 2, 3} , {4, 5, 6, 7} };
or
int a[2][4] = {0,1,2,3,4,5,6,7};
Both are equivalent
Int [2][4] ={0} ;// all initialized with zero
int [3][4]= {1,1,1,1,2,2,2,2}
firstly
1 1 1 1
2 2 2 2

This row will be initialized by 0


• WAP to show the Runtime initialization • //WAP to print matrix

• #include <iostream> • #include <iostream>


• using namespace std; • using namespace std;
• int main() • int main()

• { • {
• int arr[3][3];
• int arr[2][2];
• int i, j;
• int i, j;
• cout<<"Enter array elements:";
• cout<<"Enter array elements:";
• for(i = 0; i < 3;i++)
• for(i = 0; i < 2;i++)
• {
• { • for(j = 0; j < 3; j++)
• for(j = 0; j < 2; j++) • {
• { • cin>>arr[i][j];
• cin>>arr[i][j]; • }
• } • }
• } • for(i = 0; i < 3; i++)
• for(i = 0; i < 2; i++) • {
• { • for(j = 0; j < 3; j++)

• for(j = 0; j < 2; j++) • {


• cout<<arr[i][j]<<'\t';
• {
• }
• cout<<arr[i][j];
• cout<<endl;
• }
• }
• }
• }
• }
Pointers
• Pointers are also known as variables, they carry the address of other
variable.
Declaration of pointers variable
Synatx :- datatype *pointer_name
eg. Int*p;
Initialization of pointers variable
Synatx :- pointer_name = &variable;
eg. P= &a;
one of the way to initialize = int *p= &a;
Another way to initialize = int a=10,*p=&a;
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int a=10; #include <stdio.h> int a=10,*p=&a;
int main()
int *p; printf("address of a %d\n ",p);
{
p=&a; int a=10; printf("value of a %d ",*p);
printf("address of a %d\n ",p); int *p=&a; return 0;
printf("value of a %d ",*p); printf("address of a %d\n ",p); }
printf("value of a %d ",*p);
return 0;
} return 0;
}
• Pointer to integer • Change the value of pointer
#include<iostream> #include<iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int a=10; int a=10;
int *p =&a; int *p =&a;
cout<<*p; cout<<*p;
return 0; *p=15;
} cout<<*p;
return 0;
}
• Passing argument to function using • #include<iostream>
pointer. • using namespace std;
• int add(int *a, int *b,int *c)
#include<iostream>
using namespace std; • {
int add(int *a, int *b,int *c) • *a=5;
{ • *b=2;
*c=*a+*b; • *c=*a+*b;
}
• return 0;
int main()
{
• }
int d,n=10, n1=11; • int main()
add(&n,&n1,&d); • {
cout<<d; • int d,n=10, n1=11;
• add(&n,&n1,&d);
return 0;
• cout<<d;
}

• return 0;
• }
• Pointer to Array int a[5]={1,2,3,4,5};
int *p[5]=&a[0]; //array of pointer
#include<iostream>
using namespace std;
int main()
{
int a[5]={1,2,3,4,5};
int *p=&a[0];
cout<<*p;
return 0;
}
• #include<iostream> • #include<iostream>
• using namespace std; • using namespace std;
• int main() • int main()
• { • {
• int a[5]={1,2,3,4,5}; • int a[5]={1,2,3,4,5};
• for (int i=0;i<5;i++) • for (int i=0;i<5;i++)
• { • {
• int *p=&i[a]; • int *p=&a[i];
• //cout<<*p; • Cout<<*p
• //cout<<*(a+i); • }
• cout<<*(i+a); • return 0;
• } • }
• return 0;
• }
Types of pointers
Void pointer • Deference is not allowed in
This pointer is not associated with void pointer
any datatype, so it can be int a= 10;
converted into any other type of void *vp;
pointer.
Vp= &a;
Syntax :- void *vp; //declaration
Cout<<*vp);
int a= 10;
this is not allowed.
Int *p=&a // p pointer can store
To use this “typecasting” is required.
only integer type of variable. But
Why:- because in case of int , char
in case of void it can point to any float(pointers) compiler knows how many bytes
type of variable. It is also know are required , but in case of void compiler don’t
as generic pointer. know about it , then there comes the need of
typecasting.
• by using void pointer, there is no so this is also the reason why Deference is not
need to use of different datatypes allowed in void pointer.
.
• #include <iostream> • #include <iostream>
• using namespace std; • using namespace std;
• int main() • int main()
• { • {
• int a=10; • char c='N';
• void *vp; • void *vp;
• vp=&a; • vp=&c;
• cout<<"void pointer"<<'\n'<<*(int*)vp; • cout<<void pointer<<'\n'<<*(char*)vp;
• return 0; • return 0;
• } • }

• *(int*)vp // this is typecasting


Null pointer Wild pointer
Pointer which points to nothing, or points An uninitialized pointer known as wild
to zero memory location. pointer. It means that it will point to the
we cannot dereference the pointer or it garbage value. They are risk to use, can
may crash the program. gave errors.
Syntax :- int *np= NULL ; To save such situations NULL pointer can
be used.
In c NULL itself is a pointer , it value is 0 Example :-
Example:- #include <iostream>
#include <iostream> using namespace std;
using namespace std; int main()
int main() {
{ int *Wp; //wild pointer
int *np= NULL; Cout<<*Wp;
cout<<null pointer <<np; return 0;
return 0; }
} Dangling pointer
A pointer pointing to the memory location
,that has been deleted(freed)is called as
dangling pointer

You might also like