Académique Documents
Professionnel Documents
Culture Documents
Algorithm
Binary algorithm is quite simple.
1. get the middle element;
2. if the middle element equals to the searched value, the algorithm stops;
3. otherwise, two cases are possible:
o searched value is less, than the middle element. In this case, go to the step 1 for
the part of the array, before middle element.
o searched value is greater, than the middle element. In this case, go to the step 1 for
the part of the array, after middle element.
The binary search is a more efficient way of searching an array of data. As an array starts to get
large (greater than 16 elements) the binary search is the suggested method of searching.
However, the use of a binary search requires that the array be sorted.
The binary search begins by analyzing the middle term of the array. This determines if the target
is in the first half or second half of the array. If the target is in the first half of the array there is
no need to check the second half of the array. If the target is in the second half of the array there
is no need to check the first half of the array. By knowing in which half the target is located, we
eliminate the other half from consideration. This eliminates unnecessary searching, which
improves performance. So, if we had a target value of 200,000 in an array of one million
elements we could get rid of elements from 500,000 to 1,000,000 without searching! This
continues until we find the given target or until it is not found.
In the following example we find a target of 4 in a sorted array. We first find what the first,
middle, and last indexes are of the array. The middle index is found by:
middle index = (first index + last index)/2
Here we see that 0 is the first index, 2 is the middle index, and 4 is the last index.
a[0]
a[1]
a[2]
a[3]
a[4]
Now, we compare the target to the value of the middle index. The value of the middle index is 3,
which is greater than the index of 2, meaning that the second half of the array doesnt need to be
considered. We are left with the first half of the array and now repeat the process to find the first,
middle, and last indexes. We see that 0 is the first index and 1 is the last index. The middle index
is 0 because even though (0 + 1)/2 = 0.5, the value is of data type integer and C++ truncates this
making it 0 and does not round the value.
a[0]
a[1]
a[2]
a[3]
a[4]
Lastly, we compare the target value to the value of the middle index. The value of the middle
index is less than the target value so we end up having the first index of the array equaling the
target value. This terminates the searching process and we become successful in finding the
target value.
a[0]
a[1]
a[2]
a[3]
a[4]
Now, well implement this example of a binary search into C++ code. Since we already know
how many elements the array has we assign the array array the values of 1, 2, 3, 4, and 5. The
while loop continually checks to see if the first index value is less than or equal to the last index
value and as long as these conditions are true divides the array until the target is found, just like
in the explanation.
Binary Search Code 1
#include <iostream.h>
int BinarySearch(int [], int, int);
int main()
{
const int NUMEL = 10;
int nums[NUMEL] = {5,10,22,32,45,67,73,98,99,101};
int item, location;
cout << "Enter the item you are searching for: ";
cin >> item;
location = BinarySearch(nums, NUMEL, item);
if (location > -1)
cout << "The item was found at index location "
last = 2;
while(first <= last)
{
mid = (first + last)/2;
if(target > array[mid])
{
first = mid + 1;
}
else if(target < array[mid])
{
last = mid + 1;
}
else
{
first = last + 1;
}
}
// When dividing can no longer proceed you are left with the
// middle term. If the target equal that term you are
// successful.
if(target == array[mid])
{
cout << "Target found." << endl;
}
else
{
cout << "Target not found." << endl;
}
return 0;
}
Bubble Sort
The bubble sort is one of the simplest sorting algorithms, but not one of the most efficient. It
puts a list into increasing order by successively comparing adjacent elements, interchanging
them if they are in the wrong order.
Complexity
The bubble sort uses (n-1)n/2 comparisons, so it has (-) (n2) worst-case complexity in terms of
the number of comparisons used.
#include <stdio.h>
#include <iostream.h>
void bubbleSort(int *array,int length)//Bubble sort function
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if(array[i]>array[j])
{
int temp=array[i]; //swap
array[i]=array[j];
array[j]=temp;
}
}
}
}
void printElements(int *array,int length) //print array elements
{
int i=0;
for(i=0;i<10;i++)
cout<<array[i]<<endl;
}
void main()
{
int a[]={9,6,5,23,2,6,2,7,1,8};
// array to sort
bubbleSort(a,10);
//call to bubble sort
printElements(a,10);
// print elements
Selection Sort
#include <iostream.h>
void selectionSort(int *array,int length)//selection sort
function
{
int i,j,min,minat;
for(i=0;i<(length-1);i++)
{
minat=i;
min=array[i];
for(j=i+1;j<(length);j++) //select the min of the rest of
array
{
if(min>array[j])
//ascending order for
descending reverse
{
minat=j; //the position of the min element
min=array[j];
}
}
int temp=array[i] ;
array[i]=array[minat]; //swap
array[minat]=temp;
}
}
// array to sort
//call to selection sort
// print elements