├── C# ├── .gitignore ├── EvenNumbers.cs ├── fibonacci.cs ├── Selection_Sort.cs ├── Bubble_sort.cs ├── selectionsort.cs ├── InsertionSort.cs ├── MergeSort.cs └── QuickSort.cs ├── C++ ├── .gitignore ├── OddNumbers.cpp ├── EvenNumbers.cpp ├── Fibonacci Series.cpp ├── Fibonacciseries.cpp ├── fibonacci.cpp ├── fibonacciSeries.cpp ├── binary_search.cpp ├── SelectionSort.cpp ├── InsertionSort.cpp ├── Selection_sort.cpp ├── bubbleSort.cpp ├── Insertion_Sort.cpp ├── Search_in_sorted_array.cpp ├── Dfs.cpp ├── dfs.cpp ├── Bubble_sort.cpp ├── quicksort.cpp ├── AllSearchingAlgorithm.cpp ├── water_conection.cpp ├── MergeSort.cpp └── BST.cpp ├── Java ├── .gitignore ├── FibonacciRecursive.java ├── StringToByte.java ├── Kadane's_Algorithm.java ├── QuickSort.java ├── SelectionSort.java ├── InsertionSort.java ├── BubbleSort.java ├── Insertion_Sort.java ├── Bubble_sort.java ├── mergeSort.java ├── trapping_rainwater_problem.java └── MergeSort.java ├── Python ├── .gitignore ├── fibonnaci_sequence.py ├── Fibonacci.py ├── Bubble_sort.py ├── Selection_Sort.py ├── HeapSort.py ├── InsertionSort.py ├── MergeSort.py ├── QuickSort.py └── games │ └── PaddleBall.py ├── GoLang ├── .gitkeep └── Fibonacci algorithm ♾ in Go programming language ├── JavaScript ├── .gitignore ├── fibonacci.js ├── insertionsort.js ├── QuickSort.js ├── verifyParams.js ├── Bubble_sort.js └── Selection_Sort.js ├── _doc ├── fork.jpg ├── cover.jpg └── cover2023.png ├── PHP ├── fibonacci.php └── mergesort.php ├── C ├── Fibonacci.c ├── FibonacciAlgorithm.c ├── PrimeNumbers.c ├── SelectionSort.c └── Golbatch.c ├── InsertionSort.java ├── BubbleSort.java ├── LICENSE ├── MergeSort.java ├── quickSort.java ├── SelectionSort.java ├── README.md └── .gitignore /C#/.gitignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /C++/.gitignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Java/.gitignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/.gitignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /GoLang/.gitkeep: -------------------------------------------------------------------------------- 1 | G 2 | -------------------------------------------------------------------------------- /JavaScript/.gitignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /_doc/fork.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DevF3st/Learn-Programming/HEAD/_doc/fork.jpg -------------------------------------------------------------------------------- /_doc/cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DevF3st/Learn-Programming/HEAD/_doc/cover.jpg -------------------------------------------------------------------------------- /_doc/cover2023.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DevF3st/Learn-Programming/HEAD/_doc/cover2023.png -------------------------------------------------------------------------------- /Python/fibonnaci_sequence.py: -------------------------------------------------------------------------------- 1 | def f(n): 2 | if(n==0): 3 | return 0 4 | elif(n==1): 5 | return 1 6 | else: 7 | return f(n-1)+ f(n-2) 8 | a=int(input('Enter the number:')) 9 | print (f(a)) -------------------------------------------------------------------------------- /C#/EvenNumbers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | class Program { 3 | static void Main() { 4 | int val; 5 | for(val = 1; val <= 100; val++) { 6 | 7 | if(val%2 == 0) { 8 | Console.WriteLine(val); 9 | } 10 | } 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /Python/Fibonacci.py: -------------------------------------------------------------------------------- 1 | def Fibonacci(n): 2 | if n < 0: 3 | print("Incorrect input") 4 | elif n == 0: 5 | return 0 6 | elif n == 1 or n == 2: 7 | return 1 8 | else: 9 | return Fibonacci(n-1) + Fibonacci(n-2) 10 | 11 | print(Fibonacci(9)) -------------------------------------------------------------------------------- /JavaScript/fibonacci.js: -------------------------------------------------------------------------------- 1 | const number = parseInt(prompt('Enter the number of terms: ')); 2 | let n1 = 0, n2 = 1, nextTerm; 3 | 4 | console.log('Fibonacci Series:'); 5 | 6 | for (let i = 1; i <= number; i++) { 7 | console.log(n1); 8 | nextTerm = n1 + n2; 9 | n1 = n2; 10 | n2 = nextTerm; 11 | } 12 | -------------------------------------------------------------------------------- /C++/OddNumbers.cpp: -------------------------------------------------------------------------------- 1 | //Odd Numbers From 1 To 100 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int i; 7 | 8 | cout << "Odd numbers between 1 to 100 : " << endl; 9 | for (i = 1; i <= 100; i++){ 10 | if (i % 2 != 0) 11 | cout << i << " "; 12 | } 13 | return 0; 14 | } -------------------------------------------------------------------------------- /PHP/fibonacci.php: -------------------------------------------------------------------------------- 1 | Fibonacci series for first 10 numbers: "; 6 | echo "\n"; 7 | echo $n1.' '.$n2.' '; 8 | while ($num < 8 ) 9 | { 10 | $n3 = $n2 + $n1; 11 | echo $n3.' '; 12 | $n1 = $n2; 13 | $n2 = $n3; 14 | $num = $num + 1; 15 | ?> 16 | -------------------------------------------------------------------------------- /JavaScript/insertionsort.js: -------------------------------------------------------------------------------- 1 | function insertionSort(arr) { 2 | for (let i = 1; i < arr.length; i++) { 3 | let currentValue = arr[i] 4 | let j 5 | for (j = i - 1; j >= 0 && arr[j] > currentValue; j--) { 6 | arr[j + 1] = arr[j] 7 | } 8 | arr[j + 1] = currentValue 9 | } 10 | return arr 11 | } 12 | console.log(insertionSort([2, 1, 3, 7, 5])) // [1, 2, 3, 5, 7 13 | -------------------------------------------------------------------------------- /C/Fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, i, t1 = 0, t2 = 1, nextTerm; 5 | 6 | printf("Enter the number of terms: "); 7 | scanf("%d", &n); 8 | 9 | printf("Fibonacci Series: "); 10 | 11 | for (i = 1; i <= n; ++i) { 12 | printf("%d, ", t1); 13 | nextTerm = t1 + t2; 14 | t1 = t2; 15 | t2 = nextTerm; 16 | } 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /C++/EvenNumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | /* Initialize i with 1. */ 7 | 8 | int i=1; 9 | 10 | /* If i is less than or equal to 100. */ 11 | 12 | while( i <= 100){ 13 | 14 | /* If number is divisible by 2, then print.*/ 15 | 16 | if(i % 2 == 0){ 17 | 18 | cout < 2 | using namespace std; 3 | 4 | int fibonacci(int num) 5 | { 6 | if(num==0) 7 | { 8 | return 0; 9 | } 10 | if(num==1) 11 | { 12 | return 1; 13 | } 14 | return (fibonacci(num-1)+fibonacci(num-2)); 15 | } 16 | 17 | int main() 18 | { 19 | int num; 20 | cout<<"Enter the number : "; 21 | cin>>num; 22 | cout<<"The fibonacci number is: "<list1[j+1]): 8 | temp = list1[j] 9 | list1[j] = list1[j+1] 10 | list1[j+1] = temp 11 | return list1 12 | 13 | list1 = [5, 3, 8, 6, 7, 2] 14 | print("The unsorted list is: ", list1) 15 | # Calling the bubble sort function 16 | print("The sorted list is: ", bubble_sort(list1)) 17 | -------------------------------------------------------------------------------- /C++/Fibonacciseries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, first = 0, second = 1, next; 5 | 6 | std::cout << "Enter the number of terms you want in the Fibonacci series: "; 7 | std::cin >> n; 8 | 9 | std::cout << "Fibonacci Series: "; 10 | 11 | for (int i = 0; i < n; ++i) { 12 | if (i <= 1) 13 | next = i; 14 | else { 15 | next = first + second; 16 | first = second; 17 | second = next; 18 | } 19 | std::cout << next << " "; 20 | } 21 | 22 | std::cout << std::endl; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Python/Selection_Sort.py: -------------------------------------------------------------------------------- 1 | # Python program for implementation of Selection 2 | # Sort 3 | import sys 4 | A = [64, 25, 12, 22, 11] 5 | 6 | # Traverse through all array elements 7 | for i in range(len(A)): 8 | 9 | # Find the minimum element in remaining 10 | # unsorted array 11 | min_idx = i 12 | for j in range(i+1, len(A)): 13 | if A[min_idx] > A[j]: 14 | min_idx = j 15 | 16 | # Swap the found minimum element with 17 | # the first element 18 | A[i], A[min_idx] = A[min_idx], A[i] 19 | 20 | # Driver code to test above 21 | print ("Sorted array") 22 | for i in range(len(A)): 23 | print("%d" %A[i],end=" ") 24 | -------------------------------------------------------------------------------- /JavaScript/verifyParams.js: -------------------------------------------------------------------------------- 1 | verifyParams(requiredParams = [], comparedObject = {}) { 2 | let response = {}; 3 | response.success = true; 4 | response.message = "No issues found"; 5 | response.statusCode = 200; 6 | response.data = [] 7 | let paramsNotFound = this.findMissingFields(comparedObject, requiredParams); 8 | if (paramsNotFound.length > 0) { 9 | response.success = false; 10 | response.message = "Some objects missing"; 11 | response.statusCode = 422; 12 | response.data = paramsNotFound 13 | } 14 | return response; 15 | } 16 | -------------------------------------------------------------------------------- /C#/fibonacci.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | public class FibonacciExample 3 | { 4 | public static void Main(string[] args) 5 | { 6 | int n1=0,n2=1,n3,i,number; 7 | Console.Write("Enter the number of elements: "); 8 | number = int.Parse(Console.ReadLine()); 9 | Console.Write(n1+" "+n2+" "); //printing 0 and 1 10 | for(i=2;i=0 && arr[j] > temp) { 14 | arr[j + 1] = arr[j]; 15 | j--; 16 | } 17 | arr[j+1] = temp; 18 | } 19 | } 20 | 21 | public static void main(String[] args) { 22 | int arr[] = {9,7,6,8,4,5,2,3}; 23 | insertionSort(arr); 24 | printArray(arr); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /C++/fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, t1 = 0, t2 = 1, nextTerm = 0; 6 | 7 | cout << "Enter the number of terms: "; 8 | cin >> n; 9 | 10 | cout << "Fibonacci Series: "; 11 | 12 | for (int i = 1; i <= n; ++i) { 13 | // Prints the first two terms. 14 | if(i == 1) { 15 | cout << t1 << ", "; 16 | continue; 17 | } 18 | if(i == 2) { 19 | cout << t2 << ", "; 20 | continue; 21 | } 22 | nextTerm = t1 + t2; 23 | t1 = t2; 24 | t2 = nextTerm; 25 | 26 | cout << nextTerm << ", "; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /GoLang/Fibonacci algorithm ♾ in Go programming language: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func fibonacci(n int) []int { 6 | fib := make([]int, n) 7 | fib[0], fib[1] = 0, 1 8 | 9 | for i := 2; i < n; i++ { 10 | fib[i] = fib[i-1] + fib[i-2] 11 | } 12 | 13 | return fib 14 | } 15 | 16 | func main() { 17 | n := 10 // Change this value to generate a different number of Fibonacci numbers 18 | 19 | if n <= 0 { 20 | fmt.Println("Invalid input. Please provide a positive integer.") 21 | return 22 | } 23 | 24 | fmt.Printf("First %d Fibonacci numbers:\n", n) 25 | fib := fibonacci(n) 26 | for _, num := range fib { 27 | fmt.Printf("%d ", num) 28 | } 29 | fmt.Println() 30 | } 31 | -------------------------------------------------------------------------------- /BubbleSort.java: -------------------------------------------------------------------------------- 1 | public class BubbleSort { 2 | public static void printArray(int arr[]) { 3 | int n = arr.length; 4 | for(int i = 0; i < n; i++) { 5 | System.out.println(arr[i]); 6 | } 7 | } 8 | 9 | public static void bubbleSort(int[] arr) { 10 | int n = arr.length; 11 | for(int i = 0; i < n-1; i++) { // this loop for (n-1)round 12 | for(int j = 0; j < n-1-i; j++) { // this loop is for (n-1) largest number[till n-2 index] 13 | if(arr[j] > arr[j+1]) { 14 | int temp = arr[j]; 15 | arr[j] = arr[j+1]; 16 | arr[j+1] = temp; 17 | } 18 | } 19 | 20 | } 21 | } 22 | 23 | public static void main(String[] args) { 24 | int[] arr = {3,4,6,1,5,7,8}; 25 | bubbleSort(arr); 26 | printArray(arr); 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /JavaScript/Bubble_sort.js: -------------------------------------------------------------------------------- 1 | BUBBLE SORT CODE : 2 | 3 | function bubbleSort(array){ 4 | 5 | for(var i = 0; i <= array.length-1; i++){ 6 | // Last i elements are already in place 7 | for(var j = 0; j < ( array.length - i -1); j++){ 8 | 9 | // Comparing two adjacent numbers 10 | // and see if first is greater than second 11 | if(array[j] > array[j+1]){ 12 | 13 | // Swap them if the condition is true 14 | var temp = array[j] 15 | array[j] = array[j + 1] 16 | array[j+1] = temp 17 | } 18 | } 19 | } 20 | // Print the sorted array 21 | console.log(array); 22 | } 23 | 24 | var array = [23, 43, 12, 56, 35]; 25 | bubbleSort(array); 26 | -------------------------------------------------------------------------------- /Java/Kadane's_Algorithm.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class Kadane { 5 | // Driver Code 6 | public static void main(String[] args) 7 | { 8 | int[] a = { -2, -3, 4, -1, -2, 1, 5, -3 }; 9 | System.out.println("Maximum contiguous sum is " 10 | + maxSubArraySum(a)); 11 | } 12 | 13 | // Function Call 14 | static int maxSubArraySum(int a[]) 15 | { 16 | int size = a.length; 17 | int max_so_far = Integer.MIN_VALUE, max_ending_here 18 | = 0; 19 | 20 | for (int i = 0; i < size; i++) { 21 | max_ending_here = max_ending_here + a[i]; 22 | if (max_so_far < max_ending_here) 23 | max_so_far = max_ending_here; 24 | if (max_ending_here < 0) 25 | max_ending_here = 0; 26 | } 27 | return max_so_far; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /C++/fibonacciSeries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void generateFibonacci(int n) 4 | { 5 | long long int first = 0, second = 1, next; 6 | 7 | cout << "Fibonacci Series up to " << n << " terms: "; 8 | for (int i = 0; i < n; ++i) 9 | { 10 | if (i <= 1) 11 | { 12 | next = i; 13 | } 14 | else 15 | { 16 | next = first + second; 17 | first = second; 18 | second = next; 19 | } 20 | cout << next << " "; 21 | } 22 | 23 | cout << endl; 24 | } 25 | 26 | int main() 27 | { 28 | int n; 29 | cout << "Enter the number of terms for the Fibonacci series: "; 30 | cin >> n; 31 | 32 | generateFibonacci(n); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Python/HeapSort.py: -------------------------------------------------------------------------------- 1 | def heapify(arr, N, i): 2 | largest = i 3 | l = 2 * i + 1 4 | r = 2 * i + 2 5 | 6 | if l < N and arr[largest] < arr[l]: 7 | largest = l 8 | 9 | if r < N and arr[largest] < arr[r]: 10 | largest = r 11 | 12 | if largest != i: 13 | arr[i], arr[largest] = arr[largest], arr[i] 14 | 15 | heapify(arr, N, largest) 16 | 17 | def heapSort(arr): 18 | N = len(arr) 19 | 20 | for i in range(N//2 - 1, -1, -1): 21 | heapify(arr, N, i) 22 | 23 | for i in range(N-1, 0, -1): 24 | arr[i], arr[0] = arr[0], arr[i] # swap 25 | heapify(arr, i, 0) 26 | 27 | if __name__ == '__main__': 28 | arr = [12, 11, 13, 5, 6, 7] 29 | 30 | heapSort(arr) 31 | N = len(arr) 32 | 33 | print("Sorted array is") 34 | for i in range(N): 35 | print("%d" % arr[i], end=" ") 36 | -------------------------------------------------------------------------------- /Python/InsertionSort.py: -------------------------------------------------------------------------------- 1 | def insertionSort(arr): 2 | 3 | # Traverse through 1 to len(arr) 4 | for i in range(1, len(arr)): 5 | 6 | key = arr[i] 7 | 8 | # Move elements of arr[0..i-1], that are 9 | # greater than key, to one position ahead 10 | # of their current position 11 | j = i-1 12 | while j >=0 and key < arr[j] : 13 | arr[j+1] = arr[j] 14 | j -= 1 15 | arr[j+1] = key 16 | 17 | 18 | #sorting the array [12, 11, 13, 5, 6] using insertionSort 19 | arr = [12, 11, 13, 5, 6] 20 | insertionSort(arr) 21 | lst = [] #empty list to store sorted elements 22 | print("Sorted array is : ") 23 | for i in range(len(arr)): 24 | lst.append(arr[i]) #appending the elements in sorted order 25 | print(lst) 26 | -------------------------------------------------------------------------------- /Python/MergeSort.py: -------------------------------------------------------------------------------- 1 | def mergeSort(arr): 2 | if len(arr) > 1: 3 | mid = len(arr)//2 4 | L = arr[:mid] 5 | R = arr[mid:] 6 | mergeSort(L) 7 | mergeSort(R) 8 | i = j = k = 0 9 | 10 | while i < len(L) and j < len(R): 11 | if L[i] <= R[j]: 12 | arr[k] = L[i] 13 | i += 1 14 | else: 15 | arr[k] = R[j] 16 | j += 1 17 | k += 1 18 | 19 | while i < len(L): 20 | arr[k] = L[i] 21 | i += 1 22 | k += 1 23 | 24 | while j < len(R): 25 | arr[k] = R[j] 26 | j += 1 27 | k += 1 28 | 29 | def printList(arr): 30 | for i in range(len(arr)): 31 | print(arr[i], end=" ") 32 | print() 33 | 34 | if __name__ == '__main__': 35 | arr = [12, 11, 13, 5, 6, 7] 36 | print("Given array is", end="\n") 37 | printList(arr) 38 | mergeSort(arr) 39 | print("Sorted array is: ", end="\n") 40 | printList(arr) 41 | -------------------------------------------------------------------------------- /C/PrimeNumbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int checkPrime(int num) 4 | { 5 | // 0, 1 and negative numbers are not primes 6 | if(num < 2){ 7 | return 0; 8 | } 9 | else{ 10 | // no need to run loop till num-1 as for any number x the numbers in 11 | // the range(num/2 + 1, num) won't be divisible anyways. 12 | // Example 36 wont be divisible by anything b/w 19-35 13 | int x = num/2; 14 | for(int i = 2; i <=x; i++) 15 | { 16 | if(num % i == 0) 17 | { 18 | return 0; 19 | } 20 | } 21 | } 22 | // the number would be prime if we reach here 23 | return 1; 24 | } 25 | 26 | int main() 27 | { 28 | int a = 1, b = 100; 29 | 30 | for(int i=a; i <= b; i++){ 31 | if(checkPrime(i)) 32 | printf("%d ",i); 33 | } 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /C/SelectionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int array[100], n, c, d, position, t; 5 | //taking input 6 | printf("Enter number of elements\n"); 7 | scanf("%d", &n); 8 | printf("Enter %d integers\n", n); 9 | for (c = 0; c < n; c++) 10 | scanf("%d", &array[c]); 11 | 12 | for (c = 0; c < (n - 1); c++) 13 | // finding minimum element (n-1) times 14 | { 15 | position = c; 16 | //assuming smallest is at cth position 17 | for (d = c + 1; d < n; d++) 18 | { 19 | if (array[position] > array[d]) 20 | position = d; 21 | } 22 | if (position != c) 23 | //if assumption was wrong then swap it with correct smallest element 24 | { 25 | t = array[c]; 26 | array[c] = array[position]; 27 | array[position] = t; 28 | } 29 | } 30 | 31 | printf("Sorted list in ascending order:\n"); 32 | //printing sorted list 33 | for (c = 0; c < n; c++) 34 | printf("%d\n", array[c]); 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /JavaScript/Selection_Sort.js: -------------------------------------------------------------------------------- 1 | 43 | -------------------------------------------------------------------------------- /C#/Selection_Sort.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class GFG 4 | { 5 | static void sort(int []arr) 6 | { 7 | int n = arr.Length; 8 | 9 | // One by one move boundary of unsorted subarray 10 | for (int i = 0; i < n - 1; i++) 11 | { 12 | // Find the minimum element in unsorted array 13 | int min_idx = i; 14 | for (int j = i + 1; j < n; j++) 15 | if (arr[j] < arr[min_idx]) 16 | min_idx = j; 17 | 18 | // Swap the found minimum element with the first 19 | // element 20 | int temp = arr[min_idx]; 21 | arr[min_idx] = arr[i]; 22 | arr[i] = temp; 23 | } 24 | } 25 | 26 | // Prints the array 27 | static void printArray(int []arr) 28 | { 29 | int n = arr.Length; 30 | for (int i=0; i 2 | using namespace std; 3 | 4 | int BinarySearch(int arr[], int size, int key){ 5 | int start = 0; 6 | int end = size - 1; 7 | 8 | int mid = start + (end - start)/2; 9 | 10 | while(start <= end){ 11 | if(arr[mid] == key){ 12 | return mid; 13 | } 14 | // go to right side 15 | else if(key > arr[mid]){ 16 | start = mid + 1; 17 | } 18 | // go to left side then 19 | else 20 | { 21 | end = mid - 1; 22 | } 23 | mid = start + (end - start)/2; 24 | } 25 | return -1; 26 | } 27 | int main() 28 | { 29 | int even[6] = {1, 2, 3, 4, 5, 6}; 30 | int odd[5] = {12, 13, 14, 15, 16}; 31 | 32 | int evenIndex = BinarySearch(even, 6, 6); 33 | cout << "Index of 6 is : " << evenIndex << endl; 34 | 35 | int oddIndex = BinarySearch(odd, 5, 11); 36 | cout << "Index of 11 is : " << oddIndex << endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Java/QuickSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class QuickSort { 4 | public static void main(String[] args) { 5 | int[] arr ={5,4,3,2,1}; 6 | quickSort(arr , 0 ,arr.length-1); 7 | System.out.println(Arrays.toString(arr)); 8 | } 9 | 10 | public static void quickSort(int[] arr, int low, int high) { 11 | if(low >= high ){ 12 | return; 13 | } 14 | int s = low; 15 | int e =high; 16 | 17 | int mid = s+(e-s)/2; 18 | 19 | while(s<= e){ 20 | while(arr[s]arr[mid]){ 24 | e--; 25 | } 26 | if(s <= e){ 27 | int temp = arr[s]; 28 | arr[s] =arr[e]; 29 | arr[e] =temp; 30 | s++; 31 | e--; 32 | } 33 | } 34 | quickSort(arr,low,e); 35 | quickSort(arr,s,high); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /C#/Bubble_sort.cs: -------------------------------------------------------------------------------- 1 | BUBBLE SORT CODE : 2 | 3 | using System; 4 | namespace BubbleSort { 5 | class MySort { 6 | static void Main(string[] args) { 7 | Console.Write("Enter the total number of elements : "); 8 | int total=Convert.ToInt32(Console.ReadLine()); 9 | int[] arr = new int[total]; 10 | for(int i=0; i arr[i + 1]) { 19 | temp= arr[i + 1]; 20 | arr[i + 1] = arr[i]; 21 | arr[i] = temp; 22 | } 23 | } 24 | } 25 | Console.WriteLine("Sorted:"); 26 | foreach (int p in arr) 27 | Console.Write(p + " "); 28 | Console.Read(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /C#/selectionsort.cs: -------------------------------------------------------------------------------- 1 | // C# program for implementation 2 | // of Selection Sort 3 | using System; 4 | 5 | class GFG 6 | { 7 | static void sort(int []arr) 8 | { 9 | int n = arr.Length; 10 | 11 | // One by one move boundary of unsorted subarray 12 | for (int i = 0; i < n - 1; i++) 13 | { 14 | // Find the minimum element in unsorted array 15 | int min_idx = i; 16 | for (int j = i + 1; j < n; j++) 17 | if (arr[j] < arr[min_idx]) 18 | min_idx = j; 19 | 20 | // Swap the found minimum element with the first 21 | // element 22 | int temp = arr[min_idx]; 23 | arr[min_idx] = arr[i]; 24 | arr[i] = temp; 25 | } 26 | } 27 | 28 | // Prints the array 29 | static void printArray(int []arr) 30 | { 31 | int n = arr.Length; 32 | for (int i=0; i 3 | using namespace std; 4 | int main() 5 | { 6 | int i, j, n, pos, temp, min, a[30]; 7 | // take input from user 8 | cout << "Enter the number of elements:"; 9 | cin>>n; 10 | cout << "\nEnter the elements\n"; 11 | for (i = 0; i < n; i++) 12 | { 13 | cin >> a[i]; 14 | } 15 | 16 | for(i = 0; i < n - 1; i++) 17 | { //assuming ith element is the minimum element 18 | min = a[i]; 19 | pos = i; 20 | for (j = i + 1; j < n; j++) 21 | { //finding element smaller than min 22 | if (min > a[j]) 23 | { 24 | min = a[j]; 25 | pos = j; 26 | } 27 | } 28 | // putting smaller element to its correct position 29 | temp = a[i]; 30 | a[i] = a[pos]; 31 | a[pos] = temp; 32 | } 33 | //printing the sorted list 34 | cout << "\nSorted list is as follows\n"; 35 | for (i = 0; i < n; i++) 36 | { 37 | cout << a[i] << " "; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Python/QuickSort.py: -------------------------------------------------------------------------------- 1 | def QuickSort(arr): 2 | 3 | elements = len(arr) 4 | 5 | #Base case 6 | if elements < 2: 7 | return arr 8 | 9 | current_position = 0 #Position of the partitioning element 10 | 11 | for i in range(1, elements): #Partitioning loop 12 | if arr[i] <= arr[0]: 13 | current_position += 1 14 | temp = arr[i] 15 | arr[i] = arr[current_position] 16 | arr[current_position] = temp 17 | 18 | temp = arr[0] 19 | arr[0] = arr[current_position] 20 | arr[current_position] = temp #Brings pivot to it's appropriate position 21 | 22 | left = QuickSort(arr[0:current_position]) #Sorts the elements to the left of pivot 23 | right = QuickSort(arr[current_position+1:elements]) #sorts the elements to the right of pivot 24 | 25 | arr = left + [arr[current_position]] + right #Merging everything together 26 | 27 | return arr 28 | 29 | array_to_be_sorted = [44,57,11,-17,101,67] 30 | print("Original Array: ",array_to_be_sorted) 31 | print("Sorted Array: ",QuickSort(array_to_be_sorted)) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 DevF3st 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Java/InsertionSort.java: -------------------------------------------------------------------------------- 1 | // Online Java Compiler 2 | // Use this editor to write, compile and run your Java code online 3 | 4 | public class InsertionSort { 5 | public static void sort(int arr[]) { 6 | int n = arr.length; 7 | for (int j = 1; j < n; j++) { 8 | int key = arr[j]; 9 | int i = j-1; 10 | while ( (i > -1) && ( arr [i] > key ) ) { 11 | arr [i+1] = arr [i]; 12 | i--; 13 | } 14 | arr[i+1] = key; 15 | } 16 | } 17 | 18 | public static void main(String a[]){ 19 | int[] arr1 = {40,26,8,56,12,15,9}; 20 | System.out.println("Before Insertion Sort"); 21 | for(int i:arr1){ 22 | System.out.print(i+" "); 23 | } 24 | System.out.println(); 25 | 26 | sort(arr1);//sorting array using insertion sort 27 | 28 | System.out.println("After Insertion Sort"); 29 | for(int i:arr1){ 30 | System.out.print(i+" "); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /C++/InsertionSort.cpp: -------------------------------------------------------------------------------- 1 | 2 | // C++ program for insertion sort 3 | 4 | #include 5 | using namespace std; 6 | 7 | // Function to sort an array using 8 | // insertion sort 9 | void insertionSort(int arr[], int n) 10 | { 11 | int i, key, j; 12 | for (i = 1; i < n; i++) 13 | { 14 | key = arr[i]; 15 | j = i - 1; 16 | 17 | // Move elements of arr[0..i-1], 18 | // that are greater than key, to one 19 | // position ahead of their 20 | // current position 21 | while (j >= 0 && arr[j] > key) 22 | { 23 | arr[j + 1] = arr[j]; 24 | j = j - 1; 25 | } 26 | arr[j + 1] = key; 27 | } 28 | } 29 | 30 | // A utility function to print an array 31 | // of size n 32 | void printArray(int arr[], int n) 33 | { 34 | int i; 35 | for (i = 0; i < n; i++) 36 | cout << arr[i] << " "; 37 | cout << endl; 38 | } 39 | 40 | // Driver code 41 | int main() 42 | { 43 | int arr[] = { 12, 11, 13, 5, 6 }; 44 | int N = sizeof(arr) / sizeof(arr[0]); 45 | 46 | insertionSort(arr, N); 47 | printArray(arr, N); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /C++/Selection_sort.cpp: -------------------------------------------------------------------------------- 1 | // selection sort 2 | #include 3 | using namespace std; 4 | 5 | //Swap function 6 | void swap(int *xp, int *yp) 7 | { 8 | int temp = *xp; 9 | *xp = *yp; 10 | *yp = temp; 11 | } 12 | 13 | void selectionSort(int arr[], int n) 14 | { 15 | int i, j, min_idx; 16 | 17 | // One by one move boundary of 18 | // unsorted subarray 19 | for (i = 0; i < n-1; i++) 20 | { 21 | 22 | // Find the minimum element in 23 | // unsorted array 24 | min_idx = i; 25 | for (j = i+1; j < n; j++) 26 | if (arr[j] < arr[min_idx]) 27 | min_idx = j; 28 | 29 | // Swap the found minimum element 30 | // with the first element 31 | if(min_idx!=i) 32 | swap(&arr[min_idx], &arr[i]); 33 | } 34 | } 35 | 36 | //Function to print an array 37 | void printArray(int arr[], int size) 38 | { 39 | int i; 40 | for (i=0; i < size; i++) 41 | cout << arr[i] << " "; 42 | cout << endl; 43 | } 44 | 45 | // Driver program to test above functions 46 | int main() 47 | { 48 | int arr[] = {64, 25, 12, 22, 11}; 49 | int n = sizeof(arr)/sizeof(arr[0]); 50 | selectionSort(arr, n); 51 | cout << "Sorted array: \n"; 52 | printArray(arr, n); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /C++/bubbleSort.cpp: -------------------------------------------------------------------------------- 1 | //Sorting elements of Array in ascending order using Bubble sort in java 2 | //Number of rounds = n-1 , where n is number of elements in array 3 | 4 | #include 5 | using namespace std; 6 | 7 | void bubbleSort(int array[] ,int n) { 8 | int i,j,flag; 9 | for(i=0; i array[j+1]) { 13 | int temp = array[j]; 14 | array[j] = array[j+1]; 15 | array[j+1] = temp; 16 | flag = 1; 17 | } 18 | } 19 | if(flag == 0) 20 | break; 21 | } 22 | 23 | } 24 | 25 | int main() 26 | { 27 | int i; 28 | int arr[] = {32,19,26,12,5}; 29 | int n = sizeof(arr) / sizeof(arr[0]); 30 | 31 | cout<<"Array Before Sorting..."; 32 | for(i=0; i=0&&key < array[j]) 19 | { 20 | array[j+1] = array[j]; 21 | --j; 22 | } 23 | array[j+1] = key; 24 | } 25 | } 26 | 27 | static void printArray(double[] array) 28 | { 29 | foreach(var el in array) 30 | { 31 | Console.Write($"{el}|"); 32 | } 33 | Console.WriteLine(); 34 | } 35 | 36 | 37 | static void Main(string[] args) 38 | { 39 | 40 | var tab = new double[] { 3.2, 5, 1, 5, 2, 5, 6, 7, 4.2, 5.7 }; 41 | Console.WriteLine("Array before sorting:"); 42 | printArray(tab); 43 | sort(tab); 44 | Console.WriteLine("Array after sorting:"); 45 | printArray(tab); 46 | } 47 | 48 | 49 | } 50 | } 51 | 52 | 53 | -------------------------------------------------------------------------------- /MergeSort.java: -------------------------------------------------------------------------------- 1 | public class MergeTwoSortedArrays { 2 | public static void printArray(int[]arr) { 3 | for(int i = 0; i < arr.length; i++) { 4 | System.out.print(arr[i] + " "); 5 | } 6 | } 7 | public static int[] merge2SortedArrays(int arr1[],int arr2[]) { 8 | int m = arr1.length; 9 | int n = arr2.length; 10 | int arr[] = new int[m+n]; 11 | int i = 0; 12 | int j = 0; 13 | int k = 0; 14 | while(i < m && j < n) { // this while loop is for comparing element between two array 15 | if(arr1[i] <= arr2[j]) { 16 | arr[k] = arr1[i]; // array1 element 17 | i++; 18 | k++; 19 | } 20 | else { 21 | arr[k] = arr2[j]; // array2 element 22 | j++; 23 | k++; 24 | } 25 | } 26 | while(i < m) { // this while loop is for copying remaining data from the array1(if any left) 27 | arr[k] = arr1[i]; 28 | i++; 29 | k++; 30 | } 31 | 32 | 33 | while(j < n) { //this while loop is for copying remaining data from the array2(if any left) 34 | arr[k] = arr2[j]; 35 | j++; 36 | k++; 37 | } 38 | 39 | return arr; 40 | } 41 | 42 | public static void main(String[] args) { 43 | int arr1[] = {1,4,6,10,13}; 44 | int arr2[] = {2,5,7,9}; 45 | int arr3[] = merge2SortedArrays(arr1,arr2); 46 | printArray(arr3); 47 | 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /quickSort.java: -------------------------------------------------------------------------------- 1 | 2 | import java.io.*; 3 | 4 | public class QuickSortAlgorithm { 5 | 6 | 7 | public static void swap(int[] arr, int i, int j) 8 | { 9 | int temp = arr[i]; 10 | arr[i] = arr[j]; 11 | arr[j] = temp; 12 | } 13 | 14 | public static int partition(int[] arr, int low, int high) 15 | { 16 | 17 | int pivot = arr[high]; 18 | 19 | 20 | int i = (low - 1); 21 | 22 | for (int j = low; j <= high - 1; j++) { 23 | 24 | 25 | if (arr[j] < pivot) { 26 | 27 | 28 | i++; 29 | swap(arr, i, j); 30 | } 31 | } 32 | swap(arr, i + 1, high); 33 | return (i + 1); 34 | } 35 | 36 | public static void quickSort(int[] arr, int low, int high) 37 | { 38 | if (low < high) { 39 | 40 | 41 | int pi = partition(arr, low, high); 42 | 43 | 44 | quickSort(arr, low, pi - 1); 45 | quickSort(arr, pi + 1, high); 46 | } 47 | } 48 | 49 | public static void printArr(int[] arr) 50 | { 51 | for (int i = 0; i < arr.length; i++) { 52 | System.out.print(arr[i] + " "); 53 | } 54 | } 55 | 56 | 57 | public static void main(String[] args) 58 | { 59 | int[] arr = { 10, 7, 8, 9, 1, 5 }; 60 | int N = arr.length; 61 | 62 | 63 | quickSort(arr, 0, N - 1); 64 | System.out.println("Sorted array:"); 65 | printArr(arr); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /C++/Insertion_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | 5 | // Insertion Sort Algorithm 6 | 7 | // Time complexity 8 | // Best: O(n) 9 | // Average: O(n^2) 10 | // Worst: O(n^2) 11 | 12 | // Space complexity: O(1) 13 | void insertionSort(int arr[], int n) // n is the size of the array 14 | { 15 | int i, key, j; // i is the index of the element to be inserted, key is the element to be inserted, j is the index of the element to be compared 16 | for (i = 1; i < n; i++) // i starts at 1 because the first element is already sorted 17 | { 18 | key = arr[i]; // key is the element to be inserted 19 | j = i - 1; // j is the index of the element to be compared 20 | while (j >= 0 && arr[j] > key) // while the element to be compared is greater than the element to be inserted 21 | { 22 | arr[j + 1] = arr[j]; // shift the element to the right 23 | j = j - 1; // decrement j 24 | } 25 | arr[j + 1] = key; // insert the element 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | int arr[] = { 12, 11, 13, 5, 6, 7 }; 32 | cout << "Sorted array is \n"; 33 | insertionSort(arr, sizeof(arr) / sizeof(arr[0])); 34 | for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) 35 | cout << arr[i] << " "; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Java/BubbleSort.java: -------------------------------------------------------------------------------- 1 | //Sorting elements of Array in ascending order using Bubble sort in java 2 | //Number of rounds = n-1 , where n is number of elements in array 3 | 4 | public class BubbleSort { 5 | 6 | void bubbleSort(int[] array) { 7 | 8 | for(int i=0; i array[j+1]) { 12 | int temp = array[j]; 13 | array[j] = array[j+1]; 14 | array[j+1] = temp; 15 | flag = 1; 16 | } 17 | } 18 | if(flag == 0) 19 | break; 20 | } 21 | 22 | } 23 | public static void main(String[] args) { 24 | BubbleSort bs = new BubbleSort(); 25 | int arr[]= {32,19,26,12,5,-1}; 26 | System.out.println("Array Before Sorting..."); 27 | for(int i:arr) 28 | System.out.print(i+ " "); 29 | System.out.println(); 30 | 31 | System.out.println(" Array After Sorting..."); 32 | bs.bubbleSort(arr); 33 | for(int i:arr) 34 | System.out.print(i+ " "); 35 | 36 | } 37 | 38 | } 39 | 40 | //Best case Time complexity :- O(n) 41 | //Avg case Time complexity :- O(n²) 42 | //Worst case Time complexity :- O(n²) 43 | 44 | //Space complexity :- O(1) 45 | -------------------------------------------------------------------------------- /SelectionSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class SelectionSort { 3 | public static void printArray(int[] arr) { 4 | int n = arr.length; 5 | for(int i = 0; i 2 | #include 3 | #include 4 | #include 5 | 6 | #define M_E 2.7182818284 7 | 8 | short int funcPrime(int NUM); 9 | int funcGoldbach(int num); 10 | 11 | void main(){ 12 | int varA; 13 | printf("| Type a number to execute Goldbach function. |\n"); 14 | scanf("%d", &varA); 15 | funcGoldbach(varA); 16 | } 17 | 18 | short int funcPrime(int NUM){ 19 | int i, raiz = sqrt(NUM) + 1; 20 | if(NUM%2 == 0 && NUM != 2 || NUM == 1) 21 | return 0; 22 | else 23 | for (i = 3; i 1000) 33 | return 0; 34 | system("cls"); 35 | printf("|==========================================================================|\n"); 36 | printf("| Current number: %4d |\n", num); 37 | if (num <=1000) 38 | for(i = 1; i < num/2 +1; i+=1) 39 | if(funcPrime(i)) 40 | for(aux = 2; aux 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution{ 10 | public: 11 | // Function to find element in sorted array 12 | // arr: input array 13 | // N: size of array 14 | // K: element to be searche 15 | int searchInSorted(int arr[], int N, int K) 16 | { 17 | 18 | // Your code here 19 | int low=0,high=N-1; 20 | while(low<=high){ 21 | int mid=(low+high)/2; 22 | if(arr[mid] == K){ 23 | return 1; 24 | } 25 | else if(arr[mid] > K){ 26 | high=mid-1; 27 | } 28 | else{ 29 | low=mid+1; 30 | } 31 | } 32 | return -1; 33 | 34 | } 35 | }; 36 | 37 | //{ Driver Code Starts. 38 | 39 | int main(void) 40 | { 41 | 42 | int t; 43 | cin >> t; 44 | while(t--){ 45 | int n, k; 46 | cin >> n >> k; 47 | 48 | int arr[n]; 49 | 50 | for(int i = 0;i> arr[i]; 52 | } 53 | 54 | Solution ob; 55 | cout << ob.searchInSorted(arr, n, k) << endl; 56 | 57 | } 58 | 59 | return 0; 60 | } 61 | 62 | // } Driver Code Ends 63 | -------------------------------------------------------------------------------- /Java/Insertion_Sort.java: -------------------------------------------------------------------------------- 1 | //Sorting the user given array in Ascending Order using insertion sort 2 | 3 | import java.util.Scanner; 4 | 5 | public class InsertionSort { 6 | 7 | void insertionSort(int[] array) { 8 | int temp; 9 | for(int i=1; i0 && array[j-1]>temp) { 13 | array[j] = array[j-1]; 14 | j = j-1; 15 | } 16 | array[j] = temp; 17 | } 18 | } 19 | 20 | public static void main(String[] args) { 21 | InsertionSort is = new InsertionSort(); 22 | Scanner sc=new Scanner(System.in); 23 | 24 | System.out.print("Enter the number of elements you want to store: "); 25 | int n=sc.nextInt(); 26 | 27 | int[] arr = new int[n]; 28 | System.out.println("Enter the elements of the array in any order: "); 29 | for(int i=0; i arr[j]){ 10 | //swap elements 11 | temp = arr[j-1]; 12 | arr[j-1] = arr[j]; 13 | arr[j] = temp; 14 | } 15 | 16 | } 17 | } 18 | 19 | } 20 | public static void main(String[] args) { 21 | int arr[] ={3,60,35,2,45,320,5}; 22 | 23 | System.out.println("Array Before Bubble Sort"); 24 | for(int i=0; i < arr.length; i++){ 25 | System.out.print(arr[i] + " "); 26 | } 27 | System.out.println(); 28 | 29 | bubbleSort(arr);//sorting array elements using bubble sort 30 | 31 | System.out.println("Array After Bubble Sort"); 32 | for(int i=0; i < arr.length; i++){ 33 | System.out.print(arr[i] + " "); 34 | } 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /C++/Dfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | private: 6 | void dfs(int node, vector adj[], int vis[], vector &ls) { 7 | vis[node] = 1; 8 | ls.push_back(node); 9 | // traverse all its neighbours 10 | for(auto it : adj[node]) { 11 | // if the neighbour is not visited 12 | if(!vis[it]) { 13 | dfs(it, adj, vis, ls); 14 | } 15 | } 16 | } 17 | public: 18 | // Function to return a list containing the DFS traversal of the graph. 19 | vector dfsOfGraph(int V, vector adj[]) { 20 | int vis[V] = {0}; 21 | int start = 0; 22 | // create a list to store dfs 23 | vector ls; 24 | // call dfs for starting node 25 | dfs(start, adj, vis, ls); 26 | return ls; 27 | } 28 | }; 29 | 30 | void addEdge(vector adj[], int u, int v) { 31 | adj[u].push_back(v); 32 | adj[v].push_back(u); 33 | } 34 | 35 | void printAns(vector &ans) { 36 | for (int i = 0; i < ans.size(); i++) { 37 | cout << ans[i] << " "; 38 | } 39 | } 40 | 41 | int main() 42 | { 43 | vector adj[5]; 44 | 45 | addEdge(adj, 0, 2); 46 | addEdge(adj, 2, 4); 47 | addEdge(adj, 0, 1); 48 | addEdge(adj, 0, 3); 49 | 50 | Solution obj; 51 | vector ans = obj.dfsOfGraph(5, adj); 52 | printAns(ans); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /C++/dfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | private: 6 | void dfs(int node, vector adj[], int vis[], vector &ls) { 7 | vis[node] = 1; 8 | ls.push_back(node); 9 | // traverse all its neighbours 10 | for(auto it : adj[node]) { 11 | // if the neighbour is not visited 12 | if(!vis[it]) { 13 | dfs(it, adj, vis, ls); 14 | } 15 | } 16 | } 17 | public: 18 | // Function to return a list containing the DFS traversal of the graph. 19 | vector dfsOfGraph(int V, vector adj[]) { 20 | int vis[V] = {0}; 21 | int start = 0; 22 | // create a list to store dfs 23 | vector ls; 24 | // call dfs for starting node 25 | dfs(start, adj, vis, ls); 26 | return ls; 27 | } 28 | }; 29 | 30 | void addEdge(vector adj[], int u, int v) { 31 | adj[u].push_back(v); 32 | adj[v].push_back(u); 33 | } 34 | 35 | void printAns(vector &ans) { 36 | for (int i = 0; i < ans.size(); i++) { 37 | cout << ans[i] << " "; 38 | } 39 | } 40 | 41 | int main() 42 | { 43 | vector adj[5]; 44 | 45 | addEdge(adj, 0, 2); 46 | addEdge(adj, 2, 4); 47 | addEdge(adj, 0, 1); 48 | addEdge(adj, 0, 3); 49 | 50 | Solution obj; 51 | vector ans = obj.dfsOfGraph(5, adj); 52 | printAns(ans); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /C#/MergeSort.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | class MergeSort { 3 | void merge(int[] arr, int l, int m, int r) 4 | { 5 | int n1 = m - l + 1; 6 | int n2 = r - m; 7 | int[] L = new int[n1]; 8 | int[] R = new int[n2]; 9 | int i, j; 10 | 11 | for (i = 0; i < n1; ++i) 12 | L[i] = arr[l + i]; 13 | 14 | for (j = 0; j < n2; ++j) 15 | R[j] = arr[m + 1 + j]; 16 | 17 | i = 0; 18 | j = 0; 19 | int k = l; 20 | 21 | while (i < n1 && j < n2) { 22 | if (L[i] <= R[j]) { 23 | arr[k] = L[i]; 24 | i++; 25 | } 26 | else { 27 | arr[k] = R[j]; 28 | j++; 29 | } 30 | k++; 31 | } 32 | 33 | while (i < n1) { 34 | arr[k] = L[i]; 35 | i++; 36 | k++; 37 | } 38 | 39 | while (j < n2) { 40 | arr[k] = R[j]; 41 | j++; 42 | k++; 43 | } 44 | } 45 | 46 | void sort(int[] arr, int l, int r) 47 | { 48 | if (l < r) { 49 | int m = l + (r - l) / 2; 50 | sort(arr, l, m); 51 | sort(arr, m + 1, r); 52 | merge(arr, l, m, r); 53 | } 54 | } 55 | 56 | static void printArray(int[] arr) 57 | { 58 | int n = arr.Length; 59 | for (int i = 0; i < n; ++i) 60 | Console.Write(arr[i] + " "); 61 | Console.WriteLine(); 62 | } 63 | 64 | public static void Main(String[] args) 65 | { 66 | int[] arr = { 24, 85, 46, 2, 64, 34 }; 67 | 68 | Console.WriteLine("Inserted Array: "); 69 | printArray(arr); 70 | 71 | MergeSort ob = new MergeSort(); 72 | ob.sort(arr, 0, arr.Length - 1); 73 | 74 | Console.WriteLine("\nSorted array: "); 75 | printArray(arr); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /C++/Bubble_sort.cpp: -------------------------------------------------------------------------------- 1 | BUBBLE SORT CODE : 2 | 3 | 4 | #include 5 | #define swap(x,y) { x = x + y; y = x - y; x = x - y; } 6 | 7 | using namespace std; 8 | void bubbleSort(int arr[], int n) 9 | { 10 | int i, j; 11 | bool flag; 12 | // Outer pass 13 | for(i = 0; i < n; i++) 14 | { 15 | flag = false; // Set flag as false 16 | for(j = 0; j < n-i-1; j++) 17 | { 18 | // Compare values 19 | if( arr[j] > arr[j+1]) 20 | { 21 | swap(arr[j],arr[j+1]); 22 | flag = true; 23 | } 24 | } 25 | // If no to elements are swapped then 26 | // array is sorted. Hence Break the loop. 27 | if(!flag) 28 | { 29 | break; 30 | } 31 | } 32 | } 33 | int main(int argv, char* argc[]) 34 | { 35 | cout<<"Enter the total number of elements : "; 36 | int total; 37 | cin>>total; 38 | int arr[total]; 39 | for(int i=0;i>arr[i]; 43 | } 44 | int n = sizeof(arr)/sizeof(int); 45 | cout<<"Unsorted Array :"; 46 | for(int i=0;i 2 | using namespace std; 3 | 4 | // function to swap two elements 5 | void swap(int* a, int* b) 6 | { 7 | int t = *a; 8 | *a = *b; 9 | *b = t; 10 | } 11 | int partition(int arr[], int low, int high) 12 | { 13 | int pivot = arr[high]; // pivot 14 | int i 15 | = (low 16 | - 1); // Index of smaller element and indicates 17 | // the right position of pivot found so far 18 | 19 | for (int j = low; j <= high - 1; j++) { 20 | // If current element is smaller than the pivot 21 | if (arr[j] < pivot) { 22 | i++; // increment index of smaller element 23 | swap(&arr[i], &arr[j]); 24 | } 25 | } 26 | swap(&arr[i + 1], &arr[high]); 27 | return (i + 1); 28 | } 29 | 30 | /* The main function that implements QuickSort 31 | arr[] --> Array to be sorted, 32 | low --> Starting index, 33 | high --> Ending index */ 34 | void quickSort(int arr[], int low, int high) 35 | { 36 | if (low < high) { 37 | /* pi is partitioning index, arr[p] is now 38 | at right place */ 39 | int pi = partition(arr, low, high); 40 | 41 | // Separately sort elements before 42 | // partition and after partition 43 | quickSort(arr, low, pi - 1); 44 | quickSort(arr, pi + 1, high); 45 | } 46 | } 47 | 48 | /* Function to print an array */ 49 | void printArray(int arr[], int size) 50 | { 51 | int i; 52 | for (i = 0; i < size; i++) 53 | cout << arr[i] << " "; 54 | cout << endl; 55 | } 56 | int main() 57 | { 58 | int arr[] = { 10, 7, 8, 9, 1, 5 }; 59 | int n = sizeof(arr) / sizeof(arr[0]); 60 | quickSort(arr, 0, n - 1); 61 | cout << "Sorted array: \n"; 62 | printArray(arr, n); 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Java/trapping_rainwater_problem.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining. 3 | 4 | Example 1: 5 | Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] 6 | Output: 6 7 | Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. 8 | 9 | Example 2: 10 | 11 | Input: height = [4,2,0,3,2,5] 12 | Output: 9 13 | 14 | 15 | Constraints: 16 | 17 | n == height.length 18 | 1 <= n <= 2 * 104 19 | 0 <= height[i] <= 105 20 | */ 21 | 22 | import java.util.Scanner; 23 | class Trapping_Rainwater{ 24 | public static int Trap(int arr[]){ 25 | //left max Auxilary Array 26 | int n=arr.length; 27 | int leftMax[]=new int[n]; 28 | leftMax[0]=arr[0]; 29 | for(int i=1;i=0;i--){ 36 | rightMax[i]=Math.max(arr[i], rightMax[i+1]); 37 | } 38 | //main loop 39 | int sum=0; 40 | for(int i=0;iFork button on top up bar. 19 | 20 | [![Fork Button](https://github.com/DevF3st/Learn-Programming/blob/main/_doc/fork.jpg)](https://github.com/DevF3st/Learn-Programming) 21 | 22 | ### 4. Contribute to issues in the issue section for any programming language you like :computer: 23 | Once you have forked the repo, do your code in the language folder :file_folder: in the main branch, if language folder doesn't exist you can make folder :file_folder: and add your program in it. 24 | 25 | You can find new languages in this [Programming Language List](https://en.wikipedia.org/wiki/List_of_programming_languages). 26 | 27 | ### 5. Commit & PR :running: 28 | 29 | After you have completed these steps, then start contributing by checking `Help Wanted` issues and creating 30 | [pull requests](https://github.com/DevF3st/Learn-Programming/pulls). 31 | 32 | ## Contributors 33 | 34 | 35 | 36 | 37 | 38 | ### Thanks for contributing. It's an amazing experience to collaborate with you all! 39 | -------------------------------------------------------------------------------- /PHP/mergesort.php: -------------------------------------------------------------------------------- 1 | 96 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *_i.c 2 | *_p.c 3 | *_h.h 4 | *.ilk 5 | *.meta 6 | *.obj 7 | *.iobj 8 | *.pch 9 | *.pdb 10 | *.ipdb 11 | *.pgc 12 | *.pgd 13 | *.rsp 14 | *.sbr 15 | *.tlb 16 | *.tli 17 | *.tlh 18 | *.tmp 19 | *.tmp_proj 20 | *_wpftmp.csproj 21 | *.log 22 | *.vspscc 23 | *.vssscc 24 | .builds 25 | *.pidb 26 | *.svclog 27 | *.scc 28 | *.exe 29 | 30 | *.iml 31 | .gradle 32 | 33 | .vscode 34 | .vscode/* 35 | !.vscode/settings.json 36 | !.vscode/tasks.json 37 | !.vscode/launch.json 38 | !.vscode/extensions.json 39 | !.vscode/*.code-snippets 40 | .history/ 41 | *.vsix 42 | 43 | .idea 44 | .idea/**/workspace.xml 45 | .idea/**/tasks.xml 46 | .idea/**/usage.statistics.xml 47 | .idea/**/dictionaries 48 | .idea/**/shelf 49 | .idea/**/aws.xml 50 | .idea/**/contentModel.xml 51 | .idea/**/dataSources/ 52 | .idea/**/dataSources.ids 53 | .idea/**/dataSources.local.xml 54 | .idea/**/sqlDataSources.xml 55 | .idea/**/dynamic.xml 56 | .idea/**/uiDesigner.xml 57 | .idea/**/dbnavigator.xml 58 | .idea/**/gradle.xml 59 | .idea/**/libraries 60 | cmake-build-*/ 61 | .idea/**/mongoSettings.xml 62 | *.iws 63 | out/ 64 | .idea_modules/ 65 | atlassian-ide-plugin.xml 66 | .idea/replstate.xml 67 | .idea/sonarlint/ 68 | com_crashlytics_export_strings.xml 69 | crashlytics.properties 70 | crashlytics-build.properties 71 | fabric.properties 72 | .idea/httpRequests 73 | .idea/caches/build_file_checksums.ser 74 | 75 | __pycache__/ 76 | *.py[cod] 77 | *$py.class 78 | *.so 79 | .Python 80 | build/ 81 | develop-eggs/ 82 | dist/ 83 | downloads/ 84 | eggs/ 85 | .eggs/ 86 | lib/ 87 | lib64/ 88 | parts/ 89 | sdist/ 90 | var/ 91 | wheels/ 92 | share/python-wheels/ 93 | *.egg-info/ 94 | .installed.cfg 95 | *.egg 96 | MANIFEST 97 | *.manifest 98 | *.spec 99 | pip-log.txt 100 | pip-delete-this-directory.txt 101 | htmlcov/ 102 | .tox/ 103 | .nox/ 104 | .coverage 105 | .coverage.* 106 | .cache 107 | nosetests.xml 108 | coverage.xml 109 | *.cover 110 | *.py,cover 111 | .hypothesis/ 112 | .pytest_cache/ 113 | cover/ 114 | *.mo 115 | *.pot 116 | *.log 117 | local_settings.py 118 | db.sqlite3 119 | db.sqlite3-journal 120 | instance/ 121 | .webassets-cache 122 | .scrapy 123 | docs/_build/ 124 | .pybuilder/ 125 | target/ 126 | .ipynb_checkpoints 127 | profile_default/ 128 | ipython_config.py 129 | -------------------------------------------------------------------------------- /C++/AllSearchingAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int linearSearch(const vector& arr, int target) { 8 | for (size_t i = 0; i < arr.size(); ++i) { 9 | if (arr[i] == target) 10 | return i; // Return the index where the target is found 11 | } 12 | return -1; // Return -1 if the target is not found 13 | } 14 | 15 | int binarySearch(const vector& arr, int target) { 16 | int left = 0; 17 | int right = arr.size() - 1; 18 | 19 | while (left <= right) { 20 | int mid = left + (right - left) / 2; 21 | 22 | if (arr[mid] == target) 23 | return mid; // Return the index where the target is found 24 | 25 | if (arr[mid] < target) 26 | left = mid + 1; 27 | else 28 | right = mid - 1; 29 | } 30 | 31 | return -1; // Return -1 if the target is not found 32 | } 33 | 34 | int jumpSearch(const vector& arr, int target) { 35 | int n = arr.size(); 36 | int step = sqrt(n); 37 | int prev = 0; 38 | 39 | while (arr[min(step, n) - 1] < target) { 40 | prev = step; 41 | step += sqrt(n); 42 | if (prev >= n) 43 | return -1; 44 | } 45 | 46 | while (arr[prev] < target) { 47 | prev++; 48 | if (prev == min(step, n)) 49 | return -1; 50 | } 51 | 52 | if (arr[prev] == target) 53 | return prev; // Return the index where the target is found 54 | 55 | return -1; // Return -1 if the target is not found 56 | } 57 | 58 | int main() { 59 | vector arr = {2, 3, 5, 8, 10, 12, 18, 20, 23, 27, 30, 35, 40}; 60 | 61 | int target = 18; 62 | 63 | cout << "Array: "; 64 | for (int num : arr) 65 | cout << num << " "; 66 | cout << "\nTarget: " << target << "\n\n"; 67 | 68 | int linearResult = linearSearch(arr, target); 69 | cout << "Linear Search Result: "; 70 | if (linearResult != -1) 71 | cout << "Found at index " << linearResult << "\n"; 72 | else 73 | cout << "Not found\n"; 74 | 75 | int binaryResult = binarySearch(arr, target); 76 | cout << "Binary Search Result: "; 77 | if (binaryResult != -1) 78 | cout << "Found at index " << binaryResult << "\n"; 79 | else 80 | cout << "Not found\n"; 81 | 82 | int jumpResult = jumpSearch(arr, target); 83 | cout << "Jump Search Result: "; 84 | if (jumpResult != -1) 85 | cout << "Found at index " << jumpResult << "\n"; 86 | else 87 | cout << "Not found\n"; 88 | 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /C++/water_conection.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to find efficient 2 | // solution for the network 3 | #include // For memset 4 | #include 5 | #include 6 | 7 | using std::cin; 8 | using std::cout; 9 | using std::endl; 10 | using std::memset; 11 | using std::vector; 12 | // number of houses and number 13 | // of pipes 14 | int number_of_houses, number_of_pipes; 15 | 16 | // Array rd stores the 17 | // ending vertex of pipe 18 | int ending_vertex_of_pipes[1100]; 19 | 20 | // Array wd stores the value 21 | // of diameters between two pipes 22 | int diameter_between_two_pipes[1100]; 23 | 24 | // Array cd stores the 25 | // starting end of pipe 26 | int starting_vertex_of_pipes[1100]; 27 | 28 | // Vector a, b, c are used 29 | // to store the final output 30 | vector a; 31 | vector b; 32 | vector c; 33 | 34 | int ans; 35 | 36 | int dfs(int w) 37 | { 38 | if (starting_vertex_of_pipes[w] == 0) 39 | return w; 40 | if (diameter_between_two_pipes[w] < ans) 41 | ans = diameter_between_two_pipes[w]; 42 | return dfs(starting_vertex_of_pipes[w]); 43 | } 44 | 45 | // Function performing calculations. 46 | void solve(int arr[][3]) 47 | { 48 | for (int i = 0; i < number_of_pipes; ++i) { 49 | 50 | int house_1 = arr[i][0], house_2 = arr[i][1], 51 | pipe_diameter = arr[i][2]; 52 | 53 | starting_vertex_of_pipes[house_1] = house_2; 54 | diameter_between_two_pipes[house_1] = pipe_diameter; 55 | ending_vertex_of_pipes[house_2] = house_1; 56 | } 57 | 58 | a.clear(); 59 | b.clear(); 60 | c.clear(); 61 | 62 | for (int j = 1; j <= number_of_houses; ++j) 63 | 64 | /*If a pipe has no ending vertex 65 | but has starting vertex i.e is 66 | an outgoing pipe then we need 67 | to start DFS with this vertex.*/ 68 | if (ending_vertex_of_pipes[j] == 0 69 | && starting_vertex_of_pipes[j]) { 70 | ans = 1000000000; 71 | int w = dfs(j); 72 | 73 | // We put the details of component 74 | // in final output array 75 | a.push_back(j); 76 | b.push_back(w); 77 | c.push_back(ans); 78 | } 79 | 80 | cout << a.size() << endl; 81 | for (int j = 0; j < a.size(); ++j) 82 | cout << a[j] << " " << b[j] << " " << c[j] << endl; 83 | } 84 | 85 | // driver function 86 | int main() 87 | { 88 | number_of_houses = 9, number_of_pipes = 6; 89 | 90 | memset(ending_vertex_of_pipes, 0, 91 | sizeof(ending_vertex_of_pipes)); 92 | memset(starting_vertex_of_pipes, 0, 93 | sizeof(starting_vertex_of_pipes)); 94 | memset(diameter_between_two_pipes, 0, 95 | sizeof(diameter_between_two_pipes)); 96 | 97 | int arr[][3] 98 | = { { 7, 4, 98 }, { 5, 9, 72 }, { 4, 6, 10 }, 99 | { 2, 8, 22 }, { 9, 7, 17 }, { 3, 1, 66 } }; 100 | 101 | solve(arr); 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /C++/MergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::cout; 5 | using std::cin; 6 | using std::vector; 7 | 8 | // Merge Sort Algorithm 9 | 10 | // Time complexity 11 | // Best: O(n log n) 12 | // Average: O(n log n) 13 | // Worst: O(n log n) 14 | 15 | // Space complexity: O(n) 16 | template 17 | void merge(vector& list, int start, int mid, int end, int &nComparisons) { // merge two sorted sublists 18 | vector left; // left sublist 19 | vector right; // right sublist 20 | int leftSize = mid - start + 1; // size of left sublist 21 | int rightSize = end - mid; // size of right sublist 22 | 23 | for (int i = 0; i < leftSize; i++) { // copy left sublist to left vector 24 | left.push_back(list[start+i]); // copy element 25 | } 26 | 27 | for (int i = 0; i < rightSize; i++) { // copy right sublist to right vector 28 | right.push_back(list[i+mid+1]); // copy element 29 | } 30 | 31 | int pos = start; // position in list 32 | int leftPos = 0; // position in left sublist 33 | int rightPos = 0; // position in right sublist 34 | 35 | while (leftPos < leftSize && rightPos < rightSize) { // while both sublists are not empty 36 | nComparisons++; // increment comparisons 37 | if (left[leftPos] < right[rightPos]) { // if left element is smaller than right element 38 | list[pos] = left[leftPos]; // copy left element to list 39 | leftPos++; // increment left sublist position 40 | } else { // if right element is smaller than left element 41 | list[pos] = right[rightPos]; // copy right element to list 42 | rightPos++; // increment right sublist position 43 | } 44 | pos++; // increment position in list 45 | } 46 | 47 | while (leftPos < leftSize) { // while left sublist is not empty 48 | list[pos] = left[leftPos]; // copy left element to list 49 | leftPos++; // increment left sublist position 50 | pos++; // increment position in list 51 | } 52 | 53 | while (rightPos < rightSize) { // while right sublist is not empty 54 | list[pos] = right[rightPos]; // copy right element to list 55 | rightPos++; // increment right sublist position 56 | pos++; // increment position in list 57 | } 58 | } 59 | 60 | template // merge sort algorithm 61 | void mergeSort(vector& list, int start, int end, int &nComparisons) { // sort sublist 62 | 63 | if(end > start) { // if sublist is not empty 64 | int mid = (start + end) / 2; // find midpoint of sublist 65 | mergeSort(list, start, mid, nComparisons); // sort left sublist 66 | mergeSort(list, mid+1, end, nComparisons); // sort right sublist 67 | merge(list, start, mid, end, nComparisons); // merge two sorted sublists 68 | } 69 | } 70 | 71 | 72 | int main() 73 | { 74 | int nComparisons = 0; 75 | vector list; 76 | int n; 77 | cout << "Enter the number of elements in the list: "; 78 | cin >> n; 79 | for (int i = 0; i < n; i++) { 80 | int x; 81 | cout << "Enter element " << i+1 << ": "; 82 | cin >> x; 83 | list.push_back(x); 84 | } 85 | cout << "The list is: "; 86 | for (int i = 0; i < n; i++) { 87 | cout << list[i] << " "; 88 | } 89 | cout << "\n"; 90 | mergeSort(list, 0, n-1, nComparisons); 91 | cout << "The sorted list is: "; 92 | for (int i = 0; i < n; i++) { 93 | cout << list[i] << " "; 94 | } 95 | cout << "\n"; 96 | cout << "The number of comparisons is: " << nComparisons << "\n"; 97 | return 0; 98 | } -------------------------------------------------------------------------------- /C++/BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Node{ 6 | 7 | public: 8 | int data; 9 | Node* left; 10 | Node* right; 11 | 12 | Node(int d){ 13 | this -> data = d; 14 | this -> left = NULL; 15 | this -> right = NULL; 16 | } 17 | }; 18 | 19 | Node* insertIntoBST(Node* &root, int d){ 20 | if(root == NULL){ 21 | root = new Node(d); 22 | return root; 23 | } 24 | 25 | if(d > root -> data){ 26 | root -> right = insertIntoBST(root -> right, d); 27 | } 28 | else{ 29 | root -> left = insertIntoBST(root -> left, d); 30 | } 31 | return root; 32 | } 33 | 34 | Node* minVal(Node* root){ 35 | Node* temp = root; 36 | 37 | while(temp -> left != NULL){ 38 | temp = temp -> left; 39 | } 40 | return temp; 41 | } 42 | 43 | Node* maxVal(Node* root){ 44 | Node* temp = root; 45 | while(temp -> right != NULL){ 46 | temp = temp -> right; 47 | } 48 | return root; 49 | } 50 | 51 | // this is the main code for the deletion 52 | Node* deleteFromBST(Node* root, int val){ 53 | if(root == NULL){ 54 | return root; 55 | } 56 | if(root -> data == val){ 57 | // 0 child 58 | if(root -> left == NULL && root -> right == NULL){ 59 | delete root; 60 | return NULL; 61 | } 62 | // 1 child 63 | // left child 64 | if(root -> left != NULL && root -> right != NULL){ 65 | Node* temp = root -> left; 66 | delete temp; 67 | return temp; 68 | } 69 | // right child 70 | if(root -> left == NULL && root -> right != NULL){ 71 | Node* temp = root -> right; 72 | delete temp; 73 | return temp; 74 | } 75 | // 2 child 76 | if(root -> left != NULL && root -> right != NULL){ 77 | int mini = minVal(root -> right) -> data; 78 | root -> data = mini; 79 | root -> right = deleteFromBST(root -> right, mini); 80 | return root; 81 | } 82 | } 83 | else if(root -> data > val){ 84 | // left part 85 | root -> left = deleteFromBST(root -> left, val); 86 | return root; 87 | } 88 | else{ 89 | // right part 90 | root -> right = deleteFromBST(root -> right, val); 91 | return root; 92 | } 93 | } 94 | 95 | void levelOrdertraversal(Node *root){ 96 | 97 | queue q; 98 | q.push(root); 99 | q.push(NULL); 100 | 101 | while(!q.empty()){ 102 | Node *temp = q.front(); 103 | q.pop(); 104 | 105 | if(temp == NULL){ 106 | // previous level has been completed 107 | cout << endl; 108 | if(!q.empty()){ 109 | // queue still has some child node 110 | q.push(NULL); 111 | } 112 | } 113 | else{ 114 | cout << temp -> data << " "; 115 | if(temp -> left){ 116 | q.push(temp -> left); 117 | } 118 | if(temp -> right){ 119 | q.push(temp -> right); 120 | } 121 | } 122 | } 123 | } 124 | 125 | void takeinput(Node* &root){ 126 | int data; 127 | cin >> data; 128 | 129 | while(data != -1){ 130 | root = insertIntoBST(root, data); 131 | cin >> data; 132 | } 133 | } 134 | 135 | int main() 136 | { 137 | Node* root = NULL; 138 | 139 | cout << "Enter data to create the binary sreach tree : " << endl; 140 | takeinput(root); 141 | 142 | cout << "Printing the BST : " << endl; 143 | levelOrdertraversal(root); 144 | 145 | /* 146 | cout << "Printing Inorder : " << endl; 147 | inorder(root); 148 | 149 | cout << "Printing Postorder : " << endl; 150 | postorder(root); 151 | 152 | cout << "Printing Preorder : " << endl; 153 | preorder(root); 154 | */ 155 | 156 | // deletion in BST 157 | root = deleteFromBST(root, 50); 158 | return 0; 159 | } 160 | -------------------------------------------------------------------------------- /Python/games/PaddleBall.py: -------------------------------------------------------------------------------- 1 | import random 2 | import pygame as pg 3 | import sys,time 4 | 5 | 6 | pg.init() #starting of the game 7 | screen_width = 800 8 | screen_height = 800 9 | 10 | # bg_img = pg.image.load('back1.jpg') 11 | # bg_img = pg.transform.scale(bg_img,(800,800)) 12 | 13 | win = pg.display.set_mode((screen_width,screen_height)) #win is use for showing window of given width and height 14 | paddle = pg.Rect((screen_width/2-60),770,120,15) #paddle will draw at the bottom in the middle of width 15 | clock = pg.time.Clock() 16 | 17 | font = pg.font.Font('arial 1.ttf',36) 18 | score_text = font.render('Score : 0',True,"#FFFF00",(0,0,0)) 19 | score_text_Rect = score_text.get_rect() 20 | score_text_Rect.x = 10 21 | score_text_Rect.y = 10 22 | 23 | font2 = pg.font.Font('arial 1.ttf',64) 24 | game_over_text = font2.render('GAME OVER',True,(255,0,0),(0,0,0)) 25 | game_over_text_rect = game_over_text.get_rect() 26 | game_over_text_rect.center = (screen_width//2,screen_height//2) 27 | 28 | font3 = pg.font.Font('arial 1.ttf',24) 29 | choice_text = font3.render('Choose Mode ',True,"#97FFFF",(0,0,0)) 30 | choice_text_rect = choice_text.get_rect() 31 | choice_text_rect.x = 575 32 | choice_text_rect.y = 10 33 | 34 | 35 | font4 = pg.font.Font('arial 1.ttf',18) 36 | choice1_text = font4.render('1. Enter e for Easy ',True,"#97FFFF",(0,0,0)) 37 | choice1_text_rect = choice1_text.get_rect() 38 | choice1_text_rect.x = 600 39 | choice1_text_rect.y = 40 40 | 41 | 42 | font5 = pg.font.Font('arial 1.ttf',18) 43 | choice2_text = font5.render('2. Enter d for Difficult ',True,"#97FFFF",(0,0,0)) 44 | choice2_text_rect = choice2_text.get_rect() 45 | choice2_text_rect.x = 600 46 | choice2_text_rect.y = 60 47 | 48 | 49 | font6 = pg.font.Font('arial 1.ttf',24) 50 | reset_text = font6.render('Enter r for reset ',True,(219,112,147),(0,0,0)) 51 | reset_text_rect = reset_text.get_rect() 52 | reset_text_rect.center = (screen_width//2,25) 53 | 54 | font7 = pg.font.Font('arial 1.ttf',24) 55 | exit_text = font7.render('Enter z for exit ',True,(219,112,147),(0,0,0)) 56 | exit_text_rect = exit_text.get_rect() 57 | exit_text_rect.center = (screen_width//2,60) 58 | 59 | font8 = pg.font.Font('arial 1.ttf',48) 60 | welcome_text = font8.render('WELCOME ',True,"white",(0,0,0)) 61 | welcome_text_rect = welcome_text.get_rect() 62 | welcome_text_rect.center = (screen_width//2,screen_height//2) 63 | 64 | font9 = pg.font.Font('arial 1.ttf',24) 65 | start_text = font9.render('Enter space to start the game ',True,(205,102,0),(0,0,0)) 66 | start_text_rect = start_text.get_rect() 67 | start_text_rect.center = (screen_width//2,450) 68 | 69 | 70 | run_game = True 71 | start_game = False 72 | paddle_speed = 10 73 | max_ball_speed = 10 74 | min_ball_speed = 5 75 | ball_x_speed = 0 76 | ball_y_speed = 0 77 | old_time = time.time() 78 | target_fps = 60 79 | score = 0 80 | ball_x = paddle.x+60 81 | ball_y = paddle.y-13 82 | point = 0 83 | z=0 84 | clr = ["#54FF9F","#8A2BE2","#CD3333","#7FFF00","#800080","#ADFF2F"] 85 | start=False 86 | 87 | def score_updater(): 88 | global score,score_text 89 | score+=1 90 | score_text = font.render(f'Score : {score}',True,"#FFFF00",(0,0,0)) 91 | 92 | def check_collision(): 93 | global screen_width,paddle,ball_x,ball_y,ball_x_speed,ball_y_speed,start_game,point,z 94 | #for fixing paddle from going outside the screen 95 | if paddle.x<0: 96 | paddle.x=0 97 | if paddle.x+120>screen_width: 98 | paddle.x = screen_width-120 99 | 100 | #for making ball from going outside the screen from left and right 101 | if ball_x-13<=0 or ball_x+13>=screen_width: 102 | ball_x_speed = -ball_x_speed 103 | if ball_y-13 <0: 104 | ball_y_speed = -ball_y_speed 105 | 106 | #when ball hit the paddle 107 | if ball_y+13>=paddle.y-5 and start_game==True and ball_x>paddle.x and ball_xpaddle.y: 114 | game_over() 115 | 116 | def game_over(): 117 | global run_game 118 | run_game = False 119 | 120 | def reset(): 121 | global ball_x,ball_y,score,score_text 122 | ball_x = paddle.x+60 123 | ball_y = paddle.y-13 124 | score =0 125 | score_text = font.render(f'Score : {score}',True,"#FFFF00",(0,0,0)) 126 | 127 | 128 | 129 | while True : 130 | new_time = time.time() #time after the frame change 131 | dt = new_time-old_time #time between frame change 132 | old_time = new_time 133 | 134 | for event in pg.event.get(): 135 | if event.type == pg.QUIT or (event.type==pg.KEYDOWN and event.key == pg.K_z): 136 | pg.quit() 137 | sys.exit() 138 | 139 | 140 | if event.type==pg.KEYDOWN: 141 | if event.key==pg.K_e: 142 | target_fps = 60 143 | elif event.key == pg.K_d: 144 | target_fps = 90 145 | 146 | if event.key==pg.K_r: 147 | run_game = True 148 | start_game = False 149 | reset() 150 | 151 | 152 | if event.key==pg.K_SPACE: 153 | start_game = True 154 | ball_y-=10 155 | sign = random.randint(0,1) 156 | ball_y_speed = -random.randint(min_ball_speed,max_ball_speed) 157 | ball_x_speed = random.randint(min_ball_speed,max_ball_speed) 158 | if sign ==0: 159 | ball_x_speed =-ball_x_speed 160 | 161 | if run_game==True: 162 | check_collision() 163 | key = pg.key.get_pressed() #the keys which are pressed stored in key variable 164 | if key[pg.K_RIGHT]: 165 | paddle.x+=paddle_speed*dt*target_fps 166 | if start_game==False: 167 | ball_x=paddle.x+60 168 | elif key[pg.K_LEFT]: 169 | paddle.x-=paddle_speed*dt*target_fps 170 | if start_game==False: 171 | ball_x=paddle.x+60 172 | 173 | win.fill("black") #insted of clearing background we fill color 174 | # win.blit(bg_img,(0,0)) 175 | pg.draw.rect(win,"white",paddle) #padlle will be drawn at the specified positions 176 | if start_game == False: 177 | win.blit(welcome_text,welcome_text_rect) 178 | win.blit(start_text,start_text_rect) 179 | pg.display.update() 180 | pg.draw.circle(win,"white",(ball_x,ball_y),13) 181 | else: 182 | ball_x+=ball_x_speed*dt*target_fps 183 | ball_y+=ball_y_speed*dt*target_fps 184 | pg.draw.circle(win,clr[point] ,(ball_x,ball_y),13) 185 | 186 | else: 187 | win.blit(game_over_text,game_over_text_rect) 188 | 189 | win.blit(score_text,score_text_Rect) 190 | win.blit(choice_text,choice_text_rect) 191 | win.blit(choice1_text,choice1_text_rect) 192 | win.blit(choice2_text,choice2_text_rect) 193 | win.blit(reset_text,reset_text_rect) 194 | win.blit(exit_text,exit_text_rect) 195 | pg.display.update() 196 | clock.tick(60) 197 | 198 | --------------------------------------------------------------------------------