Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
WELCOME TO ARRAY VINAY ALEXANDER PGT(CS) KV SECL, JHAGRAKHAND Data Structure A 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 Structure Simple 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 Stack Stack refer to the lists stored and accessed in a special way i.e. LIFO technique. In stack, insertion and deletions take place only at one end called the top. Queues Queues are FIFO lists, where insertions take place at the “rear” end of the queue and deletions take place at the “front” end of the queues. Stack and Queue Operations Link Lists Linked lists are special lists of some data elements linked to on another. The logical ordering is represented by having each element pointing to the next element. Each element is called node, which has two parts. The INFO part which stores the information and the POINTER part, which points to the next element. Tree Tree are multilevel data structures having a hierarchical relationship among its elements called nodes. Topmost node is called root of the tree and bottommost nodes are called leaves of the tree. Operation on Data Structures 1. 2. 3. 4. 5. 6. Insertion Deletion Searching Traversal Sorting Merging Array Operations Searching: 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: 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); int Lsearch(int ar[], int size, int item) if(index==-1) { for(int i=0; i<size;i++) cout<<“not found”; {if (ar[i]==item) return 1;} else return -1; cout<<“found”; } } 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++) int Bsearch(int ar[], int size, int item) cin>>ar[i]; { int beg=0, last=size-1, mid; cout<<“Enter the element to be search for”; while(beg<=last) cin>>item; { mid=(beg+last)/2; index=Lsearch(ar,n,item); if (item==ar[mid]) return mid; if(index==-1) else if (item>ar[mid]) beg=mid+1; cout<<“not found”; else last =mid -1; else } cout<<“found”; 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]; int FindPos(int ar[], int size, int item) cout<<“Enter the element to be inserted”; { int pos; cin>>item; if(item<ar[0]) pos=0; if(n==50) else {for(int i=0;i<size-1;i++) {cout<<“Overflow”; exit(1);} { if(ar[i]<=item && item>ar[i]) index=FindPos(ar,n,item); { pos=i+1; break;} for(i=n;i>index;i--) } ar[i]=ar[i-1]; if (i==size-1) pos=size; ar[index]=item; n+=1; } for(i=0;i<n;i++) return pos; cout<<ar[i]<<“ “; } 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; int Lsearch (int ar[], int size, int item) else cout<<“sorry”; {for(int i=0; i<size;i++) for(i=index;i>n;i++) {if (ar[i]==item) return 1;} ar[i]=ar[i+1]; return -1; n-=1; } for(i=0;i<n;i++) cout<<ar[i]<<“ “; 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 array int 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 element for(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 values grade = 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 array int 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 array void 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 array void 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--]; } }