├── 06-08-2023 ├── 05 -08-2023 Algothidm lab.cpp ├── Binary Search with recurtion.cpp ├── Binary Search.cpp └── sorting [ selection ].cpp ├── 10 - 09-2023 Lab ├── Factorial.c ├── Factorial.cpp ├── Fibonakki.c ├── Fibonakki.cpp ├── Problem 3.c └── Sum of Number.c ├── A-Exact sum.c ├── Extra Backup Lab ├── Knapsack 1.cpp ├── Knapsack.c ├── Parking.cpp ├── Teams Forming.cpp └── tESTING THE chature.cpp ├── Home Task 1 ├── A-Exact sum.c ├── B Next round.c ├── C wrong subtraction.c ├── Divinning Coin.c └── Longest common subsequence.c ├── Lab Task 9-9-2023.pdf ├── Lab Tast week-5 ├── Bin Packing.c ├── Coin Change.c └── Fractional k.c ├── Lab Test 1 [02-09-2023] ├── Binary search on array.c ├── Max element in the array.c ├── Summing Digits.c └── Train Swapping.c ├── Largest and Smallest in array.c ├── Leniar Search.c ├── Multipication rable.c ├── Negative Positive in array.c ├── Palindrome simple.c ├── Palindrome using recursion.c ├── Prime number.c ├── Python File ├── Largest And Small Number.py ├── Linear Searching.py └── Prime Number.py ├── README.md ├── Sorting ├── Bouble sort.c ├── Marge Sort.c ├── Quick Sort.c ├── Selection sort.c └── sorting [ selection ].cpp ├── lab 23-09-2023 ├── Knapshak.c └── coin change problem.c └── next alphabet.c /06-08-2023/05 -08-2023 Algothidm lab.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int linearSearch(int array[] , int n, int key) 5 | { 6 | for(int i=0 ; i> n; 22 | 23 | int array[n]; 24 | cout <<"Enter elements of array !!" <>array[i]; 29 | } 30 | 31 | cout <> key; 33 | 34 | int index = linearSearch(array ,n, key); 35 | 36 | if (index != 69) 37 | { 38 | cout << "Found in index!" < 2 | using namespace std; 3 | 4 | int Binary_search(int array[] ,int low , int high, int key) 5 | { 6 | 7 | if(low <= high) 8 | { 9 | int mid =low + (high - low) / 2 ; 10 | if(array[mid] == key) 11 | { 12 | return mid; 13 | } 14 | else if(array[mid] < key) 15 | { 16 | return Binary_search(array , mid +1 , high , key); 17 | } 18 | else 19 | { 20 | return Binary_search(array , low , mid-1 , key); 21 | } 22 | } 23 | return-1; 24 | } 25 | 26 | int main() 27 | { 28 | int n , key; 29 | cout <<"Enter size of array : "; 30 | cin >> n; 31 | 32 | int array[n]; 33 | for(int i=0 ; i>array[i]; 37 | } 38 | 39 | cout <<"Enter the key : "; 40 | cin >> key; 41 | 42 | int index = Binary_search(array ,0 ,n -1, key); 43 | 44 | if (index != -1) 45 | { 46 | cout << "Found in index "<< index +1<<" !" < 2 | using namespace std; 3 | 4 | int Binary_search(int array[] , int n, int key) 5 | { 6 | int low =0; 7 | int high = n-1 ; 8 | while(low <= high) 9 | { 10 | int mid =low + (high - low) / 2 ; 11 | if(array[mid] == key) 12 | { 13 | return mid; 14 | } 15 | else if(array[mid] < key) 16 | { 17 | low = mid +1; 18 | } 19 | else 20 | { 21 | high = mid -1; 22 | } 23 | } 24 | return-1; 25 | } 26 | 27 | int main() 28 | { 29 | int n , key; 30 | cout <<"Enter size of array : "; 31 | cin >> n; 32 | 33 | int array[n]; 34 | for(int i=0 ; i>array[i]; 38 | } 39 | 40 | cout <<"Enter the key : "; 41 | cin >> key; 42 | 43 | int index = Binary_search(array ,n, key); 44 | 45 | if (index != 69) 46 | { 47 | cout << "Found in index!" << index +1 < 2 | using namespace std; 3 | 4 | void swap(int *a, int *b) { 5 | int temp = *a; 6 | *a = *b; 7 | *b = temp; 8 | } 9 | 10 | void printArray(int array[], int size) { 11 | for (int i = 0; i < size; i++) { 12 | cout << array[i] << " "; 13 | } 14 | cout << endl; 15 | } 16 | 17 | void selectionSort(int array[], int size) { 18 | for (int step = 0; step < size - 1; step++) { 19 | int min_index = step; 20 | for (int i = step + 1; i < size; i++) { 21 | 22 | if (array[i] < array[min_index]) 23 | min_index = i; 24 | } 25 | swap(&array[min_index], &array[step]); // Swap function call this Linen for number selection swap 26 | } 27 | } 28 | 29 | int main() { 30 | int size; 31 | cout <<"Enter how many number you want to short : "; 32 | cin >>size; 33 | int array[size]; 34 | for(int i=0 ; i> array[i]; 37 | } 38 | selectionSort(array ,size ); 39 | cout << "Sorted array in Acsending Order: "; 40 | printArray(array, size); 41 | } 42 | -------------------------------------------------------------------------------- /10 - 09-2023 Lab/Factorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int result[1000] = {0}; 4 | 5 | int fact(int n) { 6 | if (n >= 0) { 7 | result[0] = 1; 8 | for (int i = 1; i <= n; ++i) { 9 | result[i] = i * result[i - 1]; 10 | } 11 | return result[n]; 12 | } 13 | } 14 | 15 | int main() { 16 | int n; 17 | while (1) { 18 | printf("Enter integer to compute factorial (enter 0 to exit): "); 19 | scanf("%d", &n); 20 | if (n == 0) 21 | break; 22 | printf("%d\n", fact(n)); 23 | } 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /10 - 09-2023 Lab/Factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int result[1000] = {0}; 4 | int fact(int n) { 5 | if (n >= 0) { 6 | result[0] = 1; 7 | for (int i = 1; i <= n; ++i) { 8 | result[i] = i * result[i - 1]; 9 | } 10 | return result[n]; 11 | } 12 | } 13 | int main() { 14 | int n; 15 | while (1) { 16 | cout<<"Enter integer to compute factorial (enter 0 to exit): "; 17 | cin>>n; 18 | if (n == 0) 19 | break; 20 | cout< 2 | 3 | int genFibonacci(int n) { 4 | int fibo[n + 2]; // array to store Fibonacci values 5 | // 0th and 1st number of the series are 0 and 1 6 | fibo[0] = 0; 7 | fibo[1] = 1; 8 | for (int i = 2; i <= n; i++) { 9 | fibo[i] = fibo[i - 1] + fibo[i - 2]; // Generate ith term using previous two terms 10 | } 11 | return fibo[n]; 12 | } 13 | 14 | int main() { 15 | int n; 16 | printf("Enter number of terms: "); 17 | scanf("%d", &n); 18 | printf("%dth Fibonacci Terms: %d\n", n, genFibonacci(n)); 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /10 - 09-2023 Lab/Fibonakki.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int genFibonacci(int n) 4 | { 5 | int fibo[n+2]; //array to store fibonacci values 6 | // 0th and 1st number of the series are 0 and 1 7 | fibo[0] = 0; 8 | fibo[1] = 1; 9 | for (int i = 2; i <= n; i++) { 10 | fibo[i] = fibo[i-1] + fibo[i-2]; //Generate ith term using previous two terms 11 | } 12 | return fibo[n]; 13 | } 14 | int main () 15 | { 16 | int n; 17 | cout << "Enter number of terms: "; 18 | cin >>n; 19 | cout << n<<"th Fibonacci Terms: "< 2 | 3 | int sumOfNumbers(int n) { 4 | int sum = 0; 5 | int* dp = (int*)malloc((n+1) * sizeof(int)); 6 | 7 | if (dp == NULL) { 8 | printf("Memory allocation failed.\n"); 9 | return -1; // Return an error code 10 | } 11 | 12 | dp[0] = 0; 13 | 14 | for (int i = 1; i <= n; i++) { 15 | dp[i] = dp[i - 1] + i; 16 | } 17 | 18 | sum = dp[n]; 19 | free(dp); // Free allocated memory 20 | 21 | return sum; 22 | } 23 | 24 | int main() { 25 | int n; 26 | printf("Enter a positive integer 'n': "); 27 | scanf("%d", &n); 28 | 29 | if (n <= 0) { 30 | printf("Please enter a positive integer.\n"); 31 | } else { 32 | int result = sumOfNumbers(n); 33 | if (result != -1) { 34 | printf("Sum of the first %d natural numbers is: %d\n", n, result); 35 | } 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /A-Exact sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int n,i,j; 6 | while (scanf("%d", &n) != EOF) { 7 | int prices[n]; 8 | for (i = 0; i < n; i++) { 9 | scanf("%d", &prices[i]); 10 | } 11 | int M ; 12 | scanf("%d", &M); 13 | int minDiff = M; 14 | int book1, book2; 15 | 16 | for (i = 0; i < n; i++) { 17 | for (j = i + 1; j < n; j++) { 18 | if (prices[i] + prices[j] == M) { 19 | int different = abs(prices[i] - prices[j]); 20 | if (different < minDiff) { 21 | minDiff = different; 22 | book1 = prices[i]; 23 | book2 = prices[j]; 24 | } 25 | } 26 | } 27 | } 28 | 29 | if (book1 > book2) { 30 | int temp = book1; 31 | book1 = book2; 32 | book2 = temp; 33 | } 34 | 35 | printf("Peter should buy books whose prices are %d and %d.\n\n", book1, book2); 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Extra Backup Lab/Knapsack 1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int NUM, W; 9 | cin >> NUM >> W; 10 | vector weightS(NUM + 1), valueS(NUM + 1); 11 | for (int i = 1; i <= NUM; i++) 12 | { 13 | cin >> weightS[i] >> valueS[i]; 14 | } 15 | vector> dp(NUM + 1, vector(W + 1, 0)); 16 | for (int i = 1; i <= NUM; i++) 17 | { 18 | for (int j = 0; j <= W; j++) 19 | { 20 | if (j < weightS[i]) 21 | { 22 | dp[i][j] = dp[i - 1][j]; 23 | } 24 | else 25 | { 26 | dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weightS[i]] + valueS[i]); 27 | } 28 | } 29 | } 30 | cout << dp[NUM][W] << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Extra Backup Lab/Knapsack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX_NUM 100 3 | #define MAX_W 1000 4 | long long maxValue(long long A, long long B) { 5 | return (A>B)?A:B; 6 | } 7 | int main() 8 | { 9 | int NUM,W; 10 | scanf("%d %d",&NUM,&W); 11 | 12 | int weights[MAX_NUM+1]; 13 | int values[MAX_NUM+1]; 14 | 15 | for (int i=1; i<= NUM;i++) 16 | { 17 | scanf("%d %d", &weights[i], &values[i]); 18 | } 19 | 20 | long long dp[MAX_NUM+1][MAX_W+1]; 21 | 22 | for (int i=0;i<=NUM;i++) 23 | { 24 | for (int j=0; j<=W;j++) 25 | { 26 | if (i==0||j==0) { 27 | dp[i][j]=0; 28 | } 29 | else if (weights[i]>j) 30 | { 31 | dp[i][j] = dp[i - 1][j]; 32 | } 33 | else 34 | 35 | { 36 | dp[i][j] = maxValue(dp[i - 1][j], dp[i - 1][j - weights[i]] + values[i]); 37 | } 38 | } 39 | } 40 | 41 | printf("%lld \n", dp[NUM][W]); 42 | 43 | return 0; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Extra Backup Lab/Parking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int value; 8 | cin >> value; //Here number take 9 | while (value--) 10 | { 11 | int pos; 12 | cin >> pos; 13 | vector positions(pos); 14 | 15 | for (int i = 0; i < pos; i++) 16 | { 17 | 18 | cin >> positions[i]; 19 | 20 | } 21 | 22 | sort(positions.begin(), positions.end()); 23 | 24 | cout << (positions[pos - 1] - positions[0]) * 2 << endl; 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Extra Backup Lab/Teams Forming.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int value; // Call a integer value 9 | 10 | cin >> value; //Take Data Input 11 | 12 | vector skill(value); 13 | 14 | for (int i = 0; i < value; i++) 15 | { 16 | cin >> skill[i]; 17 | } 18 | sort(skill.begin(), skill.end()); 19 | int totalNumber = 0; 20 | for (int i = 0; i < value; i += 2) 21 | { 22 | totalNumber += skill[i + 1] - skill[i]; 23 | } 24 | cout << totalNumber << endl; 25 | 26 | return 0; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Extra Backup Lab/tESTING THE chature.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int j=0; int i=1; 7 | int HIGH, testCase = 1; 8 | while (cin >>HIGH&&HIGH!= -1) 9 | { 10 | vector missile; 11 | 12 | missile.push_back(HIGH); 13 | 14 | while (cin >> HIGH && HIGH != -1) 15 | { 16 | missile.push_back(HIGH); 17 | } 18 | vector dp (missile.size(), 1); 19 | for ( i =1;i< missile.size();i++) 20 | { 21 | for ( j=0;j1) 33 | 34 | cout << endl; 35 | 36 | cout << "Test #" << testCase++ << ":" << endl; 37 | 38 | cout << " maximum possible interceptions: " < 2 | #include 3 | 4 | int main() { 5 | int n,i,j; 6 | while (scanf("%d", &n) != EOF) { 7 | int prices[n]; 8 | for (i = 0; i < n; i++) { 9 | scanf("%d", &prices[i]); 10 | } 11 | int M ; 12 | scanf("%d", &M); 13 | int minDiff = M; 14 | int book1, book2; 15 | 16 | for (i = 0; i < n; i++) { 17 | for (j = i + 1; j < n; j++) { 18 | if (prices[i] + prices[j] == M) { 19 | int different = abs(prices[i] - prices[j]); 20 | if (different < minDiff) { 21 | minDiff = different; 22 | book1 = prices[i]; 23 | book2 = prices[j]; 24 | } 25 | } 26 | } 27 | } 28 | 29 | if (book1 > book2) { 30 | int temp = book1; 31 | book1 = book2; 32 | book2 = temp; 33 | } 34 | 35 | printf("Peter should buy books whose prices are %d and %d.\n\n", book1, book2); 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Home Task 1/B Next round.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int n, k; 4 | scanf("%d %d", &n, &k); 5 | 6 | int scores[n]; 7 | for (int i = 0; i < n; i++) { 8 | scanf("%d", &score[i]); 9 | } 10 | 11 | int temp = 0; 12 | int k_scores = scores[k - 1]; 13 | 14 | for (int i = 0; i < n; i++) { 15 | if (scores[i] >= k_scores && scores[i] > 0) { 16 | temp++; 17 | } 18 | } 19 | 20 | printf("%d\n", temp); 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Home Task 1/C wrong subtraction.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int num, k; 4 | scanf("%d %d", &num, &k); 5 | 6 | for (int i = 0; i < k; i++) { 7 | if (num % 10 != 0) { 8 | num--; 9 | } else { 10 | num /= 10; 11 | } 12 | } 13 | 14 | printf("%d\n", num); 15 | 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Home Task 1/Divinning Coin.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int minDivision(int m, int coins[]) { 5 | int total = 0,i,j; 6 | for (i = 0; i < m; i++) { 7 | total += coins[i]; 8 | } 9 | 10 | int half_total = total / 2; 11 | int *dp = (int *)malloc((half_total + 1) * sizeof(int)); 12 | for (i = 0; i <= half_total; i++) { 13 | dp[i] = 0; 14 | } 15 | 16 | for (i = 0; i < m; i++) { 17 | for ( j = half_total; j >= coins[i]; j--) { 18 | dp[j] = (dp[j] > dp[j - coins[i]] + coins[i]) ? dp[j] : dp[j - coins[i]] + coins[i]; 19 | } 20 | } 21 | 22 | int difference = total - 2 * dp[half_total]; 23 | free(dp); 24 | return difference; 25 | } 26 | 27 | int main() { 28 | int N,i,j; 29 | scanf("%d", &N); 30 | 31 | for (i = 0; i < N; i++) { 32 | int m; 33 | scanf("%d", &m); 34 | 35 | int coins[100]; 36 | for (j = 0; j < m; j++) { 37 | scanf("%d", &coins[j]); 38 | } 39 | 40 | int difference = minDivision(m, coins); 41 | printf("%d\n", difference); 42 | } 43 | 44 | return 0; 45 | } 46 | 47 | 48 | -------------------------------------------------------------------------------- /Home Task 1/Longest common subsequence.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define size 1024 4 | char x[size], y[size]; 5 | int lcs[size][size]; 6 | int maxvalue(int a, int b) 7 | { 8 | return a > b ? a : b; 9 | } 10 | int main() 11 | { 12 | 13 | while(gets(x) && gets(y)) 14 | { 15 | 16 | int xlen = strlen(x); 17 | int ylen = strlen(y); 18 | 19 | for(int i = 1; i <= xlen; ++i) 20 | { 21 | for(int j = 1; j <= ylen; ++j) 22 | { 23 | if(x[i-1] == y[j-1]) 24 | lcs[i][j] = lcs[i-1][j-1] + 1; 25 | else 26 | lcs[i][j] = maxvalue(lcs[i-1][j], lcs[i][j-1]); 27 | } 28 | } 29 | 30 | 31 | printf("%d\n", lcs[xlen][ylen] ); 32 | 33 | } 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Lab Task 9-9-2023.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sajjadjim/Algorithms/93cbd3a00421512d757bd4663b2c4fe19a27f2a2/Lab Task 9-9-2023.pdf -------------------------------------------------------------------------------- /Lab Tast week-5/Bin Packing.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void BinPacking(int weights[], int numWeights, int binCapacity) 4 | { 5 | int bins = 0; 6 | int remainingCapacity = binCapacity; 7 | 8 | for (int i = 0; i < numWeights; i++) 9 | { 10 | if (weights[i] > remainingCapacity) 11 | { 12 | bins++; 13 | remainingCapacity = binCapacity - weights[i]; 14 | } else { 15 | remainingCapacity -= weights[i]; 16 | } 17 | } 18 | 19 | printf("\nOutput\nMinimum no of bins : %d\n", bins); 20 | } 21 | 22 | int compare(const void *a, const void *b) 23 | { 24 | return ((int)b - (int)a); 25 | } 26 | 27 | int main() 28 | { 29 | int numWeights; 30 | printf("No of weights to be packed : "); 31 | scanf("%d", &numWeights); 32 | 33 | int givenWeights[numWeights]; 34 | printf("Enter the weights: "); 35 | for (int i = 0; i < numWeights; i++) 36 | { 37 | scanf("%d", &givenWeights[i]); 38 | } 39 | 40 | int binCapacity; 41 | printf("Enter the bin capacity: "); 42 | scanf("%d", &binCapacity); 43 | 44 | qsort(givenWeights, numWeights, sizeof(int), compare); 45 | 46 | BinPacking(givenWeights, numWeights, binCapacity); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Lab Tast week-5/Coin Change.c: -------------------------------------------------------------------------------- 1 | #include 2 | int minCoins(int coins[], int numCoins, int total, int useCoins[]) { 3 | int dp[total + 1]; 4 | dp[0] = 0; 5 | 6 | for (int i = 1; i <= total; i++) { 7 | dp[i] = total + 1; 8 | for (int j = 0; j < numCoins; j++) { 9 | if (coins[j] <= i && dp[i - coins[j]] + 1 < dp[i]) { 10 | dp[i] = dp[i - coins[j]] + 1; 11 | useCoins[i] = coins[j]; 12 | } 13 | } 14 | } 15 | 16 | return dp[total]; 17 | } 18 | 19 | int main() { 20 | int numCoins, total; 21 | printf("Enter number of coins: "); 22 | scanf("%d", &numCoins); 23 | 24 | int coins[numCoins]; 25 | printf("Enter coin elements: "); 26 | for (int i = 0; i < numCoins; i++) { 27 | scanf("%d", &coins[i]); 28 | } 29 | 30 | printf("Enter given sum: "); 31 | scanf("%d", &total); 32 | 33 | int useCoins[total + 1]; 34 | int minCoin = minCoins(coins, numCoins, total, useCoins); 35 | 36 | printf("The coins are: "); 37 | int remainingSum = total; 38 | while (remainingSum > 0) { 39 | printf("%d ", useCoins[remainingSum]); 40 | remainingSum =remainingSum - useCoins[remainingSum]; 41 | } 42 | printf("\nMinimum number of coins: %d \n", minCoin); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Lab Tast week-5/Fractional k.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct { 4 | int weight; 5 | int profit; 6 | double ratio; 7 | } Item; 8 | 9 | int compare(const void *a, const void *b) { 10 | Item *itemA = (Item *)a; 11 | Item *itemB = (Item *)b; 12 | return (itemB->ratio - itemA->ratio); 13 | } 14 | 15 | double fractionalKnapsack(int weights[], int profits[], int numItems, int capacity) { 16 | Item items[numItems]; 17 | 18 | for (int i = 0; i < numItems; i++) { 19 | items[i].weight = weights[i]; 20 | items[i].profit = profits[i]; 21 | items[i].ratio = (double)profits[i] / weights[i]; 22 | } 23 | 24 | qsort(items, numItems, sizeof(Item), compare); 25 | 26 | double totalProfit = 0.0; 27 | int currentWeight = 0; 28 | 29 | for (int i = 0; i < numItems; i++) { 30 | if (currentWeight + items[i].weight <= capacity) { 31 | currentWeight += items[i].weight; 32 | totalProfit += items[i].profit; 33 | } else { 34 | int remainingCapacity = capacity - currentWeight; 35 | totalProfit += items[i].ratio * remainingCapacity; 36 | break; 37 | } 38 | } 39 | 40 | return totalProfit; 41 | } 42 | 43 | int main() { 44 | int numItems; 45 | printf("Enter the number of items: "); 46 | scanf("%d", &numItems); 47 | 48 | int givenWeights[numItems]; 49 | printf("Enter the weights: "); 50 | for (int i = 0; i < numItems; i++) { 51 | scanf("%d", &givenWeights[i]); 52 | } 53 | 54 | int givenProfits[numItems]; 55 | printf("Enter the profits: "); 56 | for (int i = 0; i < numItems; i++) { 57 | scanf("%d", &givenProfits[i]); 58 | } 59 | 60 | int knapsackCapacity=20; 61 | 62 | 63 | double maxProfit = fractionalKnapsack(givenWeights, givenProfits, numItems, knapsackCapacity); 64 | 65 | printf("Maximum profit: %.1f\n", maxProfit); 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Lab Test 1 [02-09-2023]/Binary search on array.c: -------------------------------------------------------------------------------- 1 | #include 2 | int binary_Search(int arry[], int N, int key) 3 | { 4 | int low = 0, high = N - 1; 5 | while (low <= high) { 6 | int mid = low + (high - low) / 2; 7 | if (arry[mid] == key) { 8 | return 1; 9 | } else if (arry[mid] < key) { 10 | low = mid + 1; 11 | } else { 12 | high = mid - 1; 13 | } 14 | } 15 | return 0; 16 | } 17 | 18 | int binary_S(int arry[], int low, int high, int key) 19 | { 20 | if (low <= high) { 21 | int mid = low + (high - low) / 2; 22 | if (arry[mid] == key) { 23 | return 1; 24 | } else if (arry[mid] < key) { 25 | return binary_S(arry, mid + 1, high, key); 26 | } else { 27 | return binary_S(arry, low, mid - 1, key); 28 | } 29 | } 30 | return 0; 31 | } 32 | 33 | int main() 34 | { 35 | int j, K; 36 | scanf("%d %d", &j, &K); 37 | 38 | int arry[j]; 39 | 40 | for (int i = 0; i < j; i++) { 41 | scanf("%d", &arry[i]); 42 | } 43 | 44 | 45 | int result_I = binary_Search(arry, j, K); 46 | 47 | 48 | int result_R = binary_S(arry, 0, j - 1, K); 49 | 50 | if (result_I || result_R) 51 | { 52 | printf("true \n"); 53 | } 54 | else 55 | { 56 | printf("false \n"); 57 | } 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Lab Test 1 [02-09-2023]/Max element in the array.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n; 5 | scanf("%d",&n); 6 | int num[n]; 7 | for(int i=0;i 2 | int sum_of_D(int n) 3 | { 4 | int sum = 0; 5 | while (n > 0) 6 | { 7 | sum += n % 10; 8 | n /= 10; 9 | } 10 | return sum; 11 | } 12 | 13 | int jim(int n) 14 | { 15 | while (n >= 10) 16 | { 17 | n = sum_of_D(n); 18 | } 19 | return n; 20 | } 21 | 22 | int main() { 23 | int n; 24 | while (1) { 25 | scanf("%d", &n); 26 | if (n == 0) 27 | { 28 | break; 29 | } 30 | int result =jim(n); 31 | printf("%d\n", result); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Lab Test 1 [02-09-2023]/Train Swapping.c: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | 8 | for (int i = 0; i < n; i++) { 9 | 10 | int numb; 11 | cin >> numb; 12 | int arr[numb]; 13 | for (int j = 0; j < numb; j++) { 14 | cin >> arr[j]; 15 | } 16 | int temp = 0, jim = 0; 17 | for (int j = 0; j < numb; j++) { 18 | for (int k = 0; k < numb - 1; k++) { 19 | if (arr[k] > arr[k + 1]) { 20 | temp = arr[k]; 21 | arr[k] = arr[k + 1]; 22 | arr[k + 1] = temp; 23 | jim++; 24 | } 25 | } 26 | } 27 | cout << "Optimal train swapping takes " << jim << " swaps." << endl; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Largest and Smallest in array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a[50],i,n,large,small; 6 | printf("How many elements:"); 7 | scanf("%d",&n); 8 | printf("Enter the Array:"); 9 | 10 | for(i=0;ilarge) 16 | large=a[i]; 17 | if(a[i] 4 | 5 | int main() 6 | { 7 | int arr[100]; 8 | int size, i, toSearch, found; 9 | 10 | printf("Enter size of array: "); 11 | scanf("%d", &size); 12 | 13 | /* Input elements of array */ 14 | printf("Enter elements in array: "); 15 | for(i=0; i 2 | int main() { 3 | int n; 4 | printf("Enter an integer: "); 5 | scanf("%d", &n); 6 | 7 | for (int i = 1; i <= 10; ++i) { 8 | printf("%d * %d = %d \n", n, i, n * i); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /Negative Positive in array.c: -------------------------------------------------------------------------------- 1 | #### 2 | #include 3 | int main() 4 | 5 | -------------------------------------------------------------------------------- /Palindrome simple.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | int num,r,sum=0,t; 6 | 7 | printf("Input a number: "); 8 | scanf("%d",&num); 9 | 10 | for(t=num;num!=0;num=num/10){ 11 | r=num % 10; 12 | sum=sum*10+r; 13 | } 14 | if(t==sum) 15 | printf("%d is a palindrome number.\n",t); 16 | else 17 | printf("%d is not a palindrome number.\n",t); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Palindrome using recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int reverse(int num); 4 | int isPalindrome(int num); 5 | int main() { 6 | int num; 7 | printf("Enter a number to check Palindrome: "); 8 | scanf("%d", & num); 9 | if (isPalindrome(num) == 1) { 10 | printf("The given number is a Palindrome"); 11 | } else { 12 | printf("The given number is not a Palindrome"); 13 | } 14 | return 0; 15 | } 16 | int isPalindrome(int num) { 17 | if (num == reverse(num)) { 18 | return 1; 19 | } 20 | return 0; 21 | } 22 | int reverse(int num) { 23 | int rem; 24 | static int sum = 0; 25 | if (num != 0) { 26 | rem = num % 10; 27 | sum = sum * 10 + rem; 28 | reverse(num / 10); 29 | } else 30 | return sum; 31 | return sum; 32 | } 33 | -------------------------------------------------------------------------------- /Prime number.c: -------------------------------------------------------------------------------- 1 | //C Program to check whether a number is prime or not 2 | #include 3 | int main() 4 | { 5 | int n; //Declare the nummber 6 | printf("Enter the number: "); 7 | scanf("%d",&n); //Initialize the number 8 | if(n == 1){ 9 | printf("1 is neither prime nor composite."); 10 | return 0; 11 | } 12 | int count = 0; //Declare a count variable 13 | for(int i = 2; i < n; i++) //Check for factors 14 | { 15 | if(n % i == 0) 16 | count++; 17 | } 18 | if(count == 0) //Check whether Prime or not 19 | { 20 | printf("%d is a prime number.", n); 21 | } 22 | else 23 | { 24 | printf("%d is not a prime number.", n); 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Python File/Largest And Small Number.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | a = [] # Create an empty list to store elements 3 | n = int(input("How many elements: ")) # Input the number of elements 4 | print("Enter the Array:") 5 | 6 | for _ in range(n): # Input the elements of the array 7 | a.append(int(input())) 8 | 9 | large = small = a[0] # Initialize variables to store largest and smallest elements 10 | 11 | for i in range(1, n): # Iterate through the elements of the array 12 | if a[i] > large: # Update largest element if current element is larger 13 | large = a[i] 14 | if a[i] < small: # Update smallest element if current element is smaller 15 | small = a[i] 16 | 17 | print("The largest element is", large) 18 | print("The smallest element is", small) 19 | 20 | if __name__ == "__main__": 21 | main() 22 | -------------------------------------------------------------------------------- /Python File/Linear Searching.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | arr = [] 3 | 4 | # Input size of array 5 | size = int(input("Enter size of array: ")) 6 | 7 | # Input elements of array 8 | print("Enter elements in array: ") 9 | for _ in range(size): 10 | arr.append(int(input())) 11 | 12 | # Input element to search 13 | to_search = int(input("\nEnter element to search: ")) 14 | 15 | # Assume that element does not exist in array 16 | found = False 17 | 18 | # Search for the element in the array 19 | for i in range(size): 20 | # If element is found in array, set found flag and terminate loop 21 | if arr[i] == to_search: 22 | found = True 23 | break 24 | 25 | # Print the result 26 | if found: 27 | print(f"\n{to_search} is found at index {i}") 28 | else: 29 | print(f"\n{to_search} is not found in the array") 30 | 31 | if __name__ == "__main__": 32 | main() 33 | -------------------------------------------------------------------------------- /Python File/Prime Number.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input("Enter the number: ")) # Input the number 3 | 4 | if n == 1: 5 | print("1 is neither prime nor composite.") 6 | return 7 | 8 | count = 0 # Initialize a count variable 9 | 10 | for i in range(2, n): # Check for factors 11 | if n % i == 0: 12 | count += 1 13 | 14 | if count == 0: # Check whether the number is prime or not 15 | print(f"{n} is a prime number.") 16 | else: 17 | print(f"{n} is not a prime number.") 18 | 19 | if __name__ == "__main__": 20 | main() 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Algorithms 2 | 3 | Code 4 | ![Opera](https://img.shields.io/badge/Opera-FF1B2D?style=for-the-badge&logo=Opera&logoColor=white) 5 | -------------------------------------------------------------------------------- /Sorting/Bouble sort.c: -------------------------------------------------------------------------------- 1 | // Bubble sort in C 2 | #include 3 | // perform the bubble sort 4 | void bubbleSort(int array[], int size) { 5 | // loop to access each array element 6 | for (int step = 0; step < size - 1; ++step) { 7 | // loop to compare array elements 8 | for (int i = 0; i < size - step - 1; ++i) { 9 | // compare two adjacent elements 10 | // change > to < to sort in descending order 11 | if (array[i] > array[i + 1]) { 12 | 13 | // swapping occurs if elements 14 | // are not in the intended order 15 | int temp = array[i]; 16 | array[i] = array[i + 1]; 17 | array[i + 1] = temp; 18 | } 19 | } 20 | } 21 | } 22 | // print array 23 | void printArray(int array[], int size) { 24 | for (int i = 0; i < size; ++i) { 25 | printf("%d ", array[i]); 26 | } 27 | printf("\n"); 28 | } 29 | 30 | int main() { 31 | int data[] = {-2, 45, 0, 11, -9}; 32 | 33 | // find the array's length 34 | int size = sizeof(data) / sizeof(data[0]); 35 | 36 | bubbleSort(data, size); 37 | 38 | printf("Sorted Array in Ascending Order:\n"); 39 | printArray(data, size); 40 | } 41 | -------------------------------------------------------------------------------- /Sorting/Marge Sort.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | void merge(int arr[], int l, int m, int r) 6 | { 7 | int i, j, k; 8 | int n1 = m - l + 1; 9 | int n2 = r - m; 10 | 11 | int L[n1], R[n2]; 12 | 13 | for (i = 0; i < n1; i++) 14 | L[i] = arr[l + i]; 15 | for (j = 0; j < n2; j++) 16 | R[j] = arr[m + 1 + j]; 17 | 18 | i = 0; 19 | j = 0; 20 | k = l; 21 | while (i < n1 && j < n2) 22 | { 23 | if (L[i] <= R[j]) 24 | { 25 | arr[k] = L[i]; 26 | i++; 27 | } 28 | else 29 | { 30 | arr[k] = R[j]; 31 | j++; 32 | } 33 | k++; 34 | } 35 | 36 | while (i < n1) 37 | { 38 | arr[k] = L[i]; 39 | i++; 40 | k++; 41 | } 42 | 43 | while (j < n2) 44 | { 45 | arr[k] = R[j]; 46 | j++; 47 | k++; 48 | } 49 | } 50 | 51 | void mergeSort(int arr[], int l, int r) 52 | { 53 | if (l < r) 54 | { 55 | int m = l + (r - l) / 2; 56 | mergeSort(arr, l, m); 57 | mergeSort(arr, m + 1, r); 58 | merge(arr, l, m, r); 59 | } 60 | } 61 | 62 | void printArray(int A[], int size) 63 | { 64 | for (int i = 0; i < size; i++) 65 | printf("%d ", A[i]); 66 | printf("\n"); 67 | } 68 | 69 | int main() 70 | { 71 | int arr_size; 72 | printf("Enter the size of the array: "); 73 | scanf("%d", &arr_size); 74 | 75 | int arr[arr_size]; 76 | printf("Enter %d elements:\n", arr_size); 77 | for (int i = 0; i < arr_size; i++) 78 | { 79 | scanf("%d", &arr[i]); 80 | } 81 | 82 | printf("Given array is:\n"); 83 | printArray(arr, arr_size); 84 | 85 | mergeSort(arr, 0, arr_size - 1); 86 | 87 | printf("\nSorted array is:\n"); 88 | printArray(arr, arr_size); 89 | 90 | return 0; 91 | 92 | } 93 | -------------------------------------------------------------------------------- /Sorting/Quick Sort.c: -------------------------------------------------------------------------------- 1 | // Quick sort in C 2 | #include 3 | 4 | // function to swap elements 5 | void swap(int *a, int *b) { 6 | int t = *a; 7 | *a = *b; 8 | *b = t; 9 | } 10 | 11 | // function to find the partition position 12 | int partition(int array[], int low, int high) { 13 | 14 | // select the rightmost element as pivot 15 | int pivot = array[high]; 16 | 17 | // pointer for greater element 18 | int i = (low - 1); 19 | 20 | // traverse each element of the array 21 | // compare them with the pivot 22 | for (int j = low; j < high; j++) { 23 | if (array[j] <= pivot) { 24 | 25 | // if element smaller than pivot is found 26 | // swap it with the greater element pointed by i 27 | i++; 28 | 29 | // swap element at i with element at j 30 | swap(&array[i], &array[j]); 31 | } 32 | } 33 | 34 | // swap the pivot element with the greater element at i 35 | swap(&array[i + 1], &array[high]); 36 | 37 | // return the partition point 38 | return (i + 1); 39 | } 40 | 41 | void quickSort(int array[], int low, int high) { 42 | if (low < high) { 43 | 44 | // find the pivot element such that 45 | // elements smaller than pivot are on left of pivot 46 | // elements greater than pivot are on right of pivot 47 | int pi = partition(array, low, high); 48 | 49 | // recursive call on the left of pivot 50 | quickSort(array, low, pi - 1); 51 | 52 | // recursive call on the right of pivot 53 | quickSort(array, pi + 1, high); 54 | } 55 | } 56 | 57 | // function to print array elements 58 | void printArray(int array[], int size) { 59 | for (int i = 0; i < size; ++i) { 60 | printf("%d ", array[i]); 61 | } 62 | printf("\n"); 63 | } 64 | 65 | // main function 66 | int main() { 67 | int data[] = {8, 7, 2, 1, 0, 9, 6}; 68 | 69 | int n = sizeof(data) / sizeof(data[0]); 70 | 71 | printf("Unsorted Array\n"); 72 | printArray(data, n); 73 | 74 | // perform quicksort on data 75 | quickSort(data, 0, n - 1); 76 | 77 | printf("Sorted array in ascending order: \n"); 78 | printArray(data, n); 79 | } 80 | -------------------------------------------------------------------------------- /Sorting/Selection sort.c: -------------------------------------------------------------------------------- 1 | // Selection sort in C 2 | #include 3 | 4 | // function to swap the the position of two elements 5 | void swap(int *a, int *b) { 6 | int temp = *a; 7 | *a = *b; 8 | *b = temp; 9 | } 10 | 11 | void selectionSort(int array[], int size) { 12 | for (int step = 0; step < size - 1; step++) { 13 | int min_idx = step; 14 | for (int i = step + 1; i < size; i++) { 15 | 16 | // To sort in descending order, change > to < in this line. 17 | // Select the minimum element in each loop. 18 | if (array[i] < array[min_idx]) 19 | min_idx = i; 20 | } 21 | 22 | // put min at the correct position 23 | swap(&array[min_idx], &array[step]); 24 | } 25 | } 26 | 27 | // function to print an array 28 | void printArray(int array[], int size) { 29 | for (int i = 0; i < size; ++i) { 30 | printf("%d ", array[i]); 31 | } 32 | printf("\n"); 33 | } 34 | 35 | // driver code 36 | int main() { 37 | int data[] = {20, 12, 10, 15, 2}; 38 | int size = sizeof(data) / sizeof(data[0]); 39 | selectionSort(data, size); 40 | printf("Sorted array in Acsending Order:\n"); 41 | printArray(data, size); 42 | } 43 | -------------------------------------------------------------------------------- /Sorting/sorting [ selection ].cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap(int *a, int *b) { 5 | int temp = *a; 6 | *a = *b; 7 | *b = temp; 8 | } 9 | 10 | void printArray(int array[], int size) { 11 | for (int i = 0; i < size; i++) { 12 | cout << array[i] << " "; 13 | } 14 | cout << endl; 15 | } 16 | 17 | void selectionSort(int array[], int size) { 18 | for (int step = 0; step < size - 1; step++) { 19 | int min_index = step; 20 | for (int i = step + 1; i < size; i++) { 21 | 22 | if (array[i] < array[min_index]) 23 | min_index = i; 24 | } 25 | swap(&array[min_index], &array[step]); // Swap function call this Linen for number selection swap 26 | } 27 | } 28 | 29 | int main() { 30 | int size; 31 | cout <<"Enter how many number you want to short : "; 32 | cin >>size; 33 | int array[size]; 34 | for(int i=0 ; i> array[i]; 37 | } 38 | selectionSort(array ,size ); 39 | cout << "Sorted array in Acsending Order: "; 40 | printArray(array, size); 41 | } 42 | -------------------------------------------------------------------------------- /lab 23-09-2023/Knapshak.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int max(int a, int b) { 4 | return (a > b) ? a : b; 5 | } 6 | 7 | int knapsack(int values[], int weights[], int n, int capacity) { 8 | int dp[n + 1][capacity + 1]; 9 | 10 | for(int i = 0; i <= n; i++) { 11 | for(int w = 0; w <= capacity; w++) { 12 | if(i == 0 || w == 0) { 13 | dp[i][w] = 0; 14 | } else if(weights[i - 1] <= w) { 15 | dp[i][w] = max(values[i - 1] + dp[i - 1][w - weights[i - 1]], dp[i - 1][w]); 16 | } else { 17 | dp[i][w] = dp[i - 1][w]; 18 | } 19 | } 20 | } 21 | return dp[n][capacity]; 22 | } 23 | 24 | int main() { 25 | int n; 26 | printf("Enter the number of items: "); 27 | scanf("%d", &n); 28 | int values[n]; 29 | int weights[n]; 30 | 31 | printf("Enter the values and weights of the item:\n"); 32 | 33 | for(int i = 0; i < n; i++) { 34 | scanf("%d %d", &values[i], &weights[i]); 35 | } 36 | int capacity; 37 | printf("Enter the knapsack capacity: "); 38 | scanf("%d", &capacity); 39 | int result = knapsack(values, weights, n, capacity); 40 | printf("The max value that can be obtained: %d\n", result); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /lab 23-09-2023/coin change problem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int min(int a, int b) 5 | { 6 | return (a > b) ? b : a; 7 | } 8 | 9 | int minCoins(int coins[], int n, int amount) 10 | { 11 | int dp[n + 1][amount + 1]; 12 | 13 | for (int i = 0; i <= amount; i++) 14 | { 15 | dp[0][i] = INT_MAX; 16 | } 17 | dp[0][0] = 0; 18 | 19 | for (int i = 1; i <= n; i++) 20 | { 21 | for (int j = 0; j <= amount; j++) 22 | { 23 | if (coins[i - 1] > j) 24 | { 25 | dp[i][j] = dp[i - 1][j]; 26 | } 27 | else 28 | { 29 | dp[i][j] = min(dp[i - 1][j], 1 + dp[i][j - coins[i - 1]]); 30 | } 31 | } 32 | } 33 | return dp[n][amount]; 34 | } 35 | 36 | int main() 37 | { 38 | int n; 39 | printf("Enter the number of coins demonstration: "); 40 | scanf("%d", &n); 41 | int coins[n]; 42 | 43 | printf("Enter the coins demonstration:\n"); 44 | 45 | for (int i = 0; i < n; i++) 46 | { 47 | scanf("%d", &coins[i]); 48 | } 49 | int amount; 50 | printf("Enter the amount of change: "); 51 | scanf("%d", &amount); 52 | int result = minCoins(coins, n, amount); 53 | if (result == INT_MAX) 54 | { 55 | printf("It's not possible to make the given amount of change with the given coins\n"); 56 | } 57 | else 58 | { 59 | printf("Minimum number of coins needed: %d\n", result); 60 | } 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /next alphabet.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int main() 5 | { 6 | //declaring the variables for the program 7 | char ch; 8 | 9 | //Taking the input character from the user 10 | printf("Enter any character: "); 11 | //Scanning the input provided from the user 12 | scanf("%c", &ch); 13 | 14 | //printing the successor of that character 15 | printf("\n"); 16 | if(ch>=65 && ch<90) 17 | printf("%c", ch+1); 18 | else if(ch>=97 && ch<122) 19 | printf("%c", ch+1); 20 | else if(ch==90) 21 | printf("%c", 65); 22 | else if(ch==122) 23 | printf("%c", 122); 24 | else 25 | printf("%c", ch); 26 | 27 | return 0; 28 | } 29 | --------------------------------------------------------------------------------