Kumpulan Program Sederhana

Minggu, 24 April 2011

Sequential search

Searching sequential / linear search is the simplest model conducted on a data set. conceptually, the explanation as follows: on L which is the array which contains n pieces of data (L [0], L [1 ],..... L [N-1]) and k is the data you want to search. search conducted to find

   L[i] = k

with the smallest index i is the number that satisfy the condition 0 <= k <= n-1. Of course there is the possibility that the data sought was not found. examples

 L <----- [10.9.4.6.4.3.2.5]

where the first position 4? in this case k is 4 and k found in dengn position index of 2. implementasi dalam algoritma dan pemrograman c++

Algorithm: The following subroutine is an implementation of the sequential search algorithm. in this case the subroutine produces the return value as follows: 
  •  -1 if the data sought is not found, and
  • Numbers between 0 to n-1 (where n is the number of array elements) if the data sought is found.
Subroutine search (L, n, k)
  IF n <= 0 then
position <- (-1)
  OTHERWISE
  See <- FALSE
  i <- 0
  REVIEW DURING (i <n-1) AND (NOT met)
IF k = L [i] THEN
position <- i
See <- TRUE
SEBALINYA
i <- i +1
END IF
FINAL REVIEW
IF NOT THEN meet
position <- (-1)
END-IF
END-IF
VALUE-BACK position
END-subroutine

in program c++

#include <iostream.h>
#include <conio.h>

{ int posisi, i, ketemu;
   if (n <= 0)
   posisi = -1;
 else
 {
   ketemu = 0;
   i=1;
  while ((i < n-1) && ! ketemu)
  if  (data [i] ==k)
 {
   posisi = i ;
   posisi = 1;
 }
else
 i++;
 if (!ketemu)
 posisi = -1;
 }
 return posisi;
 }
int main ()
{ int data [8] = {6,7,8,5,4,3,2,1};
   int dicari = 1;
   cout <<"posisi" << dicari << "dalam larik data : "<< cari (data, 8, dicari) << "\n";
 getch ();
 return 0;
}

Tidak ada komentar:

Posting Komentar