-
Posts
894 -
Joined
-
Last visited
About Azain
- Birthday October 2
Profile Information
-
Gender
Male
-
Location
Pakistan
-
Hobbies
Videos Games, Reading History, & Programming
Previous Fields
-
Real name
Azain
-
RPG Nickname
AzaiN
Recent Profile Visitors
3154 profile views
Azain's Achievements
-
Azain changed their profile photo
-
Verdant Family - Invoiri Activitati / Activities Consent
Azain replied to Kip's topic in Verdant Family
Nick-name: AzaiN Rank: 2 Invoire pentru war-ul din data: 02.05.2022 Motivul pentru care nu pot veni la war: I have a match in another game Numarul invoirii din aceasta saptamana (x/2): 1/2 Alte precizari: - Good luck at war! -
Verdant Family - Invoiri Activitati / Activities Consent
Azain replied to Kip's topic in Verdant Family
Nick-name: AzaiN Rank: 1 Invoire pentru war-ul din data: 29.04.2022 Motivul pentru care nu pot veni la war: Not at home Numarul invoirii din aceasta saptamana (x/2): 1/2 Alte precizari: Good luck! -
Green Street Bloods - Învoiri activităţi | Activities consent
Azain replied to aLexBz's topic in Green Street Bloods
Nick: AzaiN Rank: 2 Data warurilor la care nu poți ajunge: 5.11.2021 Numarul de invoiri pe aceasta saptamana: 1/2 Motivul învoirii: Not at home -
Green Street Bloods - Învoiri activităţi | Activities consent
Azain replied to aLexBz's topic in Green Street Bloods
Nick: AzaiN Rank: 1 Data warurilor la care nu poți ajunge: 1.11.2021 Numarul de invoiri pe aceasta saptamana: 1/2 Motivul învoirii: Not at home Alte precizări: - -
Green Street Bloods - Învoiri activităţi | Activities consent
Azain replied to aLexBz's topic in Green Street Bloods
Nick: AzaiN Rank: 1 Data warurilor la care nu poți ajunge: 26.10.2021 Numarul de invoiri pe aceasta saptamana: 1/2 Motivul învoirii: I have some important work to do Alte precizări: - Good luck! -
Nick: AzaiNRank: 1 Data activitatii: September 07, 2021 Alte precizari: https://imgur.com/a/9jekl0S
-
Verdant Family - Invoiri Activitati / Activities Consent
Azain replied to Kip's topic in Verdant Family
Nick-name: AzaiN Rank: 1 Invoire pentru war-ul din data: 09/06/2021 Motivul pentru care nu pot veni la war: Not at home Numarul invoirii din aceasta saptamana (x/2): 2/2 Alte precizari: na -
Verdant Family - Invoiri Activitati / Activities Consent
Azain replied to Kip's topic in Verdant Family
Nick-name: AzaiN Rank: 1 Invoire pentru war-ul din data: 07.06.2021 Motivul pentru care nu pot veni la war: Not at home Numarul invoirii din aceasta saptamana (x/2): 1 Alte precizari: - na -
Verdant Family - Invoiri Activitati / Activities Consent
Azain replied to Kip's topic in Verdant Family
Nick-name: AzaiN Rank: 1 Invoire pentru war-ul din data: 04.06.2021 Motivul pentru care nu pot veni la war: I have some work to do Numarul invoirii din aceasta saptamana (x/2): 1/2 Alte precizari: Good Luck! -
Green Street Bloods - Învoiri activităţi | Activities consent
Azain replied to aLexBz's topic in Green Street Bloods
Nick: AzaiN Rank: 2 Data warurilor la care nu poți ajunge: 25.05.2021 Numarul de invoiri pe aceasta saptamana: 1 Motivul învoirii: not at home Alte precizări: bafta! -
Binary Search What is Binary Search? In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array. Binary search runs in logarithmic time in the worst case, making O(log n) comparisons, where n is the number of elements in the array. Binary search is faster than linear search except for small arrays. However, the array must be sorted first to be able to apply binary search. There are specialized data structures designed for fast searching, such as hash tables, that can be searched more efficiently than binary search. However, binary search can be used to solve a wider range of problems, such as finding the next-smallest or next-largest element in the array relative to the target even if it is absent from the array. How to do Binary Search? Given an array A of n elements with values sorted such that A{0} <= A{1} <= A{2} <= ..... A{n-1} and target value T, the following subroutine uses binary search to find the index of T in A. Set L to 0 and R to n-1. If L > R, the search terminates as unsuccessful. Set m (the position of the middle element) to the floor of L + R / 2 , which is the greatest integer less than or equal to L + R / 2. If A[m] < T, set L to m+1 and go to step 2. If A[m] > T, set R to m-1 and go to step 2. Now A[m] = T, the search is done; return m. Code: package binarysearch; /** * * @author Azain */ public class BinarySearch { /* Recursive method to do binary search on an array, takes an integer array left index, right index and key to find as arguments, array should be sorted otherwise result will not be accurate */ public static int binarySearch(int arr[], int left, int right, int key) { //continue searching for the key until right index is greater then left index if (right >= left) { //middle index of left and right index int mid = left + (right - left) / 2; //Base case, if array middle element is equal to key, the index is returned if (arr[mid] == key) return mid; //when the middle element is greater than the key, search the left if (arr[mid] > key) return binarySearch(arr, left, mid - 1, key); //when the middle element is less than the key, search the right return binarySearch(arr, mid + 1, right, key); } //returning -1, when the key is not found in the array return -1; } public static void main(String[] args) { int arr[] = {1, 2 , 3 , 4, 5, 6, 7, 8 , 9}; int key = 7; int result = binarySearch(arr, 0, arr.length - 1, key); //if key is found if (result == -1) System.out.println(key + " not found!"); else System.out.println(key + " found at index " + result); } } Sources: https://en.wikipedia.org/wiki/Binary_search_algorithm
- 1 reply
-
- binary search
- algorithms
-
(and 1 more)
Tagged with:
-
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; } }
-
@Tupi For SFPlugins?