Skip to content

Commit f40ff20

Browse files
authored
Merge branch 'master' into master
2 parents fa8d548 + 3f72398 commit f40ff20

9 files changed

Lines changed: 181 additions & 31 deletions

File tree

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
/*Binary Search-Search a sorted array by repeatedly dividing the search interval
2-
* in half. Begin with an interval covering the whole array. If the value of the
3-
* search key is less than the item in the middle of the interval, narrow the interval
4-
* to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the
5-
* value is found or the interval is empty.
6-
*/
7-
8-
function binarySearch(arr, i) {
9-
var mid = Math.floor(arr.length / 2);
10-
if (arr[mid] === i) {
11-
console.log('match', arr[mid], i);
12-
return arr[mid];
13-
} else if (arr[mid] < i && arr.length > 1) {
14-
binarySearch(arr.splice(mid, Number.MAX_VALUE), i);
15-
} else if (arr[mid] > i && arr.length > 1) {
16-
binarySearch(arr.splice(0, mid), i);
17-
} else {
18-
console.log('not found', i);
19-
return -1;
20-
}
1+
/**
2+
* Search a value into a sorted array by repeatedly dividing the search interval in half.
3+
* @param {Array} arr Array to search into
4+
* @param {Number} k Value to search
5+
* @return {Number} index of found item or -1 for not found
6+
*/
7+
function binarySearch(arr, k) {
8+
let min = 0
9+
let max = arr.length - 1
10+
11+
while (min <= max) {
12+
const cur = Math.floor((min + max) / 2)
13+
if (arr[cur] === k) { return cur }
14+
(arr[cur] > k)
15+
? max = cur - 1
16+
: min = cur + 1
17+
}
2118

19+
return -1
2220
}
2321

24-
var ar=[1,2,3,4,5,6,7,8,9,10];
25-
binarySearch(ar,3);
26-
binarySearch(ar,7);
27-
binarySearch(ar,13);
22+
const arr = [1,2,3,4,5,6,7,8,9,10]
23+
console.log(binarySearch(arr, 6)) // 5
24+
console.log(binarySearch(arr, 9)) // 8
25+
console.log(binarySearch(arr, 2)) // 1
26+
console.log(binarySearch(arr, 7)) // 6
27+
console.log(binarySearch(arr, 11)) // -1
28+
29+
console.log(binarySearch(arr, 3)) // 2
30+
console.log(binarySearch(arr, 3)) // 2
31+
console.log(binarySearch(arr, 3)) // 2

CONTRIBUTING.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,7 @@ Unfortunately, sometimes the bug can be only reproduced in your project or in yo
147147
- [BurnzZ](https://github.com/BurnzZ)
148148
- [FernandaOchoa](https://github.com/FernandaOchoa)
149149
- [npcoder2k14](https://github.com/npcoder2k14)
150-
- [Jaernbrand](https://github.com/Jaernbrand)
150+
- [Jaernbrand](https://github.com/Jaernbrand)
151+
- [DiegoVicen](https://github.com/DiegoVicen)
152+
- [Ashwin-Kapes](https://github.com/Ashwin-Kapes)
153+
- [Judar Lima](https://github.com/judarlima)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class FibonacciRecursion{
2+
static int a = 0,b = 1,c = 0;
3+
static void printFibonacci(int count){
4+
if(count > 0){
5+
c = a + b;
6+
a = b;
7+
b = c;
8+
System.out.print(" " + c);
9+
printFibonacci(count - 1);
10+
}
11+
}
12+
public static void main(String args[]){
13+
int count = 10;
14+
System.out.print(a + " " + b);
15+
printFibonacci(count - 2);
16+
}
17+
}

Fibonacci/Swift/Fibonacci.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//Recursive algorithm
2+
func fibonacci(_ n: Int) -> Int {
3+
guard n != 0, n != 1 else { return n }
4+
return fibonacci(n - 1) + fibonacci(n - 2)
5+
}

LinearSearch/Go/linear_search.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package search
2+
3+
// LinearSearch returns the index of the given key found on the list.
4+
// It returns a value of -1 if the key doesn't exist
5+
func LinearSearch(list []int, key int) int {
6+
for index, element := range list {
7+
if key == element {
8+
return index
9+
}
10+
}
11+
12+
return -1
13+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package search
2+
3+
import "testing"
4+
5+
func TestLinearSearch(t *testing.T) {
6+
var tests = []struct {
7+
input []int
8+
key int
9+
output int
10+
}{
11+
{
12+
input: []int{1, 2, 3, 4, 5},
13+
key: 3,
14+
output: 2,
15+
},
16+
{
17+
input: []int{-1, 0, 100, 33, 44},
18+
key: -2,
19+
output: -1,
20+
},
21+
{
22+
input: []int{},
23+
output: -1,
24+
},
25+
}
26+
27+
for _, test := range tests {
28+
result := LinearSearch(test.input, test.key)
29+
30+
if result != test.output {
31+
t.Errorf("LinearSearch(%v, %v) => %v, want %v",
32+
test.input, test.key, result, test.output)
33+
}
34+
}
35+
}

QuickSort/Rust/quicksort.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
mod sort {
2+
3+
pub fn quicksort<T: Ord>(arr: &mut [T]) {
4+
let length = arr.len();
5+
quicksort_step(arr, 0, (length as isize) - 1);
6+
}
7+
8+
9+
fn quicksort_step<T: Ord>(arr: &mut [T], lo: isize, hi: isize) {
10+
if lo < hi {
11+
let pivot = lomuto_partiton(arr, lo, hi);
12+
quicksort_step(arr, lo, pivot - 1);
13+
quicksort_step(arr, pivot + 1, hi);
14+
}
15+
}
16+
17+
fn lomuto_partiton<T: Ord>(arr: &mut [T], lo: isize, hi: isize) -> isize {
18+
let mut i = lo - 1;
19+
let mut j = lo;
20+
21+
while j < hi - 1 {
22+
if arr[j as usize] < arr[hi as usize] {
23+
i = i + 1;
24+
arr.swap(i as usize, j as usize);
25+
}
26+
j = j + 1;
27+
}
28+
29+
if arr[hi as usize] < arr[(i + 1) as usize] {
30+
arr.swap(hi as usize, (i + 1) as usize);
31+
}
32+
33+
return i + 1;
34+
}
35+
}
36+
37+
fn main() {
38+
let mut arr = [3, 7, 8, 5, 2, 1, 9, 5, 4];
39+
40+
sort::quicksort(&mut arr);
41+
42+
println!("{:?}", arr);
43+
}

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Algorithms Example
1+
# Algorithms Example
22

33
This repository contains examples of various algorithms which were written on different programming languages.
44

@@ -25,20 +25,20 @@ EditDistance | | :+1: | | | :+1: | | | |
2525
Edmonds-Karp | :+1: | | | | | | | |
2626
ElevatorAlgorithm | :+1: | | | | | | | |
2727
Fast Fourier Transform | | | | | :+1: | | | |
28-
Fibonacci | :+1: | :+1: | | :+1: | :+1: | :+1: | | :+1: | :+1: | |
28+
Fibonacci | :+1: | :+1: | | :+1: | :+1: | :+1: | | :+1: | :+1: | :+1: |
2929
FisherYatesShuffle | :+1: | | | | :+1: | :+1: | | :+1: |
3030
FloodFill Algorithm | :+1: | | | | | | | |
3131
Floyd'sAlgorithm | :+1: | :+1: | | | :+1: | | | |
3232
GreatestCommonDivisor | :+1: |:+1:| :+1: | :+1: | :+1: | | | |
3333
HammingDistance | :+1: | :+1: | | :+1: | | :+1: | :+1: | |
3434
HeapSort | :+1: | :+1: | | | :+1: | :+1: | :+1: | | :+1:
3535
HistogramEqualization | :+1: | | | | | | | |
36-
InsertionSort | :+1: | :+1: | :+1: | :+1: | :+1: | | :+1: | :+1: | :+1:
36+
InsertionSort | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1:
3737
Inverse Fast Fourier Transform | | | | | :+1: | | | |
3838
Johnson algorithm | :+1: | :+1: | | | :+1: | | | |
3939
Kadane's algorithm | :+1: | :+1: | | :+1: | :+1: | :+1: | :+1: | |
4040
Knuth Morris Prath Algorithm | :+1: | :+1: | | | :+1: | | | |
41-
LinearSearch | :+1: | :+1: | | :+1:| :+1: | :+1: | | | | :+1: |
41+
LinearSearch | :+1: | :+1: | | :+1:| :+1: | :+1: | :+1: | | | :+1: |
4242
Longest-Common-Subsequence | :+1: | :+1: | | :+1: | :+1: | | | | :+1:
4343
Longest-Increasing-Subsequence | :+1: | :+1: | | | :+1: | | | |
4444
LongestPath | | | | | :+1: | | | |
@@ -47,7 +47,7 @@ MiniMaxWithABPruning | :+1: | | | | | | | |
4747
Modified_Binary_Search | | | | :+1: | | | | |
4848
Pearson Hashing | :+1: | | | | | | | |
4949
Postman Sort | | | | :+1: | | | | |
50-
Quick Sort | :+1: | :+1: | | | | :+1: | :+1: | :+1: | :+1: | :+1: |
50+
Quick Sort | :+1: | :+1: | :+1: | | | :+1: | :+1: | :+1: | :+1: | :+1: |
5151
Quick Select | :+1: | :+1: | | :+1: | | | :+1: | |
5252
Uniform-cost search | :+1: | | | | | :+1: | :+1: | |
5353
RadixSort | :+1: | :+1: | | | :+1: | | | |
@@ -61,6 +61,7 @@ TernarySearch | :+1: |:+1: | | :+1: | :+1: | | | |
6161
Topological Sort | | | | | :+1: | | | |
6262
Segmented Sieve |:+1:| :+1: | | | :+1: | | | |
6363
Union Find |:+1:|:+1:| | :+1: | | | | |
64+
Xor swap |:+1:| | | | | | | |
6465

6566

6667
### List of Algorithms :
@@ -769,7 +770,7 @@ Union Find |:+1:|:+1:| | :+1: | | | | |
769770

770771
* Xiaolin Wu's line algorithm : algorithm for line antialiasing.
771772

772-
* Xor swap algorithm : swaps the values of two variables without using a buffer
773+
* [Xor swap algorithm](XorSwap) : swaps the values of two variables without using a buffer
773774

774775
* Yamartino method : calculate an approximation to the standard deviation σθ of wind direction θ during a single pass through the incoming data
775776

@@ -781,6 +782,8 @@ Union Find |:+1:|:+1:| | :+1: | | | | |
781782

782783
* Union Find : used to know if there is a path between 2 objects or not
783784

785+
* Fibonacci Recursive : Fibonacci series printed using Java Recursion
786+
784787
Folder structure should be
785788
[**Algorithm name**]/[**language**]/**file**
786789

XorSwap/Java/XorSwap.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* XOR swap is an algorithm that uses the XOR bitwise operation to swap
3+
* values of distinct variables without using a temporary variable.
4+
*
5+
* @author Atom
6+
* @see <a href="https://en.wikipedia.org/wiki/XOR_swap_algorithm">XOR swap</a>
7+
*/
8+
public class XorSwap {
9+
10+
public static void main(String[] args) {
11+
for (int i = -1, j = 3; i <= 3; i++, j--) {
12+
int x = i;
13+
int y = j;
14+
System.out.print("x = " + x + ", y = " + y);
15+
16+
// Xor swap. Swap values without using a temporary variable
17+
if (x != y) {
18+
x ^= y;
19+
y ^= x;
20+
x ^= y;
21+
}
22+
23+
System.out.println(", swap(x, y) -> x = " + x + ", y = " + y);
24+
}
25+
}
26+
27+
}

0 commit comments

Comments
 (0)