diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ea3fda18..ae8983da 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -426,4 +426,13 @@ INSTITUTE/COMPANY : Maharshi Dayanand University
DOMAIN: Python
--- +--- + +NAME : Shabaj Ansari
+GITHUB : [SHabaj-dev](https://github.com/SHabaj-dev)
+INSTITUTE/COMPANY : Galgotias University
+DOMAIN/LANGUGAE : C++
+ +--- +--- diff --git a/CPP/Algos/SearchingAlgos/BinarySearch.cpp b/CPP/Algos/SearchingAlgos/BinarySearch.cpp new file mode 100644 index 00000000..509d9585 --- /dev/null +++ b/CPP/Algos/SearchingAlgos/BinarySearch.cpp @@ -0,0 +1,61 @@ +/*Implementing Binary Search + +Algo: +1: Start; +2: Initalize start = 0 ans end = size - 1; +3: Find mid = start + (end - start) / 2; +4: Compare the Target +5: IF tareget is greater then set start = mid + 1; + ELSE + mid - 1; +6: Repete Step 5 Until Target is not found. +7: END; + +Time Complexity is O(log n); +Space Complexity = O(1); +*/ +#include + +using namespace std; + +void readArray(int array[], int size){ + cout << "Enter the Elements of the Array : "<< endl; + for(int i = 0; i < size; i++){ + cin >> array[i]; + } +} + +int binarySearch(int array[], int size, int target){ + int start = 0; + int end = size - 1; + int mid; + + while(start <= end){ + mid = start + (end - start) / 2; + + if(array[mid] == target){ + return mid; + } + + if(array[mid] > target){ + end = mid - 1; + }else{ + start = mid + 1; + } + } + return -1; +} + +int main(){ + int size, target, index; + cin >> size; + int array[size]; + readArray(array, size); + cout << "Enter the target Number " << endl; + cin >> target; + + index = binarySearch(array, size, target); + + cout << "Target Present at index " << index << endl; + +} \ No newline at end of file diff --git a/CPP/Algos/SearchingAlgos/LinearSeach.cpp b/CPP/Algos/SearchingAlgos/LinearSeach.cpp new file mode 100644 index 00000000..3aadbc75 --- /dev/null +++ b/CPP/Algos/SearchingAlgos/LinearSeach.cpp @@ -0,0 +1,44 @@ +/*Program to implement Linear search Algo. + +Algo: +1: intialize target; +2: start traversing the array one by one and comparing each +with target. +3: if found return the index; +4: else repet step 2. +5:end + +Time Complexity = O(n^2); +Space Complexity = O(1); + +*/ +#include +using namespace std; + +void readArray(int array[], int size){ + for(int i = 0 ; i < size; i++){ + cin >> array[i]; + } +} + +int linearSearch(int array[], int size, int target){ + + for(int i = 0; i < size; i++){ + if(target == array[i]){ + return i; + } + } + return -1; +} + +int main(){ + int size, target; + cin >> size; + int array[size]; + readArray(array, size); + cin >> target; + + cout << "Element found at index " << linearSearch(array, size, target); + + return 0; +} \ No newline at end of file diff --git a/CPP/Algos/SortingAlgos/BubbleSort.cpp b/CPP/Algos/SortingAlgos/BubbleSort.cpp new file mode 100644 index 00000000..99fd6c5e --- /dev/null +++ b/CPP/Algos/SortingAlgos/BubbleSort.cpp @@ -0,0 +1,35 @@ +/* +Implemnting Bubble Sort. + +Time Complexity = O(n^2); +Space Complexity = O(1); +*/ + +#include +using namespace std; + +int main(){ + int size; + cin >> size; + int array[size]; + cout << "Enter the Elements : "<< endl; + + for(int i = 0; i < size; i++){ + cin >> array[size]; + } + + for(int i = 1; i < size; i++){ + for(int j = 0; j < size - i; j++){ + + if(array[j] > array[j + 1]){ + swap(array[j], array[j + 1]); + } + } + } + + for(int i = 0; i < size; i++){ + cout << array[i] << " "; + } + + return 0; +} \ No newline at end of file diff --git a/CPP/Algos/SortingAlgos/BubbleSort.exe b/CPP/Algos/SortingAlgos/BubbleSort.exe new file mode 100644 index 00000000..e70fb37f Binary files /dev/null and b/CPP/Algos/SortingAlgos/BubbleSort.exe differ diff --git a/CPP/Algos/SortingAlgos/InsertionSort.cpp b/CPP/Algos/SortingAlgos/InsertionSort.cpp new file mode 100644 index 00000000..df5a351c --- /dev/null +++ b/CPP/Algos/SortingAlgos/InsertionSort.cpp @@ -0,0 +1,39 @@ +/* +Implemnting the Insertion sort. +Time Complexity = O(n^2); +Space Complexity = O(1); +*/ + +#include +using namespace std; + +int main(){ + int size; + cin >> size; + int array[size]; + + cout << " Enter the Array : " << endl; + for(int i = 0; i < size; i++){ + cin >> array[i]; + } + int i = 1; + while(i < size){ + int temp = array[i]; + int j = i - 1; + while(j >= 0){ + + if(array[j] > temp){ + array[j + 1] = array[j]; + }else{ + break; + } + j--; + } + array[j + 1] = temp; + i++; + } + + for(int i = 0; i < size; i++){ + cout << array[i] << " "; + } +} \ No newline at end of file diff --git a/CPP/Algos/SortingAlgos/InsertionSort.exe b/CPP/Algos/SortingAlgos/InsertionSort.exe new file mode 100644 index 00000000..6bffd4d8 Binary files /dev/null and b/CPP/Algos/SortingAlgos/InsertionSort.exe differ diff --git a/CPP/Algos/SortingAlgos/SelectionSort.cpp b/CPP/Algos/SortingAlgos/SelectionSort.cpp new file mode 100644 index 00000000..8749f6fd --- /dev/null +++ b/CPP/Algos/SortingAlgos/SelectionSort.cpp @@ -0,0 +1,76 @@ +/* +Algorithm +Step 1 − Set MIN to location 0 +Step 2 − Search the minimum element in the list +Step 3 − Swap with value at location MIN +Step 4 − Increment MIN to point to next element +Step 5 − Repeat until list is sorted + +Pseudocode +procedure selection sort + list : array of items + n : size of list + + for i = 1 to n - 1 + set current element as minimum + min = i + + check the element to be minimum + + for j = i+1 to n + if list[j] < list[min] then + min = j; + end if + end for + + swap the minimum element with the current element + if indexMin != i then + swap list[min] and list[i] + end if + end for + +end procedure + +Time Complexity = O(n^2); +Space Complexity = O(1); +*/ + +#include +using namespace std; + +void readArray(int array[], int size){ + for(int i = 0 ; i < size; i++){ + cin >> array[i]; + } +} + +void printArray(int array[], int size){ + for(int i = 0 ; i < size; i++){ + cout << array[i] << " "; + } +} + +void selectionSort(int array[], int size){ + for(int i = 0; i < size - 1; i++){ + int minIndex = i; + + for(int j = i + 1; j < size; j++){ + if(array[j] < array[minIndex]){ + minIndex = j; + } + } + swap(array[minIndex], array[i]); + } +} + +int main(){ + int size; + cin >> size; + int array[size]; + cout << "Enter the Elements of the Array. " << endl; + readArray(array, size); + selectionSort(array, size); + printArray(array, size); + + +} \ No newline at end of file