1 / 28

WELCOME TO ARRAY

WELCOME TO ARRAY. By Vinay Alexander PGT(CS) Kendriya vidyalaya 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.

echo-hobbs
Download Presentation

WELCOME TO ARRAY

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. WELCOME TO ARRAY By Vinay Alexander PGT(CS) Kendriya vidyalaya jhagrakhand

  2. 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. That is it deal with the study of how data is organized in the memory ,how efficiently the data can be retrieved and manipulated and possible ways in which different data items are logically related. 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.

  3. 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

  4. 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. Stack

  5. 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. Queues

  6. Stack and Queue Operations

  7. 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. Link Lists

  8. 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. Tree

  9. Operation on Data Structures Insertion Deletion Searching Traversal Sorting Merging

  10. Array The starting address of the very first element of the array is called Base Address of the array. ARRAY SIZE(LENGTH)=Upper Bound(UB)-Lower(LB)+1 =>Address of element with subscript I=Base Address+ES(I-L).where ES is size of an array element.

  11. 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.

  12. 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; }

  13. 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; }

  14. Insertion in array: Insertion of new element in array can done in two ways • (i). If the array is unordered ,the new element is inserted at the end of array. • (ii). If the array is sorted then new element is added at appropriate position without altering the order and to achieve this, rest of the elements are shifted

  15. #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]<<“ “; } Insertion in array 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; }

  16. DELETION: The element to be deleted is first searched for in the array using one of the search techniques that is either linear search or binary search. if the search is successful, the element is removed and rest of the elements are shifted so as to keep the order of array undisturbed.

  17. #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]<<“ “; } Deletion in array int Lsearch (int ar[], int size, int item) {for(int i=0; i<size;i++) {if (ar[i]==item) return 1;} return -1; }

  18. #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]<<“ “;} } Traversal in array

  19. SELECTION SORT: The basic idea of a selection sort is to repeatedly selection the smallest key in the remaining unsorted array.

  20. #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

  21. 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;} Selection Sorting in array

  22. Bubble Sorting in array: The basic idea of bubble sort is to compare two adjoining values and exchange them if they are not in proper order. that is unsorted array is to be sorted in ascending order using bubble sort.

  23. #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

  24. 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; } Bubble Sorting in array

  25. 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; } Insertion Sorting in array

  26. 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--]; } } Merge Sorting in array

  27. TWO-Dimensional Arrays:

  28. Thank You !!!

More Related