You are on page 1of 4

Example 3: Recursive Algorithm for Sequential Search

Algorithm 3 SeqSearch(L, i, j, x)
Input: L is an array, i and j are positive integers, i j, and x is the key to
be searched for in L.
Output: If x is in L between indexes i and j, then output its index, else output
0.
Algorithm:
if i j , then
{
if L(i) = x, then return i ;
else return SeqSearch(L, i+1, j, x)
}
else return 0.
Recursive algorithms can also be used to test objects for membership in a set.
function fib( int n )
{
if ( (n == 0) || (n == 1) ) return 1;
else return fib(n-1) + fib(n-2);
}
===========
//=========================================================== binarySearch
/** Binary search of sorted array. Negative value on search failure.
* The upperbound index is not included in the search.
* This is to be consistent with the way Java in general expresses ranges.
* The performance is O(log N).
* @param sorted Array of sorted values to be searched.
* @param first Index of first element to serach, sorted[first].
* @param upto Index of last element to search, sorted[upto-1].
* @param key Value that is being looked for.
* @return Returns index of the first match, or or -insertion_position
* -1 if key is not in the array. This value can easily be
* transformed into the position to insert it.
*/
public static int binarySearch(int[] sorted, int first, int upto, int key) {

while (first < upto) {
int mid = (first + upto) / 2; // Compute mid point.
if (key < sorted[mid]) {
upto = mid; // repeat search in bottom half.
} else if (key > sorted[mid]) {
first = mid + 1; // Repeat search in top half.
} else {
return mid; // Found it. return position
}
}
return -(first + 1); // Failed to find key
}
BinarySearch(A[0..N-1], value, low, high) {
if (high < low)
return -1 // not found
mid = low + ((high - low) / 2)
if (A[mid] > value)
return BinarySearch(A, value, low, mid-1)
else if (A[mid] < value)
return BinarySearch(A, value, mid+1, high)
else
return mid // found
}
===========
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
const int SIZE = 5; // must be a constant variable
int myArray[SIZE];
cout << "Enter 5 Numbers: "<< endl;
for(int i = 0; i < SIZE; i++)
{
cin >> myArray[i];
}
cout << "Your 5 numbers are: ";
for(int i = 0; i < SIZE; i++)
{
cout << myArray[i] << " ";
}
//system("pause"); // pause system
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{int max;
int a[5]={12,1,0,4,5};
int min=a[0];
int i=1;
max=a[0];
while(i<=4)
{
if (max < a[i])
max=a[i];
if (min > a[i])
min=a[i];
i++;
}
cout << min <<"\n";
cout << max;
getch();
return 0;
}
public void reverse(Object [] a){
for(int i = 0; i < a.length / 2; i++){
Object temp = a[i]; // swap using temporary storage
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;
}
}
Read more: http://javarevisited.blogspot.com/2013/03/how-to-reverse-array-in-jav
a-int-String-array-example.html#ixzz3BbyKXuDn
=========
// count algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::count
#include <vector> // std::vector
int main () {
// counting elements in array:
int myints[] = {10,20,30,30,20,10,10,20}; // 8 elements
int mycount = std::count (myints, myints+8, 10);
std::cout << "10 appears " << mycount << " times.\n";
// counting elements in container:
std::vector<int> myvector (myints, myints+8);
mycount = std::count (myvector.begin(), myvector.end(), 20);
std::cout << "20 appears " << mycount << " times.\n";
return 0;
}
====
A palindrome number is the same as a palindrome string, if you reverse it, it wi
ll not change. For instance the numbers 121, 212, 454 and 12321 are palindrome n
umbers. Take a look at the following C example:
#include <stdio.h>
int main() {
int number, reverse = 0, temp;
printf("Enter number:");
scanf("%d", &number);
temp = number;
while( temp != 0 ) {
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp / 10;
}
if ( number == reverse )
printf("The number is a Palindrome number.\n");
else
printf("The number is not a Palindrome number.\n");
return 0;
}
=========
#include<iostream>
using namespace std;
int main()
{
for(int i=0;i<=5;i++){
for(int j=0;j<=i;j++)
{
cout<<j;
}
cout<<endl;
}
return 0;
}
=============
// function
int square( int n ) {
return n * n;
}
// procedure
void display( int n ) {
printf( "The value is %d", n );
}

You might also like