Você está na página 1de 15

DATA STRUCTURES

AND ALGORITHM

ALGORITHM
An algorithm is a method or a process followed to
solve a problem.
An algorithm has all of the following properties.
It must be correct.
It is composed of a series of concrete steps.
Concrete means that the action described by that
step is completely understood.
There can be no ambiguity as to which step will
be performed next.
It must be composed of a finite number of steps.

ALGORITHM
An algorithm is a well defined list of steps for
solving a particular problem.
The time and space are the two measures for
efficiency.
The complexity of an algorithm is a function
which gives the running time and/or space in
terms of input size.
The logical or mathematical model of a particular
organization of data is called a Data structure.
Examples are Array, stack ,Queue, Linked List etc.

DATA OPERATIONS
The operations performed on Linear Array are
Traversing (visiting) : Processing each element in
the Array
Insertion: Insert a new element in the array
Deletion: Delete an element from the list
Merging:
Combining two lists into one list.
Sorting: Arranging the elements in some type of
order.
Search: Finding the location of the element with
a given value.

TRAVERSING LINEAR ARRAY


Length or the number of data elements of the
array can be obtained from the index set by the
formula
Length = UB LB +1
i = LB;
While( i UB)
{
Cout<< A[i]<<\n;
i = i+1;
}

INSERTION OF AN ARRAY
Void insert(int a[], int size)
{ int index;
Cout<< Enter index:;
Cin>>index;
For(int i=size; i>=index; i--)
{
a[i] = a[i-1];
}
Cout<< Enter value that you want to insert: ;
Cin>> a[index];
}

DELETION OF AN ARRAY
Void delete(int a[],int &size)
{

Int index;
Cout<< Enter index: ;
Cin>>index;
For( int i=index; i<size-1; i++)
a[i] = a[i+1];
Size--;
}

BUBBLE SORT
Void bsort ( int a[] , int size)
{
For ( int i=0; i<size; i++){
For( int j=0; j< size i 1; j++){
If (a [ j+1] < a[j] )
int temp = a[j];
a[j] = a[ j + 1];
a [j+1] = temp;
}
}

BUBBLE SORT

SELECTION SORT
Void ssort(int a[],int size )
{
Int small,index;
For (int I = 0; i< size 1; i++)
{

Small = a[i];
Index = I;
For (int j = i + 1; j<size ; j++){
If (a [j] < small )
Small = a [j] ;
Index = j;
}

SELECTION SORT
a [index] = a[i];
a [i] = small;
}

LINEAR SEARCH
In linear search we search value by value and in
the end either we got the value or not.
Void search ( int a[] , int size)
{

Int key;
Bool found = false;
Cout << Enter key ;
Cin >> key;
For (int i=0; i<size && !found; i++)
If ( a[i] == key)
Found == true;

LINEAR SEARCH

If (found)
Cout<< value found;
Else
Cout<<Not Found;
If no changes are involved then we sent the value
by copy. When changes are involved that we set
value by reference.

BINARY SEARCH
Array must sorted
Void bsearch( int a[] , int size)
{

Int key, low = 0, high = size -1, mid;


Bool found = false;
Cout << Enter key: ;
Cin>>key;
While(low <= high && !found)
{
Mid = (low + high) / 2;
If ( a[mid] == key)

BINARY SEARCH

Found = true;
Else if (key < a[mid])
High = mid -1;
Else
Low = mid +1;
}
If (found)
Cout << Value found;
Else
Cout<< Found;
}

Você também pode gostar