Skip to content
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
40 changes: 40 additions & 0 deletions C/Bell.c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>

int main(void) {
int t;
scanf("%d",&t);
int u[100];
int v[100];
int w[100];
for(int i=0;i<t;i++){
scanf("%d",&u[i]);
scanf("%d",&v[i]);
scanf("%d",&w[i]);
}
int V;
scanf("%d",&V);
int arr[V];
for(int i=0;i<V;i++){
arr[i]=2147483647;
}
int src;
scanf("%d",&src);
arr[src]=0;
for(int j=0;j<V-1;j++){
for(int k=0;k<t;k++){
if(arr[u[k]]!=2147483647 && arr[u[k]]+w[k] < arr[v[k]]){
arr[v[k]] = arr[u[k]] + w[k];
}
}
}
for(int p=0;p<1;p++){
if(arr[u[p]]!=2147483647 && arr[u[p]]+w[p] < arr[v[p]]){
printf("Negative cycle detected");
}
}
for(int i=0;i<V;i++){
printf("%d",arr[i]);
}
return 0;
}

11 changes: 11 additions & 0 deletions C/D&CFact.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include<stdio.h>
int main(){
int n,fact=1;
printf("Enter the no\n");
scanf("%d",&n);
for(int i=1;i<=n;i++){
fact = fact * i;
}
printf("%d",fact);
return 0;
}
59 changes: 59 additions & 0 deletions C/maxSub.c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <stdio.h>
int MaxSubArraySum(int *,int,int);
int submidsum(int,int,int,int *);
int max(int,int,int);
int main(void) {
int n;
scanf("%d",&n);
int arr[100];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int largest = MaxSubArraySum(arr,arr[0],arr[n-1]);
printf("%d",largest);

return 0;
}

int MaxSubArraySum(int* arr,int l,int r){
if(l==r){
return(l);
}
int mid = (l+r)/2;
int largest_sum = max(submidsum(mid,l,r,arr),MaxSubArraySum(arr,l,mid),MaxSubArraySum(arr,mid+1,r));
return largest_sum;
}
int submidsum(int m,int l,int r,int* arr){
int temp= 0;
int temp1 =0;
int left_sum= arr[m];
for(int i=m;i>l-1;i--){
temp = temp + arr[i];
if(temp>left_sum){
left_sum = temp;
}
}
int right_sum = arr[m+1];
for(int j=m+1;j<r+1;j++){
temp1=temp1+arr[j];
if(temp1>right_sum){
right_sum=temp1;
}
}
int sub_sum = left_sum + right_sum;
return sub_sum;
}
int max(int a, int b, int c){
int d[3] = {a,b,c};
d[0]=a;
d[1]=b;
d[2]=c;
int max=d[0];
for(int i=1;i<3;i++){
if(max<d[i]){
max=d[i];
}
}
return max;
}

105 changes: 105 additions & 0 deletions C/mer-ins.c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// C program for insertion sort
#include <stdio.h>
#include <math.h>
#include<stdlib.h>

/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
void print(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main()
{
int arr[] = {12, 11, 13, 5, 6,1,7,2,90,45,65,66,78,87,45,34,12,32,345,56};
int n = sizeof(arr)/sizeof(arr[0]);
if(n<20){
insertionSort(arr, n);
printf("insertionSort implemented\n");
print(arr, n);
}
else{
mergeSort(arr, 0, n - 1);
printf("mergeSort\n");
print(arr, n);

return 0;
}}

void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;


int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;

// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);

merge(arr, l, m, r);
}
}

105 changes: 105 additions & 0 deletions mer-ins.c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// C program for insertion sort
#include <stdio.h>
#include <math.h>
#include<stdlib.h>

/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
void print(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main()
{
int arr[] = {12, 11, 13, 5, 6,1,7,2,90,45,65,66,78,87,45,34,12,32,345,56};
int n = sizeof(arr)/sizeof(arr[0]);
if(n<20){
insertionSort(arr, n);
printf("insertionSort implemented\n");
print(arr, n);
}
else{
mergeSort(arr, 0, n - 1);
printf("mergeSort\n");
print(arr, n);

return 0;
}}

void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;


int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;

// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);

merge(arr, l, m, r);
}
}