Friday, April 1, 2011

SEARCHING in c++

| Friday, April 1, 2011 | 0 comments

Data search algorithms
 pile Search (Sequential search)
 Search by sentinel pile (Sequential search with sentinel)
 Search For two
1. Search consecutive (Sekuential Search)
Concept: comparing each of each array element one by one in sequence (row),starting from the first element to last element.
There are 2 kinds of successive searches, ie search the array is already sorted, andsearch on the array that has not been sorted.
 Algorithm Ascending Not On Data-1:

I  0
{check for i <N and L [I]! = X}
while (I <(N-1)) and (L [I]! = X) do
I  I +1;
End while
{I = N or L [I] = X}
Search results} {Conclude
if (L [I]! = X)
position = 0
else
position = i +1
end if
examples
# include <iostream>
# include <conio.h>
void main ()
{
int array [9] = {1,2,3,4,5,6,7,8,9}, i, n, x, position;
court <<"data that you want to search?"; cin>> x;
i = 0;
position = 0;
while (i <8 & & array [i]! = x)
{
i + +;
}
if (array [i]! = x)
court <<"sorry the search data does not exist ";
else if (array [i] == x)
{
position = i +1;
court <<"at position:" <<position;
}
getch ();
}

 
 If the searchable data no data found 90 results
 If the searchable data on position 6 6 results



• Algorithm Ascending Not On Data-2I  0Found  false{Check for i <N and L [I]! = X}while (I <(N-1)) and (not met) doif L [I] = X thenSee  true} {stop searchposition = i + 1;End ifelse {L [I]! = X}I  I +1if (i> 4)
         
Data''do not meetend ifEnd elseEnd whileExamples of programs searching Sekuential Search data is not sorted# Include <iostream># Include <conio.h>void main (){int array [] = {1,2,3,4,5}, i = 0, x;bool met; meet = false;court <<"enter the data to be searched?"; cin>> x;while (i <= 4 & &! met){If (array [i] == x){Met = true; court <<"data is found at position" <<i +1;}else{I + +; if (i> 4) court <<"data not found";}}getch ();}

2. Searching With pile-Sentinel• What is meant by the sentinel is a deliberately fictitious elements added after the last element of the array.• So if the last element of the array is L [N-1], the sentinel is placed on elements of L [N].• As a result, the search process will always find a searchable data,• will but must always be checked for the location of the data found, whether:o Among the array elements are real (between L [1] to L [N])o In the fictional element [L [N +1]]
Algorithm
L [N]  XI  0Found  false{Check for i <N and L [I]! = X}while (L [I]! = X)I  I +1End whileIf (I <N) then the data was found} {Position  I +1elsePosition  0 {not in array}End if
Example searching Sentinel# Include <iostream># Include <conio.h>void main (){int array [] = {1,2,3,4,5}, i = 0, x;court <<"enter the data to be searched?"; cin>> x;array [5] = x;i = 0; while (array [i]! = x) i + +;if (i <5){Court <<"See you in the position of the data:" <<i +1;}else{Court <<"data not found";}getch ();}
3. Searching For Two• Terms: Data must be sorted• One advantage of the sorted data is easier searches, which in this case is the search for two.• Actually, in everyday life we ​​often apply this algorithm.• To search for specific words in the dictionary (eg English dictionary),• we do not open the dictionary from the home page until the final pages one by one,• but we are looking for a way to divide or share the pages of the book



Principle of Search:• We assume the data is already sorted, for example sorted decline. We call the smallest index as the index of the leftmost end, and the largest index as the index of the far right end.
o Suppose he left index and right index is Ib. At first he was 0 and Ib is N.
• Step 1:o For two array elements at the middle element. Central element is the element with index k = (He + Ib) div 2.o (Element middle, L [k], dividing the array into 2 parts L [He ... k-1] and the right of L [k +1 ... Ib]).• Step 2:o Check whether L [k] = X. If L [k] = X, the search was stopped because X has been found, but if not, must be determined whether the search on the array to the left or the right array. If L [k] <X then the search is performed on the left array. Conversely, if L [k]> X then the search is performed on the right lines.• Step 3:o Repeat steps 1 through X, or He> Ib.

0 comments:

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Post a Comment

 

analytics

Powered By Blogger
© Copyright 2010. http://hackhaha.blogspot.com . All rights reserved | http://hackhaha.blogspot.com is proudly powered by Blogger.com | Template by o-om.com - zoomtemplate.com