diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..e43b0f988 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/BFS/BFS.cpp b/BFS/BFS.cpp new file mode 100644 index 000000000..0bf36e40e --- /dev/null +++ b/BFS/BFS.cpp @@ -0,0 +1,66 @@ +#include +#define ll long long + +using namespace std; +vector gr[1010]; + +void bfs(ll u,ll V,bool vis[]) +{ + + + queue q; + vis[u]=true; + q.push(u); + while(!q.empty()) + { + ll ver=q.front(); + q.pop(); + cout<>v>>e; + while(e--) + { + ll u,v; + cin>>u>>v; + gr[u].push_back(v); + gr[v].push_back(u); + + } + ll src; + cin>>src; + bfsMain(src,v); + return 0; +} +/* +8 9 +0 1 +0 2 +1 3 +2 4 +1 4 +3 4 +3 5 +4 5 +6 7 +0 +*/ diff --git a/bubble_sort/bubble_sort.cpp b/bubble_sort/bubble_sort.cpp new file mode 100644 index 000000000..259b3cf60 --- /dev/null +++ b/bubble_sort/bubble_sort.cpp @@ -0,0 +1,28 @@ +// Bubble Sort in C++ +#include +using namespace std; + +int main() { + + int array[] = {10, 6, 56, 45, 37, 7, 23}; // Array to sort + int size = sizeof(array) / sizeof(array[0]); + + while (true) { + bool sorted = true; + for (int i = 0; i < size; i++) { + if (array[i] > array[i + 1]) { + int temp = array[i]; + array[i] = array[i + 1]; + array[i + 1] = temp; + sorted = false; + } + } + if (sorted) break; + } + + // Printing sorted array + for (int i = 0; i < size; i++) + cout << array[i] << " "; + + return 0; +} diff --git a/bubble_sort/bubble_sort.py b/bubble_sort/bubble_sort.py new file mode 100644 index 000000000..0daba2ce1 --- /dev/null +++ b/bubble_sort/bubble_sort.py @@ -0,0 +1,15 @@ +'''Bubble Sort in Python''' + +def bubble_sort(alist): + for i in range(len(alist)-1,0,-1): + for j in range(i): + if alist[j]>alist[j+1]: + temp = alist[j] + alist[j] = alist[j+1] + alist[j+1] = temp + +if __name__ == '__main__': + #Sample data for sorting. + ALIST = [54, 26, 93, 17, 77, 31, 44, 55, 20] + bubble_sort(ALIST) + print(ALIST) diff --git a/count_the_age_person/count_the_age_person.cpp b/count_the_age_person/count_the_age_person.cpp new file mode 100644 index 000000000..47c29a294 --- /dev/null +++ b/count_the_age_person/count_the_age_person.cpp @@ -0,0 +1,30 @@ +using namespace std; + +#include + +int main() +{ + int BirthMonth, BirthYear; + int CurrentMonth, CurrentYear; + int AgeYear, AgeMonth; + + cout<<"\n\n\t\t\tCount the age person\n\n"; + cout<<"Enter Your Birth Year(Eg:1989):"; + cin>>BirthYear; + + cout<<"\n\nEnter Your Birth Month(Eg:7):"; + cin>>BirthMonth; + + cout<<"\nEnter The Current Month(Eg:7):"; + cin>>CurrentMonth; + + cout<<"\nEnter The Current Year(Eg:2010):"; + cin>>CurrentYear; + + AgeYear=CurrentYear-BirthYear; + AgeMonth=12-BirthMonth; + + cout<<"\n\n\t\tYour Age is "< +#define ll long long + +using namespace std; +vector gr[1010]; + +void bfs(ll u,ll V,bool vis[]) +{ + + + queue q; + vis[u]=true; + q.push(u); + while(!q.empty()) + { + ll ver=q.front(); + q.pop(); + cout<>v>>e; + while(e--) + { + ll u,v; + cin>>u>>v; + gr[u].push_back(v); + gr[v].push_back(u); + + } + ll src; + cin>>src; + bfsMain(src,v); + return 0; +} +/* +8 9 +0 1 +0 2 +1 3 +2 4 +1 4 +3 4 +3 5 +4 5 +6 7 +0 +*/ diff --git a/dfs/readme.txt b/dfs/readme.txt new file mode 100644 index 000000000..378153158 --- /dev/null +++ b/dfs/readme.txt @@ -0,0 +1,9 @@ +Depth First Traversal + +Depth First Traversal for a graph is similar to Depth First Traversal of a tree. +The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same node again. +To avoid processing a node more than once, we use a boolean visited array. + +The function dfsMain is used for cases when all vertices are not reachable from the starting vertex. + +Time Complexity: O(V+E) where V is number of vertices in the graph and E is number of edges in the graph. diff --git a/encryption_algorithms/RSA_algorithm.py b/encryption_algorithms/RSA_algorithm.py new file mode 100644 index 000000000..1d1156e8f --- /dev/null +++ b/encryption_algorithms/RSA_algorithm.py @@ -0,0 +1,88 @@ +#RSA encryption algorithm + +def main(): + n = e = d = 0 + while 1: + print(""" + 1. Set Public Key + 2. Encode + 3. Decode + 0. Quit + Your choice? """, end = "") + choice = int(input()) + if not choice: + return + if choice == 1: + n, e, d = set_keys() + if choice == 2: + if not n: + n = int(input("Public Key: ")) + e = int(input("e: ")) + encode(n, e) + if choice == 3: + if not d: + n, e, d = set_keys() + decode(d, n) + +def set_keys(): + """This fuction asks for 2 primes. + It sets a public key and an encoding number, 'e'.""" + p = int(input("prime(p): ")) + q = int(input("prime(q): ")) + n = p * q + m = (p - 1) * (q - 1) + e = get_e(m) + print("N = ", n, "\ne = ", e) + d = get_d(e, m) + while d < 0: + d += m + return [n, e, d] + +def encode(n, e): + """This function asks for a number and encodes it using 'n' and 'e'.""" + while 1: + c = int(input("Number to encode: ")) + if not c: + return + print(pow(c, e, n)) + +def decode(d, n): + """This function asks for a number and decodes it using 'd' and 'n'.""" + while 1: + c = int(input("Number to decode: ")) + if not c: + return + else: + print(pow(c, d, n)) + +def even(x): + """True if x is even.""" + return x % 2 == 0 + +def get_e(m): + """Finds an e coprime with m.""" + e = 2 + while gcd(e, m) != 1: + e += 1 + return e + +def gcd(a,b): + """Euclid's Algorithm: Takes two integers and returns gcd.""" + while b > 0: + a, b = b, a % b + return a + +def get_d(e, m): + """Takes encoding number, 'e' and the value for 'm' (p-1) * (q-1). + Returns a decoding number.""" + x = lasty = 0 + lastx = y = 1 + while m != 0: + q = e // m + e, m = m, e % m + x, lastx = lastx - q*x, x + y, lasty = lasty - q*y, y + return lastx + +if __name__ == "__main__": + main() diff --git a/fib/README.md b/fib/README.md deleted file mode 100644 index 096b5d67e..000000000 --- a/fib/README.md +++ /dev/null @@ -1 +0,0 @@ -The following c++ proram will take the nth term as input and will give the value of at that nth term as output. diff --git a/fibonacci/README.md b/fibonacci/README.md new file mode 100644 index 000000000..ec414cc5f --- /dev/null +++ b/fibonacci/README.md @@ -0,0 +1,17 @@ +## The Fibonacci Sequence +### Algorithm + +The Fibonacci Sequence is the series of numbers: + +`0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...` + +The next number is found by adding up the two numbers before it. + +* The 2 is found by adding the two numbers before it (1+1) +* The 3 is found by adding the two numbers before it (1+2), +* And the 5 is (2+3), +* and so on! + +### Output of the program +* `Enter the number of terms: 10` +* `Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,` diff --git a/fibonacci/fibonacci.c b/fibonacci/fibonacci.c new file mode 100644 index 000000000..5fe391b8e --- /dev/null +++ b/fibonacci/fibonacci.c @@ -0,0 +1,22 @@ +#include + int main() + { + int i, n, t1 = 0, t2 = 1, nextTerm; + + + printf("Enter the number of terms: "); + scanf("%d", &n); + + + printf("Fibonacci Series: "); + + + for (i = 1; i <= n; ++i) + { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + return 0; + } diff --git a/fib/fibonacci.cpp b/fibonacci/fibonacci.cpp similarity index 100% rename from fib/fibonacci.cpp rename to fibonacci/fibonacci.cpp diff --git a/fibonacci/fibonacci.go b/fibonacci/fibonacci.go new file mode 100644 index 000000000..a2c2104c4 --- /dev/null +++ b/fibonacci/fibonacci.go @@ -0,0 +1,26 @@ +package main + +import "fmt" + +func fibonacci() func() int { + a, b := 0, 1 + return func() int { + a = a + b + a, b = b, a + return a + } +} + +func main() { + var n int + fmt.Print("Enter value: ") + fmt.Scanf("%d", &n) + + f := fibonacci() + var fib int + for i := 0; i < n; i++ { + fib = f() + fmt.Printf("%d, ", fib) + } + fmt.Printf("\nfib(%d) = %d\n", n, fib) +} diff --git a/hello_world/hello_world.cpp b/hello_world/hello_world.cpp index d44063bf3..4017aaa69 100644 --- a/hello_world/hello_world.cpp +++ b/hello_world/hello_world.cpp @@ -2,7 +2,8 @@ using namespace std; -int main() { - cout << "Hello world!" << endl; +int main() +{ + cout << "Hello world!" << endl;//cout statement is used for printing a given output to the screen return 0; } diff --git a/hello_world/hello_world.java b/hello_world/hello_world.java index 339f675dc..9bc1603f8 100644 --- a/hello_world/hello_world.java +++ b/hello_world/hello_world.java @@ -1,7 +1,5 @@ -public class HelloWorld -{ - public static void main(String[] args) - { - System.out.println("Hello World!"); - } +public class HelloWorld { + public static void main(String[] args) { + System.out.println("Hello World!"); + } } diff --git a/hello_world_haskell/helloWorld.hs b/hello_world_haskell/helloWorld.hs deleted file mode 100644 index 942d2f479..000000000 --- a/hello_world_haskell/helloWorld.hs +++ /dev/null @@ -1 +0,0 @@ -main = print $ "Hello, world!" diff --git a/linear_search/linear_search.cpp b/linear_search/linear_search.cpp new file mode 100644 index 000000000..66ff4fc5e --- /dev/null +++ b/linear_search/linear_search.cpp @@ -0,0 +1,26 @@ +// Linear Search in C++ +#include +using namespace std; + +int main() { + + // Random data to search from + int searchArray[] = {10, 23, 89, 126, 51, 7, 45, 55}; + int size = sizeof(searchArray) / sizeof(searchArray[0]); + int search = 51; // Number to search + int index = -1; + + for (int i = 0; i < size; i++) { + if (searchArray[i] == search) { + index = i; + break; + } + } + + if (index >= 0) + cout << search << " found at index " << index << endl; + else + cout << search << " not found in the array" << endl; + + return 0; +} diff --git a/linked_list/linked_list.go b/linked_list/linked_list.go new file mode 100644 index 000000000..e837954fd --- /dev/null +++ b/linked_list/linked_list.go @@ -0,0 +1,53 @@ +package main + +import "fmt" + +type Node struct { + prev *Node + next *Node + key interface{} +} + +type LinkedList struct { + head *Node + tail *Node +} + +func (L *LinkedList) Insert(key interface{}) { + list := &Node{ + next: L.head, + key: key, + } + if L.head != nil { + L.head.prev = list + } + L.head = list + + l := L.head + for l.next != nil { + l = l.next + } + L.tail = l +} + +func (l *LinkedList) Show() { + list := l.head + for list != nil { + fmt.Printf("%+v", list.key) + if list.next != nil { + fmt.Printf(" --> ") + } + list = list.next + } + fmt.Println() +} + +func main() { + l := LinkedList{} + l.Insert(1) + l.Insert(2) + l.Insert(3) + l.Insert(4) + l.Insert(5) + l.Show() +} diff --git a/merge_sort.cpp b/merge_sort.cpp new file mode 100644 index 000000000..2993ae0b3 --- /dev/null +++ b/merge_sort.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +int a[100005]; +int n; + +void Merge(int a[], int l, int m, int r) { + int i1, i2, pos; + int n1 = m - l + 1; + int n2 = r - m; + int L[n1], R[n2]; + + for(int i = 0; i < n1; ++i) + L[i] = a[l + i]; + for(int i = 0; i < n2; ++i) + R[i] = a[m + 1 + i]; + + i1 = i2 = 0; // index of array 1 and 2 + pos = l; // index of megred array + while (i1 < n1 && i2 < n2) { + if (L[i1] <= R[i2]) a[pos] = L[i1++]; + else a[pos] = R[i2++]; + ++pos; + } + while (i1 < n1) a[pos] = L[i1++], ++pos; + while (i2 < n2) a[pos] = R[i2++], ++pos; +} + +void MergeSort(int a[], int l, int r) { + if (l < r) { + int m = (l + r) / 2; + + //Sort 2 parts + MergeSort(a, l, m); + MergeSort(a, m + 1, r); + Merge(a, l, m, r); + } +} + +int main() { + ios_base::sync_with_stdio(false); cin.tie(NULL); + cin >> n; + for(int i = 1; i <= n; ++i) + cin >> a[i]; + + MergeSort(a, 1, n); + + for(int i = 1; i <= n; ++i) + cout << a[i] << '\n'; + return 0; +}