Search the Community
Showing results for tags 'search'.
-
In this Tutorial, I will show you how to perform Linear Search on an Array in Java, I assume that you know the basics of programming and Java language. What is Linear Search? In computer science, a linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched. Wikipedia Why Linear Search? Linear search is usually very simple to implement, and is practical when the list has only a few elements, or when performing a single search in an un-ordered list. When many values have to be searched in the same list, it often pays to pre-process the list in order to use a faster method. For example, one may sort the list and use binary search, or build an efficient search data structure from it. Should the content of the list change frequently, repeated re-organization may be more trouble than it is worth. As a result, even though in theory other search algorithms may be faster than linear search (for instance binary search), in practice even on medium-sized arrays (around 100 items or less) it might be infeasible to use anything else. On larger arrays, it only makes sense to use other, faster search methods if the data is large enough, because the initial time to prepare (sort) the data is comparable to many linear searches. Wikipedia How to do Linear Search? To do Linear Search on an array, we have to traverse through each element until we have found the element, we were searching for. 1. We'll create a method that takes an integer array and a key as input public static int linearSearch(int arr[], int key) { } 2. Declare two variables, one for storing the index and other for storing the length of the array //In Java, you can get length of an array by typing .length after the name of array int index, length = arr.length; 3. To traverse through each element of the array, we will use a for loop, initialize the index to 0 and keep incrementing by 1, inside the the loop there is a condition if indexth element of array is equal to the key then we'll return the index, otherwise if we have traversed through each element and have not found the key then we'll return -1; for (index = 0; index < arr.length; index++) { if (arr[index] == key) { return index; } } return -1; That's how you can do linear search on an array, following is the full code package com.azain.algorithms; /** * * @author Azain */ public class Search { //Linear Search public static int linearSearch(int arr[], int key) { int index, length = arr.length; for (index = 0; index < arr.length; index++) { if (arr[index] == key) { return index; } } return -1; } }
-
[Tutorial PHP] Functia "Search" in aceeasi pagina
Jackson Brooklyn posted a topic in Web Development
Salutare prieteni, am revenit cu o continuare la primul tutorial in care aratam cum sa conectam baza de date in PHP si sa afisam date in pagina. Astazi o sa va arat cum sa adaugam functia de search care afiseaza rezultate in aceeasi pagina PHP. Pentru a putea intelege corect tot tutorialul de astazi, va rog sa aruncati o geana peste acest tutorial: Adaugarea functiei search si afisarea datelor in aceeasi pagina. Dupa ce baza de date este conectata iar pagina index.php afiseaza datele din baza de date adaugam in mai sus de codul PHP un cod HTML care arata cam asa: <form action="index.php" method="post"> <input type="text" name="search" /> <input type="submit" value="Search" /></form> Ce face acest cod mai exact, face un textbox in care noi putem sa adaugam niste valori iar cand apasam butonul, el prin metoda post ne paseaza valorile catre functia %searchq% pe care o sa o definim si o folosim mai departe in codul MySQL. Pagina initiala trebuie sa arate cam asa, inainte de adaugarea codului HTML pentru search: De la acest cod de baza(Codul este explicat parca in celalalt tutorial) care doar afiseaza niste date in pagina incepem sa lucram mai departe pentru functia search: <?php require_once "config.php"; ?> <form action="index.php" method="post"> <input type="text" name="search" /> <input type="submit" value="Search" /></form> <section id="content"> <?php $q = mysqli_query($link, "SELECT * FROM comments Order by `id` desc LIMIT 5600"); while($row = mysqli_fetch_array($q)) { ?> <h5>User: <?= $row["nume"]; ?> <small> Ora: <?= $row["ora"]; ?></small></h5> <?= $row["text"]; ?> <p> <br> </p> <?php } ?> </section> Dupa adaugarea codului, pagina trebuie sa arate cam asa: Primul pas este deja facut, am adaugat codul HTML care ne paseaza valorile, urmeaza sa facem o conditie(IF), care spune asa: Daca functia search nu este atinsa, afiseaza toate datele, daca functia search este atinsa, afiseaza doar datele care sunt asemanatoare cu valorile din %searchq%. Tot ce trebuie sa facem este sa copiem partea de cod care deja ne afiseaza datele in MySQL si sa o adaptam conditiei conform cerintelor noastre. <?php require_once "config.php"; ?> <form action="index.php" method="post"> <input type="text" name="search" /> <input type="submit" value="Search" /></form> <section id="content"> <?php if(isset($_POST['search'])) { $searchq = $_POST['search']; $q = mysqli_query($link, "SELECT * FROM comments WHERE `nume` LIKE '%$searchq%' OR `text` LIKE '%$searchq%' OR `ora` LIKE '%$searchq%' Order by `id` desc"); while($row = mysqli_fetch_array($q)) { ?> <h5>User: <?= $row["nume"]; ?> <small> Ora: <?= $row["ora"]; ?></small></h5> <?= $row["text"]; ?> <p> <br> </p> <?php } } else { $q = mysqli_query($link, "SELECT * FROM comments Order by `id` desc LIMIT 5600"); while($row = mysqli_fetch_array($q)) { ?> <h5>User: <?= $row["nume"]; ?> <small> Ora: <?= $row["ora"]; ?></small></h5> <?= $row["text"]; ?> <p> <br> </p> <?php } } ?> </section> Dupa adaugarea conditiei, cam asa trebuie sa functioneze codul: Si practic cam asta este toata vrajeala, totul tine de o conditie care afiseaza doar datele relevante cautarii noastre. Daca aveti intrebari, astept PM pe forum, comentarii sau mesaje pe discord: GhOsT_x#6329 sau b-zone.ro/discord.