Skip to content

added Algos #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,13 @@ INSTITUTE/COMPANY : Maharshi Dayanand University <br>
DOMAIN: Python <br>

---
---

NAME : Shabaj Ansari <br>
GITHUB : [SHabaj-dev](https://github.com/SHabaj-dev)<br>
INSTITUTE/COMPANY : Galgotias University<br>
DOMAIN/LANGUGAE : C++ <br>

---
---

61 changes: 61 additions & 0 deletions CPP/Algos/SearchingAlgos/BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -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<iostream>

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;

}
44 changes: 44 additions & 0 deletions CPP/Algos/SearchingAlgos/LinearSeach.cpp
Original file line number Diff line number Diff line change
@@ -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<iostream>
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;
}
35 changes: 35 additions & 0 deletions CPP/Algos/SortingAlgos/BubbleSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Implemnting Bubble Sort.

Time Complexity = O(n^2);
Space Complexity = O(1);
*/

#include<iostream>
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;
}
Binary file added CPP/Algos/SortingAlgos/BubbleSort.exe
Binary file not shown.
39 changes: 39 additions & 0 deletions CPP/Algos/SortingAlgos/InsertionSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Implemnting the Insertion sort.
Time Complexity = O(n^2);
Space Complexity = O(1);
*/

#include<iostream>
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] << " ";
}
}
Binary file added CPP/Algos/SortingAlgos/InsertionSort.exe
Binary file not shown.
76 changes: 76 additions & 0 deletions CPP/Algos/SortingAlgos/SelectionSort.cpp
Original file line number Diff line number Diff line change
@@ -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<iostream>
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);


}