Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "WELCOME TO ARRAY By Vinay Alexander PGT(CS) Kendriya vidyalaya jhagrakhand."— 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 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.

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

6 Stack and Queue Operations

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

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

9 Operation on Data Structures 1.Insertion 2.Deletion 3.Searching 4.Traversal 5.Sorting 6.Merging

10 Array The starting address of the very first element of the array is called Base Address of the 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 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>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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4151990/slides/slide_11.jpg", "name": "Searching : Linear Search #include 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>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>n; cout<< Enter array elements ; for(int i=0; i>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

13 Searching : Binary Search #include 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>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 Insertion in array #include 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>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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4151990/slides/slide_14.jpg", "name": "Insertion in array #include 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>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; cout<< Enter array elements (sorted in asc order) ; for(int i=0; i>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

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 Deletion in array #include 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>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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4151990/slides/slide_16.jpg", "name": "Deletion in array #include 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>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; cout<< Enter array elements (sorted in asc order) ; for(int i=0; i>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

18 Traversal in array #include 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>ar[i]; cout<<“\n Array with doubled elements is as follows\n”; for(i=0;i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4151990/slides/slide_17.jpg", "name": "Traversal in array #include 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>ar[i]; cout<< \n Array with doubled elements is as follows\n ; for(i=0;i>n; cout<< Enter array elements (sorted in asc order) ; for(int i=0; i>ar[i]; cout<< \n Array with doubled elements is as follows\n ; for(i=0;i

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

20 Selection Sorting in array #include 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; }

21 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;}

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 Bubble Sorting in array #include 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 << " " < { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4151990/slides/slide_22.jpg", "name": "Bubble Sorting in array #include 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 << <

24 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; }

25 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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4151990/slides/slide_24.jpg", "name": "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

26 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 =0;) { if (A[a]<=B[b]) C[c++] = A[a++]; else C[c++] = B [b--]; } if(a=0) C[c++]=B[b--]; } }

27 TWO-Dimensional Arrays:

28


Download ppt "WELCOME TO ARRAY By Vinay Alexander PGT(CS) Kendriya vidyalaya jhagrakhand."

Similar presentations


Ads by Google