Skip to content

Commit

Permalink
fix : selectionsort
Browse files Browse the repository at this point in the history
  • Loading branch information
shubha987 committed Oct 13, 2023
2 parents de95956 + da9363a commit 5aa1e6d
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 11 deletions.
17 changes: 7 additions & 10 deletions C/RadixSort.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>

// Function to find the maximum element in an array
int getMax(int arr[], int n) {
Expand All @@ -15,24 +16,20 @@ int getMax(int arr[], int n) {
void countSort(int arr[], int n, int exp) {
int *output = (int *)malloc(n * sizeof(int)); // Output array
int count[10] = {0};

// Count occurrences of each digit in count[]
for (int i = 0; i < n; i++) {
count[(arr[i] / exp) % 10]++;
}

// Change count[i] so that count[i] now contains
// actual position of this digit in output[]
// the actual position of this digit in output[]
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}

// Build the output array
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}

// Copy the output array to arr[], so that arr[] now
// contains sorted numbers according to the current digit
for (int i = 0; i < n; i++) {
Expand All @@ -47,7 +44,7 @@ void radixSort(int arr[], int n) {
int max = getMax(arr, n);

// Do counting sort for every digit. Note that
// instead of passing digit number, exp is passed.
// instead of passing the digit number, exp is passed.
// exp is 10^i where i is the current digit number
for (int exp = 1; max / exp > 0; exp *= 10) {
countSort(arr, n, exp);
Expand All @@ -64,11 +61,10 @@ void printArray(int arr[], int n) {

int main() {
int n;

printf("Enter the number of elements: ");
scanf("%d", &n);

int *arr = (int *)malloc(n * sizeof(int));


printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
Expand All @@ -77,12 +73,13 @@ int main() {

printf("Original array: ");
printArray(arr, n);

radixSort(arr, n);

printf("Sorted array: ");
printArray(arr, n);

// Free dynamically allocated memory
free(arr);

free(arr);
return 0;
}
39 changes: 39 additions & 0 deletions C/linearSearch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>

// Function to perform linear search
int linearSearch(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
if (arr[i] == target) {
return i; // Return the index where the target is found
}
}
return -1; // Return -1 if the target is not found in the array
}

int main() {
int n;

printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int arr[n]; // Declare an array of size 'n'

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

int target;
printf("Enter the element to search for: ");
scanf("%d", &target);

int result = linearSearch(arr, n, target);

if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}

return 0;
}
78 changes: 78 additions & 0 deletions CPP/knapsack algorithms/Fractional_Knapsack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//FRACTIONAL KNAPSACK

// greedy approach to solve the knapsack problem

#include<iostream>
using namespace std;

class greedy{
public:
int id;
float weight;
float profit;
float pw;
float amount;
void ratio(){
pw=profit/weight;
}


};

int main(){
int n;
float cap,totalm=0,totalp=0;
cout<<"enter the no.of objects:";
cin>>n;
cout<<"enter the bag capacity:";
cin>>cap;

greedy obj[n]; //creating an array of object of size n
for(int i=0;i<n;i++){
obj[i].id=i+1;
obj[i].amount=0;
cout<<"enter the weight of "<<i+1<<" : ";
cin>>obj[i].weight;
cout<<"enter the profit of "<<i+1<<" : ";
cin>>obj[i].profit;
}
for(int i=0;i<n;i++){ //finding the profit to weight ratio of each object
obj[i].ratio();
}

for(int i=0;i<n-1;i++){ //Sorting the objects in descending order according to profit to weight ratio
for(int j=0;j<n-1-i;j++){
if(obj[j].pw < obj[j+1].pw){
greedy temp=obj[j];
obj[j]=obj[j+1];
obj[j+1]=temp;
}
}
}

int i=0;
while(totalm<cap){ //Looping until totalweight of selected items doesn't become equal to capacity of bag
if (obj[i].weight <= (cap - totalm))
{
obj[i].amount = 1; //if object fits in the bag with remaining capacity then it is taken as whole
totalm = totalm + obj[i].weight;
totalp = totalp + obj[i].profit;
}
else if (obj[i].weight > (cap - totalm)) //else we take fraction of it
{
obj[i].amount = (cap - totalm) / (obj[i].weight);
totalm = totalm + (cap - totalm);
totalp = totalp + (obj[i].profit) * (obj[i].amount);
}
i++;
}

cout<<"\n\nfinal table is::\n"<<"id "<<"fract "<<"weight "<<"profit "<<"pw ratio\n";
for(int i=0;i<n;i++){
cout<<obj[i].id<<" "<<obj[i].amount<<" ";
cout<<obj[i].weight<<" ";
cout<<obj[i].profit<<" ";
cout<<obj[i].pw<<endl;
}
cout<<endl<<"total profit is::"<<totalp;
}
126 changes: 126 additions & 0 deletions CPP/knapsack algorithms/KnapsackDynamicApproach.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
Knapsack Algorithm Dynamic Approach
*/



#include<iostream>
#include<algorithm>
using namespace std;

class dclass{
public:
int w; //w stands for weight of object
int p; //p stands for profit of object
int id; //id is used to see wether object is used or not where intially its 0 means not used
};

int main(){
int n,cap;
cout<<"enter the no of objects:";
cin>>n;
cout<<"enter the capacity of bag:";
cin>>cap;

dclass obj[n+1]; //declaring array of objects and matrix

int mat[n+1][cap+1]; //initializing matrix with rows equal to no. of objects and columns equal to capacity of bag

obj[0].w=0; //here we initialize attributes of 0th object which is considered by us for easy calculations
obj[0].p=0;
obj[0].id=0;

for(int i=1;i<n+1;i++){
obj[i].id=0; //for putting id=0 for all objects to denote that no object is used till now;
cout<<"enter the weight of "<<i<<":";
cin>>obj[i].w;
cout<<"enter the profit of "<<i<<":";
cin>>obj[i].p;
}

for(int i=0;i<n;i++){ //sorting the objects in ascending order of weights
for(int j=0;j<n-i;j++){
if(obj[j].w >obj[j+1].w){
dclass temp=obj[j];
obj[j]=obj[j+1];
obj[j+1]=temp;
}
}
}

for(int j=0;j<cap+1;j++){ //initializing first row and first column as 0 because if capacity of bag is 0 profit will be 0 and if 0 objects are selected profit will be 0
if(j<n+1){
mat[j][0]=0;
}
mat[0][j]=0;
}

for(int i=1;i<n+1;i++){
for(int j=1;j<cap+1;j++){ //finding values of matrix
int p= mat[i-1][j];
int l= j-obj[i].w;
int q=0;
if(l<0){
q=0;
}
else{
q= mat[i-1][l]+obj[i].p;
}
mat[i][j]=max(p,q);
}
}


cout<<endl<<"matrix is::\n";
for(int i=0;i<n+1;i++){ //print matrix
for(int j=0;j<cap+1;j++){
cout<<mat[i][j]<<" ";
}cout<<endl;
}





int c=n,d=cap; //bottom up approach used to find which object are selected
int finalp=mat[c][d];
while(finalp!=0){
while (1)
{
if (mat[c - 1][d] == finalp)
{
c--;
}
else
{
obj[c].id = 1;
finalp = finalp - obj[c].p;
c--;
break;
}
}
for (int i = 0; i < cap + 1; i++)
{
if (mat[c][i] == finalp)
{
d = i;
break;
}
}
}


cout<<"\nweight "<<"profit "<<"remark \n";
for(int i=1;i<n+1;i++){
cout<<" "<<obj[i].w<<" "<<obj[i].p;
if(obj[i].id==1)
cout<<" Selected";
else
cout<<" -";
cout<<endl;
}

cout<<"\n\ntotal profit is::"<<mat[n][cap];

}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Take a look at CONTRIBUTING.md for guide to making contributions <br>
<li>Swap Alternate (C++)</li>
<li>Heap Sort(C)</li>
<li>Cocktail Sort(C)</li>
<li>Linear Search(C++)</li>
<li>Linear Search(C, C++)</li>
<li>Binary Search(C++)</li>
<li>Knapsack Algorithm(C++)</li>


0 comments on commit 5aa1e6d

Please sign in to comment.