Sie sind auf Seite 1von 12

DATA STRUCTURES (ARRAYS) Data Structure is basically an arrangement of various data items, either of the same type or of different

data types, so as to treat them as a single unit, which identifies an object. Types of Data Structures Array is basically a named list of finite number of variables, of same type, which are placed one after the other and are identified by a common name. Arrays can be 1D,2D and Multi D. Structure is basically a group of data items of different types, identified by a name. Stack is basically a collection of data items in which items are assumed to be stored on top of one another. In Stack, insertion and deletion takes place at one end. Queue is another data structure, in which data items are assumed to be placed one after the other in a line. Linked List is a special type of arrangement in which a pointer accompanies each data item. Pointer points to the next item in the list. Data structures can be classified into two categories: i) Linear Data Structures
1| Page

ii) Non Linear Data Structures Tree DATA STRUCTURE OPERATIONS Insertion, Deletion, Searching, Traversing, Sorting and Merging The arrays are static in nature because the space for array is allocated by the compiler before the execution of the program. Array Types: Arrays can be of different types: i) One Dimensional Array This array has got only one row of elements. ii) Multi Dimensional Arrays This type of array must have two or more rows. Binary Search Binary search makes the searching faster by applying a technique known as Divide and Conquer. The array is divided into two halves. Now the element will be present either in first half or in the second half of the array. If it is seen to be in first half then first half of the array is searched and the second half is discarded. If it is seen to be in second half then second half of the array is searched and the first half is discarded. This method is repeated till the element is found. The pre condition for this search is that array should be in sorted order. int bin_srch(int a[],int N,int item) { int beg=0,end=N-1; while(beg<=end)
2| Page

{ mid=int((beg+end)/2); if(item==a[mid]) return mid; else if(item<a[mid]) end=mid-1; else beg=mid+1; } return -1; } Sorting Sorting means arranging elements in a particular way (Ascending / Descending). Different type of sorting i) Bubble Sort as a bubble, in water when rises up , it keeps on growing and it becomes of maximum size when it reaches at the top level of water. Based on this behavior , the name is assigned to this technique, which works fine for small arrays. This technique is not efficient for big arrays. Bubble Sort #include<stdio.h> #include<iostream.h> #include<conio.h> void bubsort(int num[],int s) { clrscr(); int i,j,k,t=0; for(i=0;i<s;i++) { for(j=0;j<s-1-i;j++) {
3| Page

if(num[j]>num[j+1]) { t=num[j]; num[j]=num[j+1]; num[j+1]=t; } } } } void disp(int ar[],int s) { clrscr(); cout<<"\n\n\n\t"; for(int i=0;i<s;i++) cout<<" "<<ar[i]; } void main() { int a[]={22,67,8,90,1}; cout<<"before Sorting"; disp(a,5); getch(); bubsort(a,5); cout<<"After Sorting"; disp(a,5); getch(); } Selection Sort This sort starts from the first element and searches the entire array until it finds the minimal value. The sort places the minimum value at first place and writes the first place value at the place from where it had picked up the minimum value. After this it selects the second element and searches the second smallest element. This process is repeated until the entire array is sorted. #include<stdio.h> #include<iostream.h>
4| Page

#include<conio.h> void selsort(int num[],int s) { clrscr(); int i,j,k,t=0; for(i=0;i<s;i++) { for(j=i;j<s;j++) { if(num[i]>num[j]) { t=num[i]; num[i]=num[j]; num[j]=t; } } } } void disp(int ar[],int s) { clrscr(); cout<<"\n\n\n\t"; for(int i=0;i<s;i++) cout<<" "<<ar[i]; } void main() { int a[]={22,67,8,90,1}; cout<<"before Sorting"; disp(a,5); getch(); selsort(a,5); cout<<"After Sorting"; disp(a,5); getch(); }
5| Page

