KEMBAR78
Array Operations.pptxdata structure array indsa | PPTX
DATA STRUCTURE
ARRAY
Type of Data Structure
2
Data Type
thanks to www.tutorialspoint.com
3
 Data type determines the values that can be used with the
corresponding type of data, the type of operations that can be
performed on the corresponding type of data. There are two
data types:
 Built-In Data Type
 Derived Data Type
 Built-In Data Type
 Those data types for which a language has built-in support are
known as built-in data types. Most of the languages provide the
following built-in data types.
 Examples: Integer, Float, Boolean, Character and String
Data Type (Continued…)
thanks to www.tutorialspoint.com
4
 Derived Data Type
 Those data types which are implementation independent, as
they can be implemented in one or the other way are known
as derived data types. There are normally built by the
combination of built-in data types and associated operations
on them.
 Examples: List, Array, Stack, Queue
 Basic Operations
 The data in the data structures are processed by certain
operations. These operations include the following:
 Traversing, Searching, Insertion, Deletion, Sorting, Merging etc
Array
thanks to www.tutorialspoint.com
5
 Array is a container which can hold a fix number of items
and these items should be of same type. Following are the
important terms of array:
 Element & Index
 Array Representation
Total Number of Elements in array
6
Basic Operations of an Array
thanks to www.tutorialspoint.com
7
 Following are the basic operations supported by an array:
 Traverse – Print all the array elements one by one
 Insertion – Adds an element at the given index
 Deletion – Deletes an element at the given index
 Search – Searches an element using the given index or by
the value
 Update – Updates an element at the given index
Traversing with Array (Examples)
8
 Total
 Finding even and odd
 Checking fail and pass
 More ?
Insertion with Array
9
 Insertion at end
 Insertion at start
 Insertion at the specified location
Insertion Operation
10
 Insert operation is to insert one or more data elements
into an array. Based on the requirement, a new element
can be added at the beginning, end, or any given index of
array.
 Let LA be a Linear Array (unordered) with N elements
and K is a positive integer such that K<=N. Following is
the algorithm where ITEM is inserted into the Kth position
of LA.
Insertion Operation (Algorithm)
11
1. Start
2. Set J = N
3. Repeat Step 4 and 5 while J >= K
4. Set LA[ J + 1 ] = LA[ J ]
5. Set J = J – 1
6. Set LA[ K ] = ITEM
7. Stop
Insertion Example
12
 int arr[5] = {4, 1, 8, 2};
 int val,loc;
 cout<<"enter value to insert"<<endl;
 cin>>val;
 cout<<"enter location"<<endl;
 cin>>loc;
 for(int i=4; i>loc; i--){
 // arr[i+1]=arr[i];
 arr[i]=arr[i-1];
 }
 arr[loc]=val;
 for(int i=0; i<=4; i++)
 cout<<"Values are "<<i<< " = "<<arr[i]<<endl;
Deletion Operation
thanks to www.tutorialspoint.com
13
 Deletion refers to removing an existing element from the
array and re-organizing all elements of an array.
 Consider LA is a linear array with N elements and K is a
positive integer such that K<=N. Following is the algorithm
to delete an element available at the Kthposition of LA.
 Complexity…?
Deletion Operation (Algorithm)
thanks to www.tutorialspoint.com
14
1. Start
2. Set J = K
3. Repeat Step 4 and 5 while J < N
4. Set LA[ J ] = LA[ J+1 ]
5. Set J = J + 1
6. Set N = N – 1
7. Stop
Search Operation
thanks to www.tutorialspoint.com
15
 You can perform a search for an array element based on
its value or its index.
 Consider LA is a linear array with N elements and K is a
positive integer such that K<=N. Following is the algorithm
to find an element with a value of ITEM using sequential
search.
 Complexity…?
Search Operation (Algorithm)
thanks to www.tutorialspoint.com
16
1. Start
2. Set J = 0
3. Repeat Step 4 and 5 while J < N
4. IF LA [ J ] is equal ITEM THEN GOTO Step 6
5. Set J = J + 1
6. Print J, ITEM
7. Stop
Update Operation
thanks to www.tutorialspoint.com
17
 Update operation refers to updating an existing element from
the array at a given index.
 Consider LA is a linear array with N elements and K is a
positive integer such that K<=N. Following is the algorithm to
update an element available at the Kth position of LA.
1. Start
2. Set LA [ K – 1 ] = ITEM
3. Stop
 Complexity…?
18
int main(){
int input[100], count, i, num;
cout << "Enter Number of Elements in Arrayn";
cin >> count;
cout << "Enter " << count << " numbers n";
// Read array elements
for(i = 0; i < count; i++){
cin >> input[i];
}
cout << "Enter a number to serach in Arrayn";
cin >> num;
// search num in inputArray from index 0 to elementCount-1
for(i = 0; i < count; i++){
if(input[i] == num){
cout << "Element found at index " << i;
break;
}
}
if(i == count){
cout << "Element Not Present in Input Arrayn";
}
return 0;
}

