Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
Samenvatting

Summary Searching Algorithms Basics

Beoordeling
-
Verkocht
-
Pagina's
12
Geüpload op
08-11-2024
Geschreven in
2024/2025

These are simple and easy-to-navigate notes with examples and answers provided. The examples are illustrated with colors. Only simple colors are used so as not to make the document overwhelming. It is structured with enough white space for future annotations. There are several examples of algorithms presented along with their details and explanations. The programming jargons are explained at each step.

Meer zien Lees minder
Instelling
Vak

Voorbeeld van de inhoud

Lecture 9 Searching Algorithms

Searching
Searching data involves determining whether a value (referred to as the search key)
is present in the data and, if so, finding its location
Two popular search algorithms are the simple linear search and the faster but more
complex binary search.
Linear Search examines each element in an array one by one.
If the search key is not found: The algorithm checks every element in the array
sequentially. Once it reaches the end without finding a match, it informs the user
that the search key is not present.
If the search key is found: The algorithm continues checking elements until it finds
a match, then returns the index of that element.
// perform a linear search on the data
public static int linearSearch(int data[],int searchKey)
{
for (int index = 0; index < data.length ; index++) {
if (data[index] == searchKey)
return index;
}
return -1;
}
Big-O of Linear Search
The linear search algorithm runs in O(n) time.
Worst Case: In the worst case, every element in the array must be checked to
determine if the search item exists.

, Array Size Impact: If the array size doubles, the number of comparisons also
doubles, making the time complexity directly proportional to the array size.
Best Case: Performance can be excellent if the element is near the front of the
array, as fewer comparisons are needed.
Average Case: We aim for algorithms that perform well across all searches,
including those where the element is near the end.
Efficiency: For programs with many searches on large arrays, a more efficient
algorithm, like binary search, is preferable


Binary Search
Binary search is more efficient than linear search but requires the array to be
sorted.
1. The algorithm starts(first iteration) by checking the middle element. If it
matches the search key, the search ends.
2. If the search key is smaller than the middle element, the algorithm focuses
on the first half of the array.
3. If the search key is larger, it continues with the second half.
4. In each iteration, the middle element of the remaining portion is tested.
5. If no match is found, half of the remaining elements are eliminated with
each iteration.
6. The algorithm ends when either the element matching the search key is
found, or the remaining subarray size becomes zero.
// perform a binary search on the data

public static int binarySearch(int[] data, int key) {
int low = 0;
int high = data.length -1;
int middle = (low + high + 1) / 2;
int location = -1;

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
8 november 2024
Aantal pagina's
12
Geschreven in
2024/2025
Type
SAMENVATTING

Onderwerpen

€7,52
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kun je een ander document kiezen. Je kunt het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper
Seller avatar
Elitish

Maak kennis met de verkoper

Seller avatar
Elitish university of mauritius
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
-
Lid sinds
1 jaar
Aantal volgers
0
Documenten
3
Laatst verkocht
-
Elitish's Summary notes

Summary notes on all topics related to Computer Science/Software Engineering. Simple, no overwhelming colors and examples. These can help refresh your memory or serve as the base for your learning.

0,0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Bezig met je bronvermelding?

Maak nauwkeurige citaten in APA, MLA en Harvard met onze gratis bronnengenerator.

Bezig met je bronvermelding?

Veelgestelde vragen