Vous êtes sur la page 1sur 8

Linear search algorithm

The Linear Search, or sequential search, is simply examining each


element in a list one by one until the desired element is found. The
Linear Search is not very efficient. If the item of data to be found is at
the end of the list, then all previous items must be read and checked
before the item that matches the search criteria is found.
#include <iostream.h>
int LinearSearch(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 << "Items in list: 5,10,22,32,45,67,73,98,99,101\n";
cout << "Enter the item you are searching for: ";
cin >> item;
location = LinearSearch(nums, NUMEL, item);
if (location > -1)
cout << "The item was found at index location " << location
<< endl;
else
cout << "The item was not found in the list\n";
return 0;
}
// this function returns the location of key in the list
// a -1 is returned if the value is not found
int LinearSearch(int list[], int size, int key)
{
int i;
cout << "The size is: " << size << endl;
for (i = 0; i < size; i++)
{
if (list[i] == key)
return i;
}
return -1;

Binary search algorithm


First, the search item is compared with the middle element of the list. If the
search item is less than the middle item of the list, we restrict the search to
the upper half of the list; otherwise, we search the lower half of the list.
The binary search does provide better performance than the sequential search. However, the
binary search does require the array to be sorted, meaning that you would have to implement a
sorting program to sort the data before beginning the binary search. It is suggested that you use
the binary search only for large, sorted arrays.

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 "

<< location << endl;


else
cout << "The item was not found in the list\n";
return 0;
}
// this function returns the location of key in the list
// a -1 is returned if the value is not found
int BinarySearch(int list[], int size, int key)
{
int left, right, midpt;
left = 0;
right = size - 1;
while (left <= right)
{
midpt = (int) ((left + right) / 2);
if (key == list[midpt])
{
return midpt;
}

else if (key > list[midpt])


left = midpt + 1;
else
right = midpt - 1;
}
return -1;
}

Binary Search Code 2


#include <iostream>
using namespace std;
int main()
{
const int arraySize = 5;
int target;
// Array size and values already known.
int array[arraySize] = {1, 2, 3, 4, 5};
int first, mid, last;
cout << "Items in list are : 1, 2, 3, 4, 5\n";
cout << "Enter a target to be found: ";
cin >> target;
// Initialize first and last variables.
first = 0;

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;
}
}

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};
selectionSort(a,10);
printElements(a,10);
}

// array to sort
//call to selection sort
// print elements

Vous aimerez peut-être aussi