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
49 changes: 49 additions & 0 deletions DAA_Algo/DnC(MinNMax).c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include<iostream.h>
using namespace std;
void findMinAndMax(int arr[], int low, int high, int& min, int& max);
{
if (low == high)
{
if (max < arr[low])
max = arr[low];
if (min > arr[high])
min = arr[high];
return;
}
if (high - low == 1)
{
if (arr[low] < arr[high])
{
if (min > arr[low])
min = arr[low];
if (max < arr[high])
max = arr[high];
}
else
{

if (min > arr[high])
min = arr[high];
if (max < arr[low])
max = arr[low];

}

return;

}
int mid = (low + high) / 2;
findMinAndMax(arr, low, mid, min, max);
findMinAndMax(arr, mid + 1, high, min, max);
}
int main()
{
int arr[] = { 5, 2, 89, 1, 12, 4, 6, 1, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
int max = INT_MIN, min = INT_MAX;
findMinAndMax(arr, 0, n - 1, min, max);
cout << "The minimum element in the array is " << min << '\n';
cout << "The maximum element in the array is " << max;
return 0;
}

48 changes: 48 additions & 0 deletions DAA_Algo/DnC(MinNMax).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include<iostream>

using namespace std;

void findMinAndMax(int arr[], int low, int high, int& min, int& max)
{
if (low == high)
{
if (max < arr[low])
max = arr[low];
if (min > arr[high])
min = arr[high];
return;
}
if (high - low == 1)
{
if (arr[low] < arr[high])
{
if (min > arr[low])
min = arr[low];
if (max < arr[high])
max = arr[high];
}
else
{
if (min > arr[high])
min = arr[high];
if (max < arr[low])
max = arr[low];
}
return;
}
int mid = (low + high) / 2;
findMinAndMax(arr, low, mid, min, max);
findMinAndMax(arr, mid + 1, high, min, max);
}

int main()
{
int arr[] = { 55, 25, 78, 91, 12, 43, 67, 11, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
int max = INT_MIN, min = INT_MAX;
findMinAndMax(arr, 0, n - 1, min, max);
cout << "The minimum element in the array is " << min << '\n';
cout << "The maximum element in the array is " << max;
return 0;
}

53 changes: 53 additions & 0 deletions DAA_Algo/Knapsack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include<stdio.h>

int n = 3;
int c[10] = {10,20,30};
int v[10] = {60,100,120};
int W = 50;

void greedy() {

int cur_w;
float tot_v;
int i, maxi;

int used[10];

for (i = 0; i < n; ++i)
{
used[i] = 0;
}
cur_w = W;

while (cur_w > 0) {

maxi = -1;

for (i = 0; i < n; ++i)
if ((used[i] == 0) && ((maxi == -1) || ((float)v[i]/c[i] > (float)v[maxi]/c[maxi])))
{
maxi = i;
}

used[maxi] = 1;
cur_w -= c[maxi];
tot_v += v[maxi];

if (cur_w >=0)
{
printf("Added object %d (%d$, %dKg) completly in the bag. Space left: %d.\n", maxi + 1, v[maxi], c[maxi], cur_w);
}
else
{
printf("Added %d%% (%d$, %dKg) of object %d in the bag.\n", (int)((1 + (float)cur_w/c[maxi]) * 100), v[maxi], c[maxi], maxi + 1);
tot_v -= v[maxi];
tot_v += (1 + (float)cur_w/c[maxi]) * v[maxi];
}
}
printf("Filled the bag with objects worth %.2f$.\n", tot_v);
}

int main(int argc, char *argv[]) {
greedy();
return 0;
}
91 changes: 91 additions & 0 deletions DAA_Algo/MergeSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdlib.h>

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;
j = 0;
k = l;

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)
{
int m = l + (r - l) / 2;

mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

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

void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}

int main()
{

int arr[100],arr_size;
printf("Enter size of array: ");
scanf("%d",&arr_size);

int i;
printf("\nEnter array: ");
for (i=0; i<arr_size; i++)
{
scanf("%d",&arr[i]);
}

mergeSort(arr, 0, arr_size - 1);

printf("\nSorted array is \n");
printArray(arr, arr_size);
return 0;
}
60 changes: 60 additions & 0 deletions DAA_Algo/QuickSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>

using namespace std;

void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}

int divide (int array[], int first_index, int last_index)
{
int pivot = array[last_index];
int i = (first_index - 1);
for (int j = first_index; j <= last_index- 1; j++)
{
if (array[j] <= pivot)
{
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i + 1], &array[last_index]);
return (i + 1);
}

void quickSort(int array[], int first_index, int last_index)
{
if (first_index < last_index)
{
int divide_index = divide(array, first_index, last_index);
quickSort(array, first_index, divide_index - 1);
quickSort(array, divide_index + 1, last_index);
}
}

void display(int array[], int number)
{
int i;
cout<<"Sorted Array: ";
for (i = 0; i < number; i++)
cout << array[i] << " ";
cout << endl;
}

int main()
{
int arr[30], i, n;
cout<<"Enter the number of elements:";
cin>>n;
cout<<"Enter the elements: ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
quickSort(arr, 0, n-1);
display(arr, n);
return 0;
}
78 changes: 78 additions & 0 deletions c program/Circular LinkedList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include<stdio.h>
#include<stdlib.h>

struct node {
int data;
struct node *link;
};

typedef struct node C;
C *front=NULL, *rear = NULL, *temp;

void create() {
C *newnode;
newnode=(C*)malloc(sizeof(C));
printf("Enter node value: ");
scanf("%d", &newnode->data);
newnode->link = NULL;
if(rear==NULL) front = rear = newnode;
else {
rear->link = newnode;
rear = newnode;
}
rear->link = front;
}

void del() {
temp = front;
if(front == NULL) printf("\nUnderflow: ");
else {
if(front == rear) {
printf("\n%p", front->link);
front = rear = NULL;
}
else {
printf("\n%p", front->link);
front = front->link;
rear->link = front;
}
temp->link = NULL;
free(temp);
}
}

void display() {
temp = front;
if(front == NULL) printf("\nEmpty list");
else {
printf("\n");
for(; temp != rear; temp=temp->link)
printf("\n%d address=%p next=%p\t",temp->data,temp,temp->link);
printf("\n%d address=%p next=%p\n",temp->data,temp,temp->link);
}
}

int menu() {
int ch;
printf("Enter your choice: ");
scanf("%d", &ch);
char ch;
while( ( ch = getchar() ) != '\n' && ch != EOF );
return(ch);
}

int main() {
printf("\n1. Insert node\n2. Delete node\n3. Display list\n4. Exit\n");
while(1) {
switch(menu()) {
case 1: create();
break;
case 2: del();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nInvalid choice");
}
}
}
Loading