Array Operations.pptxdata structure array indsa

  • 1.
  • 2.
    Type of DataStructure 2
  • 3.
    Data Type thanks towww.tutorialspoint.com 3  Data type determines the values that can be used with the corresponding type of data, the type of operations that can be performed on the corresponding type of data. There are two data types:  Built-In Data Type  Derived Data Type  Built-In Data Type  Those data types for which a language has built-in support are known as built-in data types. Most of the languages provide the following built-in data types.  Examples: Integer, Float, Boolean, Character and String
  • 4.
    Data Type (Continued…) thanksto www.tutorialspoint.com 4  Derived Data Type  Those data types which are implementation independent, as they can be implemented in one or the other way are known as derived data types. There are normally built by the combination of built-in data types and associated operations on them.  Examples: List, Array, Stack, Queue  Basic Operations  The data in the data structures are processed by certain operations. These operations include the following:  Traversing, Searching, Insertion, Deletion, Sorting, Merging etc
  • 5.
    Array thanks to www.tutorialspoint.com 5 Array is a container which can hold a fix number of items and these items should be of same type. Following are the important terms of array:  Element & Index  Array Representation
  • 6.
    Total Number ofElements in array 6
  • 7.
    Basic Operations ofan Array thanks to www.tutorialspoint.com 7  Following are the basic operations supported by an array:  Traverse – Print all the array elements one by one  Insertion – Adds an element at the given index  Deletion – Deletes an element at the given index  Search – Searches an element using the given index or by the value  Update – Updates an element at the given index
  • 8.
    Traversing with Array(Examples) 8  Total  Finding even and odd  Checking fail and pass  More ?
  • 9.
    Insertion with Array 9 Insertion at end  Insertion at start  Insertion at the specified location
  • 10.
    Insertion Operation 10  Insertoperation is to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array.  Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that K<=N. Following is the algorithm where ITEM is inserted into the Kth position of LA.
  • 11.
    Insertion Operation (Algorithm) 11 1.Start 2. Set J = N 3. Repeat Step 4 and 5 while J >= K 4. Set LA[ J + 1 ] = LA[ J ] 5. Set J = J – 1 6. Set LA[ K ] = ITEM 7. Stop
  • 12.
    Insertion Example 12  intarr[5] = {4, 1, 8, 2};  int val,loc;  cout<<"enter value to insert"<<endl;  cin>>val;  cout<<"enter location"<<endl;  cin>>loc;  for(int i=4; i>loc; i--){  // arr[i+1]=arr[i];  arr[i]=arr[i-1];  }  arr[loc]=val;  for(int i=0; i<=4; i++)  cout<<"Values are "<<i<< " = "<<arr[i]<<endl;
  • 13.
    Deletion Operation thanks towww.tutorialspoint.com 13  Deletion refers to removing an existing element from the array and re-organizing all elements of an array.  Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to delete an element available at the Kthposition of LA.  Complexity…?
  • 14.
    Deletion Operation (Algorithm) thanksto www.tutorialspoint.com 14 1. Start 2. Set J = K 3. Repeat Step 4 and 5 while J < N 4. Set LA[ J ] = LA[ J+1 ] 5. Set J = J + 1 6. Set N = N – 1 7. Stop
  • 15.
    Search Operation thanks towww.tutorialspoint.com 15  You can perform a search for an array element based on its value or its index.  Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to find an element with a value of ITEM using sequential search.  Complexity…?
  • 16.
    Search Operation (Algorithm) thanksto www.tutorialspoint.com 16 1. Start 2. Set J = 0 3. Repeat Step 4 and 5 while J < N 4. IF LA [ J ] is equal ITEM THEN GOTO Step 6 5. Set J = J + 1 6. Print J, ITEM 7. Stop
  • 17.
    Update Operation thanks towww.tutorialspoint.com 17  Update operation refers to updating an existing element from the array at a given index.  Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to update an element available at the Kth position of LA. 1. Start 2. Set LA [ K – 1 ] = ITEM 3. Stop  Complexity…?
  • 18.
    18 int main(){ int input[100],count, i, num; cout << "Enter Number of Elements in Arrayn"; cin >> count; cout << "Enter " << count << " numbers n"; // Read array elements for(i = 0; i < count; i++){ cin >> input[i]; } cout << "Enter a number to serach in Arrayn"; cin >> num; // search num in inputArray from index 0 to elementCount-1 for(i = 0; i < count; i++){ if(input[i] == num){ cout << "Element found at index " << i; break; } } if(i == count){ cout << "Element Not Present in Input Arrayn"; } return 0; }