Skip to content
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

Create folder with name and add file in it #1

Open
wants to merge 4 commits 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
50 changes: 50 additions & 0 deletions Diptanshu Banerjee/BinarySearch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <stdio.h>

int binarySearch(int arr[], int n);

int main() {
int x;

printf("Enter the size of the array: \n");
scanf("%d", &x);

int arr[x];

printf("Enter the values in the array: \n");

for(int i = 0; i<x; i++) {
scanf("%d", &arr[i]);
}

int n;
printf("Enter the number to find\n");
scanf("%d", &n);

int result = binarySearch(arr, n);

if(result == -1) {
printf("%d is not in the array\n", n);
} else {
printf("%d is at position %d in the array\n", n, result);
}
return 0;
}

int binarySearch(int arr[], int n) {
int start = 0;
int end = sizeof(arr)-1;
int mid = (end+start)/2;

while(start <= end) {
if(arr[mid] == n) {
return mid;
} else if(arr[mid] > n) {
end = mid;
mid = ((end+start)/2)-1;
} else if(arr[mid] < n) {
start = mid;
mid = ((end + start)/2)+1;
}
}
return -1;
}
40 changes: 40 additions & 0 deletions Diptanshu Banerjee/bubbleSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>

void bubbleSort(int *arr, int length);

int main() {
int n;

printf("Enter the size of the array: \n");
scanf("%d", &n);

int arr[n];

printf("Enter the values of the array: \n");
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int length = sizeof(arr)/sizeof(int);

bubbleSort(arr, length);

for(int i = 0; i<n; i++) {
printf("%d, ", arr[i]);
}

return 0;
}

void bubbleSort(int *arr, int length) {

for(int i = 0; i <= length -2; i++) {
for(int j = 0; j <= length-1-i; j++) {
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
43 changes: 43 additions & 0 deletions Diptanshu Banerjee/linearSearch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdio.h>

int linearSearch(int *arr, int length, int key);

int main() {
int n;

printf("Enter the size of the array: \n");
scanf("%d", &n);

int arr[n];

printf("Enter the values of the array: \n");
for(int i = 0; i <= n-1; i++) {
scanf("%d", &arr[i]);
}

int key;
printf("Enter the value to find: \n");
scanf("%d", &key);

int length = sizeof(arr)/sizeof(int);

int result = linearSearch(arr, length, key);

if(result == -1) {
printf("%d is not in the array\n", key);
} else {
printf("%d is at position %d in the array\n", key, result);
}

return 0;
}

int linearSearch(int *arr, int length, int key) {
for(int i = 0; i<= length-1; i++) {
if(arr[i] == key) {
return i;
}
}

return -1;
}
40 changes: 40 additions & 0 deletions Diptanshu Banerjee/selectionSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>

void selectionSort(int *arr, int length);

int main() {
int n;
printf("Enter the size of the array: \n");
scanf("%d", &n);

int arr[n];

printf("Enter the values of the array: \n");
for(int i = 0; i <= n-1; i++) {
scanf("%d", &arr[i]);
}

int length = sizeof(arr)/sizeof(int);

selectionSort(arr, length);

for(int i = 0; i<=n-1; i++) {
printf("%d, ", arr[i]);
}
return 0;
}

void selectionSort(int *arr, int length) {
for(int i = 0; i<=length-2; i++) {
int min = i;
int j;
for(j = i+1; j <= length-1; j++) {
if(arr[j] < arr[min]) {
min = j;
}
}
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}