Data Structure and Algorithms Short Note: NSJ Online Academy
Data Structure and Algorithms Short Note: NSJ Online Academy
❖ Big 0 Notation
We can say that a function is "of the order of n", which can be written as
O(n) to describe the
upper bound on the number of operations. This is called Big-Oh notation.
Data Types
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
char keys[6]={'Q','W','E','R','T','Y'};
cout<<"The first 6 keys of a keyboard are ";
for (i=0; i<=5; i++)
cout<<keys[i]<<" ";
}
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
char s,r;
int found=0; //found=false
char keys[6]={'Q','W','E','R','T','Y'};
cout<<"Enter a character to be searched: "; cin>>s;
cout<<"Enter a character to replace: "; cin>>r;
i= (-1);
while (i<5 && found==0)
if (keys[++i]==s) found=1;
if (found==1)
keys[i]=r;
else
cout<<"No such element.";
}
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
char d;
nt found=0; //found=false
char keys[6]={'Q','W','E','R','T','Y'};
cout<<"Enter a character to be deleted: "; cin>>d;
i= (-1);
while (i<5 && found==0)
if (keys[++i]==d) found=1;
if (found==1)
keys[i]=NULL;
else
cout<<"No such element."
}
Multi-Dimensional Array:
#include<iostream.h>
#include<conio.h>
void main()
{
Int i,j;
clrscr();
int matrix[3][4]={{5,3,6,4},{2,5,3,7},{3,6,5,8}};
cout<<"The elements of the array are";
for (i=0; i<3; i++)
{
for (j=0; j<4; j++)
cout<<matrix[i][j]<<"\t";
cout<<"\n";
}
}
❖ Advantages of arrays:
❖ Disadvantages of arrays:
Fixed size
Same data type
Slow in searching an element
Slow in inserting/deleting an element
❖ What is Pointer?
❖ What is Stack
Stack is a data structure which is used to handle data in a last-in-first-out (LIFO) method.
That is we
can remove the most recently added element from the stack first.
Undo sequence in a text editor and the chain of method calls in a programming
language are
examples for applications of stack.
pop()-removes and returns the top most element from the stack.
topElt()-returns the top element without removing it.
isEmpty() - returns true if the stack has no elements and false otherwise.
isFull() - returns true if the stack is full of elements and false otherwise.
displayStack() - displays all elements from top to bottom.
❖ What is Queue?
❖ What is Tree?
Sorting Algorithm
❖ What is sorting?
❖ Selection Sort:
Here we repeatedly find the next largest (or smallest) element in the
array and move it to its final
❖ Bubble Sort
Here we repeatedly move the largest element to the highest index
position of the array.
Example: Sort the numbers 6, 7,72, 4, 32, 65, 9, 56 using bubble sort.
NSJ ONLINE ACADEMY 37
DATA STRUCTURE AND ALGORITHMS SHORT NOTE
Searching Algorithm
❖ What is search algorithm?
❖ Sequential Search:
It examines the first element in the list and then second element and
so on until a match is found.
❖ Binary Search:
This algorithm finds the middle item of a sorted array (in ascending
ordered array), compare it against
the searched value, then decide which half of the list must contain the
searched value, and repeat
with that half.
❖ Links
Introduction
https://youtu.be/JelOeHEG-WQ
Sorting Algorithms
https://youtu.be/G9fdmP-5sXM
Searching Algorithms
https://youtu.be/gJr9Im2sDF4