Data Structures & Algorithms: by L. Mutanu
Data Structures & Algorithms: by L. Mutanu
ALGORITHMS
By L. Mutanu
CH2: ARRAYS
By L. Mutanu
CH2: ARRAYS
=
=
=
=
=
=
=
=
=
=
Exercise: Modify the insert method to accept data from the user instead
CH2: ARRAYS
void display()
{for(int j=0; j<10; j++) //display items
cout << v[j] << " ";
cout << endl;
}
};
int main()
{
myArray arr; //create an object
arr.insert();
arr.display();
return 0;
}
Exercise: Modify the insert method to accept data from the user instead
CH2: ARRAYS
By L. Mutanu
CH2: ARRAYS
SEARCH ALGORITHMS
1.
2.
3.
Searching calls for receiving an element value to search for (search key).
The search key is compared to each element in the array
Finally a message displayed if it was found or not
bool find(int searchKey) //receive search key
{
for(int j=0; j<10; j++) //compare each element,
if(v[j] == searchKey) //if found
break;
//exit loop before end
if(j == 10) //if end without find
return false;
// return cant find it expression
else
return true; //return found it expression
} //end find()
};
By L. Mutanu
CH2: ARRAYS
COMPLETE PROGRAM
#include <iostream>
using namespace std;
class myArray
{
private:
int v[100]; //array doubles
public:
void insert()
{
v[0]= 77; //insert 10 items
v[1]= 99;
v[2]= 44;
v[3]= 55;
v[4]= 22;
v[5]= 88;
v[6]= 11;
v[7]= 00;
v[8]= 66;
v[9]= 33;
} //array, at index
void display()
{for(int j=0; j<10; j++) //display items
cout << v[j] << " ";
cout << endl;
}
CH2: ARRAYS
DELETE ALGORITHM
1.
2.
The delete algorithm is similar to the search because it has to search for the item to delete first.
When it finds that element, it shifts all the elements in higher index cells down one cell, thus
overwriting the deleted value
You can call display() member function again after deleting to confirm
bool remove(int deleteKey)
{
int j;
for(j=0; j<10; j++) //look for it
if( deleteKey == v[j] )
break;
if(j==10) //cant find it
return false;
else //found it
{
for(int k=j; k<10; k++) //move higher ones down
v[k] = v[k+1];
return true;
}
} //end delete()
CH2: ARRAYS
COMPLETE PROGRAM
#include <iostream>
using namespace std;
class myArray
{
private:
int v[100]; //array doubles
public:
void insert()
{
v[0]= 77; //insert 10 items
v[1]= 99;
v[2]= 44;
v[3]= 55;
v[4]= 22;
v[5]= 88;
v[6]= 11;
v[7]= 00;
v[8]= 66;
v[9]= 33;
} //array, at index
void display()
{for(int j=0; j<10; j++) //display items
cout << v[j] << " ";
cout << endl;
}
int main()
{
myArray arr; //create an object
arr.insert();
arr.display();
if( arr.find(35) )
cout << "Found " << endl;
else
cout << "Cant find " << endl;
if( arr.remove(55))
cout << "Deleted " << endl;
else
cout << "Cant find " << endl;
arr.display();
Exercise: Modify the program to ask the user for a number to delete
CH2: ARRAYS
ORDERED ARRAYS
Linear search - With unordered arrays the only way to search for data is to go through
each element. This is called a linear search. At best the first element is what you are
searching for. At worst case it at the end thus search time=full array
Binary Search - With ordered arrays you can pick the midpoint of the array and if the
figure you are looking for is higher or lower you only need to move to one side and
pick the midpoint again. Notice this way you dont need to go through the entire array.
You can get lucky and find the search key at the first midpoint. Thus search
time=zero. Or at worst you only need to go through half the array. Thus search
time=half array.
By L. Mutanu
CH2: ARRAYS
By L. Mutanu
CH2: ARRAYS
2.
3.
4.
5.
CH2: ARRAYS
CH2: ARRAYS
To insert employee number 750 first perform a linier search to find out
where 750 should go
Create room by shifting the other elements from the end one element
at a time
CH2: ARRAYS
int main()
{
myArray arr; //create an object
arr.insert(77); //insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
return 0;
}
Exercise: Modify the program to accept input from the user. Use a loop
Add a delete method for ordered arrays.
CH2: ARRAYS
Deleting an element in an
ordered array calls for:
1. Performing a binary search
to find it
2. Pushing every element
after it forward to overwrite
it.
By L. Mutanu
CH2: ARRAYS
Advantages:
Disadvantages:
Searching is fast
By L. Mutanu
CH2: ARRAYS
How many steps are necessary to perform a binary search on a given array? Assume
an array of size 10. Lets pick an index to search :
With a binary search the first and last element take the most checks. For an array of
size 10 any index position can be found with less than 4 checks.
Thus for an array of size 100, index calls for checking a maximum of 50, 25, 12, 6, 3, 2,
and 1...7 checks
For an un ordered array the best case would be if the item is the first in the array and
the worst case would be if the item is the last one. We can assume the average of the
best and worst case to be the average search time in a linear search. Thus an array of
size 10 would take 10/2=5 and array of size 100 would be 100/2=50.or simply N/2
By L. Mutanu
CH2: ARRAYS
CH2: ARRAYS
By L. Mutanu
CH2: ARRAYS
BIG O NOTATION
When comparing algorithms, you dont really care about the particular
microprocessor chip or compiler; all you want to compare is how T
changes for different array sizes (N).
The Big O notation thus ignores the constant and describes time (T) as
the order of variations of the size (N) denoted by O()
Thus In Big O notation, we would say that a linear search takes O(N)
time (we ignore /2), and a binary search takes O(log N) time. Insertion
into an unordered array takes O(1), or constant time.
We find that Insertion into an unordered array takes the least time,
followed by a binary search and then a linear search takes the most
time.
CH2: ARRAYS
For both kinds of arrays, deletion takes O(N) time because half
the items (on the average) must be moved to fill in the hole.
By L. Mutanu