This repository has been archived by the owner on Dec 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
67 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Linear Search in perl | ||
|
||
Example linear search code in perl language. | ||
|
||
### Run | ||
|
||
```bash | ||
$ ./perl perl-linear-search.pl | ||
``` | ||
|
||
|
||
Pay attention to the way certain shells split words and parse input! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#Take the input for the array | ||
print "Enter the no of elements in the array "; | ||
chomp ( $size = <STDIN> ); | ||
print "Enter the values: "; | ||
for ( $i =0; $i <$size; ++$i){ | ||
my $num = <STDIN>; | ||
chomp $num; | ||
push @array, $num; | ||
} | ||
|
||
# prompt the user for a search key | ||
print "Enter an integer search key: "; | ||
chomp ( $searchKey = <STDIN> ); | ||
|
||
$found = 0; # $found is initially false | ||
|
||
for ( $i = 0; $i < @array && !$found; ++$i ) { | ||
|
||
if ( $array[ $i ] == $searchKey ) { | ||
$index = $i; | ||
$found = 1; | ||
} | ||
} | ||
|
||
if ( $found ) { # $found == 1 | ||
print "Found $searchKey at subscript $index \n"; | ||
} | ||
else { # $found == 0 | ||
print "$searchKey not found \n"; | ||
} |