welcome to array vinay alexander pgt(cs) kv secl, jhagrakhand
TRANSCRIPT
WELCOME TO
ARRAY
VINAY ALEXANDER
PGT(CS)
KV SECL, JHAGRAKHAND
Data StructureA Data Structure is a named group of different data types which can be
processed as a single unit. A data structure has well-defined operations, behaviour and properties.
It has three prospective:
Application (or user) level: A way of modeling real-life data in a specific context.
Abstract (or logical) level: An abstract collection of elements and its corresponding set of accessing operations.
Implementation Level: A specific representation of the structure and its accessing operations in a programming language.
Types of Data StructureSimple Data Structure : These are normally built from
primitive data types.
Array and Structure
Compound Data Structure: Simple data Structure can be combined in various ways to form more complex structure called compound data structure classified it two types:
Linear: single level data structure. Elements form a sequence i.e. Stack, Queue and Linked List
Non-Linear: multilevel i.e. Tree
StackStack refer to the lists stored and accessed in a special way Stack refer to the lists stored and accessed in a special way i.e. LIFO technique. In stack, insertion and deletions take i.e. LIFO technique. In stack, insertion and deletions take place only at one end called the top.place only at one end called the top.
Queues
Queues are FIFO lists, where insertions take place at the Queues are FIFO lists, where insertions take place at the “rear” end of the queue and deletions take place at the “rear” end of the queue and deletions take place at the “front” end of the queues.“front” end of the queues.
Stack and Queue OperationsStack and Queue Operations
Link ListsLinked lists are special lists of some data elements linked to on Linked lists are special lists of some data elements linked to on another. The logical ordering is represented by having each element another. The logical ordering is represented by having each element pointing to the next element. Each element is called node, which has pointing to the next element. Each element is called node, which has two parts. The INFO part which stores the information and the two parts. The INFO part which stores the information and the POINTER part, which points to the next element.POINTER part, which points to the next element.
TreeTree are multilevel data structures having a hierarchical Tree are multilevel data structures having a hierarchical relationship among its elements called nodes. Topmost node is called relationship among its elements called nodes. Topmost node is called root of the tree and bottommost nodes are called leaves of the tree.root of the tree and bottommost nodes are called leaves of the tree.
Operation on Data Structures
1. Insertion
2. Deletion
3. Searching
4. Traversal
5. Sorting
6. Merging
Array OperationsSearching:Searching:
Linear Search:Linear Search:Each element of the array is compared with the given
item to be searched for, one by one. This method, which traverses the array sequentially to locate the given item, is called linear search or sequential search.
Binary Search:Binary Search:This search technique searches the given item in
minimum possible comparisons. Array must be sorted in any order.
Searching : Linear Search#include<iostrem.h>int Lsearch(int [ ], int, int);void main( ){ int ar[50], item, n ,index;cout<<“Enter desired array size (max 50) ”;cin>>n;cout<<“Enter array elements”;for(int i=0; i<n;i++){ cin>>ar[i];}cout<<“Enter the element to be search for”;cin>>item;index=Lsearch(ar,n,item);if(index==-1)
cout<<“not found”;else
cout<<“found”;}
int Lsearch(int ar[], int size, int item){ for(int i=0; i<size;i++)
{if (ar[i]==item) return 1;}return -1;
}
Searching : Binary Search
#include<iostrem.h>int Bsearch(int [ ], int, int);void main( ){ int ar[50], item, n ,index;cout<<“Enter desired array size (max 50) ”;cin>>n;cout<<“Enter array elements (sorted in asc order)”;for(int i=0; i<n;i++)
cin>>ar[i];cout<<“Enter the element to be search for”;cin>>item;index=Lsearch(ar,n,item);if(index==-1)
cout<<“not found”;else
cout<<“found”;}
int Bsearch(int ar[], int size, int item){ int beg=0, last=size-1, mid;while(beg<=last){ mid=(beg+last)/2;
if (item==ar[mid]) return mid;else if (item>ar[mid]) beg=mid+1;else last =mid -1;
}Return -1;}
Insertion in array#include<iostrem.h>int FindPos(int [ ], int, int);void main( ){ int ar[50], item, n ,index;cout<<“Enter desired array size (max 50) ”;cin>>n;cout<<“Enter array elements (sorted in asc order)”;for(int i=0; i<n;i++)
cin>>ar[i];cout<<“Enter the element to be inserted”;cin>>item;if(n==50)
{cout<<“Overflow”; exit(1);}index=FindPos(ar,n,item);for(i=n;i>index;i--)
ar[i]=ar[i-1];ar[index]=item;n+=1;for(i=0;i<n;i++)
cout<<ar[i]<<“ “;}
int FindPos(int ar[], int size, int item){ int pos;if(item<ar[0]) pos=0;else {for(int i=0;i<size-1;i++)
{ if(ar[i]<=item && item>ar[i]) { pos=i+1; break;}}if (i==size-1) pos=size;
}return pos;
}
Deletion in array#include<iostrem.h>int Lsearch (int [ ], int, int);void main( ){ int ar[50], item, n ,index;cout<<“Enter desired array size (max 50) ”;cin>>n;cout<<“Enter array elements (sorted in asc order)”;for(int i=0; i<n;i++)
cin>>ar[i];cout<<“Enter the element to be inserted”;cin>>item;if(n==0) {cout<<“Underflow”; exit(1);}index=Lsearch(ar,n,item);if (index!=-1) ar[index]=0;else cout<<“sorry”;for(i=index;i>n;i++)
ar[i]=ar[i+1];n-=1;for(i=0;i<n;i++)
cout<<ar[i]<<“ “;}
int Lsearch (int ar[], int size, int item){for(int i=0; i<size;i++)
{if (ar[i]==item) return 1;}return -1;
}
Traversal in array#include<iostrem.h>void main( ){ int ar[50], item, n ,index;cout<<“Enter desired array size (max 50) ”;cin>>n;cout<<“Enter array elements (sorted in asc order)”;for(int i=0; i<n;i++)
cin>>ar[i];cout<<“\n Array with doubled elements is as follows\n”;for(i=0;i<n;i++)
{ ar[i] *=2;cout<<ar[i]<<“ “;}
}
Selection Sorting in array#include <iostream.h>int SelectionSort(int [], int);int main()
{const int NUMEL = 10;int nums[NUMEL] = {22,5,67,98,45,32,101,99,73,10};int i, moves;moves = SelectionSort(nums, NUMEL);cout << "The sorted list, in ascending order, is:\n";for (i = 0; i < NUMEL; i++)cout << " " << nums[i];cout << '\n' << moves << " moves were made to sort this list\n";return 0;}
Selection Sorting in arrayint SelectionSort(int num[], int numel){ int i, j, min, minidx, grade, moves = 0;for ( i = 0; i < (numel - 1); i++){ min = num[i]; // assume minimum is the first array element
minidx = i; // index of minimum elementfor(j = i + 1; j < numel; j++)
{ if (num[j] < min) // if we've located a lower value { // capture it min = num[j]; minidx = j;}}
if (min < num[i]) // check if we have a new minimum{ // and if we do, swap valuesgrade = num[i];num[i] = min;num[minidx] = grade;moves++;}}
return moves;}
Bubble Sorting in array#include <iostream.h>
int BubbleSort(int [], int);
int main()
{
const int NUMEL = 10;
int nums[NUMEL] = {22,5,67,98,45,32,101,99,73,10};
int i, moves;
moves = BubbleSort(nums, NUMEL);
cout << "The sorted list, in ascending order, is:\n";
for (i = 0; i < NUMEL; ++i)
cout << " " <<nums[i];
cout << '\n' << moves << " were made to sort this list\n";
return 0;
}
Bubble Sorting in arrayint BubbleSort(int num[], int numel){ int i, j, grade, moves = 0; for ( i = 0; i < (numel - 1); i++) { for(j = 1; j < numel; j++) {
if (num[j] < num[j-1]) { grade = num[j]; num[j] = num[j-1]; num[j-1] = grade; moves++; } } }return moves;}
Insertion Sorting in arrayvoid InSort ( int ar[], int size)
{ int tmp, j;ar[0]=INT_MIN;for(int i=1; i <=size ; i++){ tmp=ar[i];
j=i+1;while(tmp<ar[j]){ ar[j+1]=ar[j];j--; }
ar[j+1]=tmp;}cout<<“After pass –” <<i <<“ – is: ”;for(int k=1; k<=size;k++)
cout<<ar[k]<<“ “;cout<<endl;
}
Merge Sorting in arrayvoid MergeSort ( int A[ ], int M, int B[ ], int N, int C[ ]){ int a,b,c;
for(a=0,b=N-1, c=-1; a<M && b>=0;){ if (A[a]<=B[b]) C[c++] = A[a++];
else C[c++] = B [b--];}if(a<M){ while(a<M)
C[c++] = A[a++];}else{ while(b>=0)
C[c++]=B[b--];}
}