Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 13

Arrays

•Derived Data types


• Arrays
Declarations and Initialization
One dimensional Arrays
Two dimensional Arrays
Multi dimensional Arrays
Character Arrays
– One dimensional
– Two dimensional

1
Operation on Arrays

• Following operation can be performed on Arrays:

• Traverse (traverse whole array)

• Insertion and Deletion (at the end or middle)

• Searching (Linear or Binary)

• Merging (combining two arrays )

• Sorting (Ascending or descending)

2
Insertion

• Insertion at the End:


• Can be done easily by providing enough memory space.

• Insertion in the Middle somewhere:


• Elements situated after the new element’s position must be moved
downwards (location with largest subscription).

3
Insertion : Algorithm
• LA is a linear array with N elements and K is a positive integer. Following
algorithm inserts new element ITEM at Kth position in LA.

• Set J = N-1; (Initialize counter)

• Repeat while J>= K;


• Set LA[J] = LA[J-1]; (move J-1th element downwards)
• Set J = J-1; (Decrement counter)

• Set LA[K] = ITEM; (insert new element)

• Set N= N+1; (reset No of element)

• Exit
4
Deletion

• Deletion at the End:


• Deleting an element at the “end” of the array can be done easily by
reducing the size of array by one.

• Deletion in the middle:


• Deleting an element somewhere from middle requires each
subsequent element be moved one location upward (location with
smaller subscript) to fill up the array.

5
Deletion: Algorithm

• LA is a linear array with N elements and K is a positive


integer. Following algorithm deletes the K th element .

• (initialize the position of element to be deleted)


• Initialize ITEM=LA[k];

• Repeat for j = K to N-1;


• Set LA[j] = LA[j+1]; (Move J+1st element towards left)

• (Decrement the size of array by one)


• Set N=N-1;
• Exit.
6
Searching

• This method finds out whether the data entered by the user
is present in an array or not.
• There are two searching method:

(i) Linear or Sequential search

(ii) Binary search

7
Search: Linear Search

• This is a very simple algorithm.

• It uses a loop to sequentially step through an array, starting with the


first element.

• It compares each element with the value being searched for and stops
when that value is found or the end of the array is reached.

8
Algorithm steps
Given array A containing N elements. This algorithm search an Item with location
LOC. If Item is not there in A then it return unsuccessful search.

1. set LOC:=-1, Set J=0 //initialize counter

2. Repeat for J=0 to N-1 // search for an item


if A[j]==Item
LOC=j

4. If LOC==-1 then
Item not found
else
Item= A[LOC] // return item

5.Exit
Algorithm Efficiency

• The advantage is its simplicity.


• It is easy to understand
• Easy to implement
• Does not require the array to be in order

• The disadvantage is its inefficiency


• If there are 20,000 items in the array and what you are looking for is
in the 19,999th element, you need to search through the entire list.
Binary Search

• The binary search is much more efficient than the linear search.

• It requires the list to be in order.

• The algorithm starts searching with the middle element.


• If the item is less than the middle element, it starts over
searching the first half of the list.
• If the item is greater than the middle element, the search starts
over starting with the middle element in the second half of the
list.
• It then continues halving the list until the item is found.
Binary Search Algorithm
1. [initialize variables]
Set beg=0,end=N-1, mid=int(beg+end)/2;
2. Repeat 3 and 4 while(beg<=end) && (a[mid]!=item)
3. If(item<a[mid])
Set end=mid-1;
Else
Set beg=mid+1
4. Set mid=int(beg+end)/2
5. if(a[mid]==item)
Set loc=mid
Else
loc=null

6. Exit
Which search method is better
• Linear Search is slower, inefficient and works on unsorted list. If
the data we are searching is not present in the list, we come to
know at the end of the operation.

• The advantage of Binary search is that each search cuts the list in
to half. A list of 10,000 names can be searched in just 12 searches.

• So Binary search is far better than Linear search

13

You might also like