Merge Sort #include<stdio.h> #include<iostream.h> #include<conio.h> void disp(int ar[],int s) { clrscr(); cout<<"\n\n\n\t"; for(int i=0;i<s;i++) cout<<""<<ar[i]; } void merges(int a[],int b[],int c[],int m,int n) { int actr=0,bctr=n-1,cctr=0; while(actr<m&&bctr>=0) { if(a[actr]<=b[bctr]) c[cctr++]=a[actr++]; else c[cctr++]=b[bctr--]; } if(actr<m) { while(actr<m) c[cctr++]=a[actr++]; } else { while(bctr>=0) c[cctr++]=b[bctr--]; } } void main()
6| Page

{ int a[50],b[50],c[50],MN=0,m,n; cout<<"Enter size of first array"; cin>>m; cout<<"Enter size of Second array"; cin>>n; MN=m+n; cout<<"Enter Element of first Array(Ascending)"; for(int i=0;i<m;i++) cin>>a[i]; cout<<"Enter Element of Second Array(Descending)"; for(i=0;i<n;i++) cin>>b[i]; merges(a,b,c,m,n); disp(c,MN); getch(); } Insertion Sort #include<iostream.h> void main() { int num[]={44,33,55,22,11}; int i=0,j=0,t=0; for(i=0;i<4;i++) { if (num[i+1] < num[0]) { t=num[i+1]; for(j=i+1;j>=0;j--) { num[j]=num[j-1];
7| Page

} num[0]=t; } } for(i=0;i<5;i++) cout<<" "<<num[i]; } Example card Playing , sorting 13 cards in order of 1,2,3,4,.. Insertion Adding a new element in the array is called Insertion.if the array is unordered, the new element is inserted at the end of the 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. If the array is already full, then insertion of an element into its result into OVERFLOW. Deletion The element to be deleted is first searched , if search is successful, the element is removed and rest of the elements are shifted so as to keep the order of array undisturbed. Program to demonstrate array #include<iostream.h> #include<conio.h> class array{ int ar[50]; int N; public: void accept(); void disp(); void selsort(); void bubsort();
8| Page

void insarray(); void delarray(); int Lsearch(int val); }; int array::Lsearch(int val) { for(int i=0;i<N;i++) if(ar[i]==val) return i; return -1;} void array::delarray() { cout<<"Enter element to delete"; int ele; cin>>ele; if(N==0) {cout<<"No elements to delete"; return;} int pos=Lsearch(ele); for(int i=pos;i<N;i++) ar[i]=ar[i+1]; N=N-1; } void array::insarray() { cout<<"Enter element to add"; int ele; cin>>ele; if(N==50) { cout<<"Overflow...."; return } int pos=0; if(ele<ar[0]) pos=0; else { for(int i=0;i<N;i++) { if(ar[i]<=ele && ele < ar[i+1]) { pos=i+1;break; } } if(i==N-1) pos=N;}
9| Page

for(int i=N;i>pos;i--) { ar[i]=ar[i-1]; } ar[pos]=ele; N=N+1; } void array::accept() { cout<<"Enter N"; cin>>N; for(int i=0;i<N;i++) { cout<<"enter element"<<i+1; cin>>ar[i]; } } void array::disp() { clrscr(); for(int i=0;i<N;i++) { cout<<"\nelement "<<ar[i]; } getch(); } void array::selsort() { int t; for(int i=0;i<N;i++) { for(int j=i;j<N;j++) { if(ar[i]>=ar[j]) { t=ar[i]; ar[i]=ar[j]; ar[j]=t;
10 | P a g e

} } }} void array::bubsort() { int t; for(int i=0;i<N;i++) { for(int j=0;j<(N-1)-i;j++) { if(ar[j]>ar[j+1]) { t=ar[j]; ar[j]=ar[j+1]; ar[j+1]=t; } } }} void main() { array a1; a1.accept();a1.disp(); //a1.selsort(); a1.bubsort(); a1.disp(); a1.insarray(); a1.disp(); a1.delarray(); a1.disp(); getch(); } Two Dimensional Array Address Calculation Row Major Implementation A[I][J]= B+w(C(I-Lr)+(J-Lc)) Column Major A[I][J]= B+w((I-Lr)+R(J-Lc))
11 | P a g e

Program Practice Matrix , 2D operations, 1D operation

12 | P a g e

Das könnte Ihnen auch gefallen