Skip to content
This repository has been archived by the owner on Dec 29, 2019. It is now read-only.

Added Java-LinearSearch-10 #143

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ Linear search is a very simple search algorithm. In this type of search, a seque
|20| [Marco Wang](https://github.com/aesophor) | University of Taipei | Taiwan | Java, C, Bash, Python-3 |
|21| [Grzegorz Wcisło](https://github.com/grzegorz-wcislo) | | Poland | |
|22| [Ivan Dyominov](https://github.com/dyominov) | | Ukraine | Scala |
|23| [Mayank Kinger](https://github.com/kingermayank) | | India | Java |
16 changes: 16 additions & 0 deletions java-linear-search/Java-LinearSearch-10.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class LinearSearch {
public static void main(String args[]){
int array[] = {10, 20, 25, 63, 96, 57};
int size = array.length;
int value = 63;

for (int i=0 ;i< size-1; i++){
if(array[i]==value){
System.out.println("Element found index is :"+ i);
}else{
System.out.println("Element not found");
}
}
}
}

50 changes: 9 additions & 41 deletions java-linear-search/java-linear-serach-3.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,15 @@
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;

/**
* Implements the linear search algorithm in Java.
* Created on 2018/09/30 by Craxy.
*
* @author Craxy
* @version 1.1
*/
public class LinearSearch {
public static void main(String args[]){
int array[] = {10, 20, 25, 63, 96, 57};
int size = array.length;
int value = 63;

/**
* The entry point for the program.
*
* @param args the command line arguments.
*/
public static void main(String[] args) {
System.out.println("How many number are you going to enter? ");
Random rand = new Random()
//Create the scanner instance and the array of numbers
Scanner scanner = new Scanner(System.in);
int len = scanner.nextInt();
int[] array = new int[len];

for(int x=0; x<len; x++){
array[x] = rand.nextInt();
}
System.out.println(Arrays.toString(array));
System.out.println("Enter a number to search for");
int find = scanner.nextInt();
//Linear search
boolean found = false;
for(int x=0; x<len; x++){
if(array[x]==find){
found = true;
System.out.println("Found at index: "+x);
for (int i=0 ;i< size-1; i++){
if(array[i]==value){
System.out.println("Element found index is :"+ i);
}else{
System.out.println("Element not found");
}
}
if(!found){
System.out.println("Not found");
}
//Close the scanner
scanner.close();
}
}
28 changes: 24 additions & 4 deletions javascript-linear-search/javascript-Linear-Search-2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
function linearSearch(arr, key){
for(let i = 0; i < arr.length; i++){
if(arr[i] === key) return i;
function linear () {
let n = parseInt(prompt('Enter the size of an array'))
let a = []
for (let i = 0; i < n; i++) {
a[i] = parseInt(prompt('Current array:\t' + a + '\nEnter array elements'))
}
let k = parseInt(prompt('Current array:\t' + a + '\nEnter the key element to search: '))
for (let i = 0; i < a.length; i++) {
if (k === a[i]) {
// document.body.innerText('');
document.writeln('Element ' + a[i] + ' Found at Position:' + i)
break
} else if (i === (a.length - 1) && k !== a[i]) {
// document.body.innerText('');
document.writeln('Element Not Found')
}
}
}
const nums = [1, 2, 3, 8, 9, 12]
const target = 2
for (const num of nums) {
if (num === target) {
console.log('Successful Search!')
break
}
return null;
}