Guest User

Untitled

a guest
Jun 14th, 2022
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.91 KB | None | 0 0
  1. Insertion Sort:
  2. #include <stdio.h>
  3.  
  4. void insertionSort(int arr[], int n)
  5. {
  6. int i, key, j;
  7. int innerCounter = 0;
  8. int outerCounter = 0;
  9. for (i = 1; i < n; i++) {
  10. key = arr[i];
  11. j = i - 1;
  12.  
  13. while (j >= 0 && arr[j] > key) {
  14. arr[j + 1] = arr[j];
  15. j = j - 1;
  16.  
  17. innerCounter++;
  18. }
  19. arr[j + 1] = key;
  20. outerCounter++;
  21. }
  22. printf("In Insertion Sort value of \nOuterCounter = %d and InnerCounter = %d\n", outerCounter, innerCounter);
  23. printf("After Implementing Insertion Sort : ");
  24. }
  25.  
  26. void printArray(int arr[], int n)
  27. {
  28. int i;
  29. for (i = 0; i < n; i++)
  30. printf("%d ", arr[i]);
  31. printf("\n");
  32. }
  33.  
  34.  
  35. int main()
  36. {
  37. int U[] = {1, 2, 3, 4, 5, 6};
  38. int n = sizeof(U)/sizeof(U[0]);
  39.  
  40. int V[] = {6, 5, 4, 3, 2, 1};
  41. int m = sizeof(V)/sizeof(V[0]);
  42.  
  43. int W[] = {1, 3, 4, 5, 2, 6};
  44. int o = sizeof(W)/sizeof(W[0]);
  45.  
  46. printf(" ");
  47. printf("\nName: Kartik Yeole\n");
  48. printf("________________________________________________");
  49.  
  50. printf("\nGiven Array: W = ");
  51. printArray(W,o);
  52. insertionSort(W,o);
  53. printArray(W,o);
  54.  
  55. printf("\nGiven Array: U = ");
  56. printArray(U,n);
  57. insertionSort(U,n);
  58. printArray(U,n);
  59.  
  60. printf("\nGiven Array: V = ");
  61. printArray(V,m);
  62. insertionSort(V,m);
  63. printArray(V,m);
  64.  
  65.  
  66. printf("________________________________________________");
  67.  
  68.  
  69. return 0;
  70. }
  71.  
  72.  
  73.  
  74. Selection Sort:
  75.  
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78.  
  79. void selectionSort(int arr[], int n)
  80. {
  81. int i, j, min_idx, temp;
  82. int innerCounter = 0;
  83. int outerCounter = 0;
  84.  
  85. for (i = 0; i < n-1; i++)
  86. {
  87. min_idx = i;
  88. for (j = i+1; j < n; j++)
  89. {
  90. if (arr[j] < arr[min_idx])
  91. {
  92. min_idx = j;
  93. }
  94. innerCounter++;
  95. }
  96.  
  97. temp = arr[i];
  98. arr[i] = arr[min_idx];
  99. arr[min_idx] = temp;
  100.  
  101. outerCounter++;
  102. }
  103. printf("In Selection Sort value of \nOuterCounter = %d and InnerCounter = %d\n", outerCounter, innerCounter);
  104. printf("After Implementing Selection Sort : ");
  105. }
  106.  
  107. void printArray(int arr[], int n)
  108. {
  109. int i;
  110. for (i = 0; i < n; i++)
  111. printf("%d ", arr[i]);
  112. printf("\n");
  113. }
  114.  
  115. int main()
  116. {
  117. int U[] = {1, 2, 3, 4, 5, 6};
  118. int n = sizeof(U)/sizeof(U[0]);
  119.  
  120. int V[] = {6, 5, 4, 3, 2, 1};
  121. int m = sizeof(V)/sizeof(V[0]);
  122.  
  123. int W[] = {1, 3, 4, 5, 2, 6};
  124. int o = sizeof(W)/sizeof(W[0]);
  125.  
  126. printf(" ");
  127. printf("\nName: Kartik Yeole\n");
  128. printf("________________________________________________");
  129.  
  130. printf("\nGiven Array: W = ");
  131. printArray(W,o);
  132. selectionSort(W,o);
  133. printArray(W,o);
  134.  
  135. printf("\nGiven Array: U = ");
  136. printArray(U,n);
  137. selectionSort(U,n);
  138. printArray(U,n);
  139.  
  140. printf("\nGiven Array: V = ");
  141. printArray(V,m);
  142. selectionSort(V,m);
  143. printArray(V,n);
  144.  
  145. printf("________________________________________________");
  146. return 0;
  147. }
  148.  
  149. Recurrance Relation:
  150.  
  151. #include <bits/stdc++.h>
  152. #include <iostream>
  153. #include <string.h>
  154.  
  155. using namespace std;
  156.  
  157. class Quadratic{
  158.  
  159. public:
  160.     //print root of quadratic equation
  161.  
  162.     void findRoots(int a, int b, int c)
  163.     {
  164.         if(a == 0)
  165.         {
  166.             cout << "Invalid";
  167.             return;
  168.         }
  169.  
  170.         int d = b*b - 4*a*c;
  171.         float sqrt_val = sqrt(abs(d));
  172.  
  173.         //real and distinct roots
  174.         if(d > 0)
  175.         {
  176.             cout<< "Roots are real and different" << endl;
  177.             cout<< fixed << std::setprecision(1)
  178.                         << float((-b + sqrt_val) / (2 * a)) << endl;
  179.             cout << fixed << std::setprecision(1)
  180.                         << float((-b - sqrt_val) / (2 * a)) << endl;
  181.             cout << "The General Solution will be: " << endl;
  182.             cout << "t(n) = c1 " <<float((-b + sqrt_val) / (2 * a)) <<"^n + c2 "<< float((-b - sqrt_val) / (2 * a)) <<"^n"<< endl;
  183.         }
  184.  
  185.         else if(d == 0)
  186.         {
  187.             cout<< "Roots are real and same" << endl;
  188.             cout<< fixed << std::setprecision(1)
  189.                         << float((-b + sqrt_val) / (2 * a)) << endl;
  190.             cout << fixed << std::setprecision(1)
  191.                         << float((-b + sqrt_val) / (2 * a)) << endl;
  192.             cout << "The General Solution will be: " << endl;
  193.             cout << "t(n) = c1 * e^" <<float((-b + sqrt_val) / (2 * a)) <<"n + c2 * e^"<< float((-b - sqrt_val) / (2 * a)) <<"n"<< endl;
  194.         }
  195.  
  196.         //Imaginary roots
  197.         else if(d < 0)
  198.         {
  199.             cout << "Roots are complex" << endl;
  200.             cout << fixed << std::setprecision(1)
  201.                  << float(b / (2.0 * a)) << " + i"
  202.                  << sqrt_val << endl;
  203.             cout << fixed << std::setprecision(1)
  204.                  << float(b / (2.0 * a)) << " - i"
  205.                  << sqrt_val << endl;
  206.             cout << "The General Solution will be: " << endl;
  207.             cout << "t(n) = " << float(b / (2.0 * a))<<"^n( c1*cos(nx) + c2*sin(nx))" << endl;
  208.         }
  209.     }
  210.  
  211. };
  212.  
  213. int main()
  214. {
  215.     Quadratic obj;
  216.     int a, b, c;
  217.     cout << "Kartik Yeole" << endl;
  218.     cout << "Enter values of a, b, c" << endl;
  219.     cin >>a>>b>>c;
  220.     obj.findRoots(a,b,c);
  221.  
  222.     return 0;
  223. }
  224.  
  225.  
  226. Merge Sort:
  227.  
  228. #include <stdio.h>
  229. #include <stdlib.h>
  230.  
  231. void merge(int arr[], int l, int m, int r)
  232. {
  233. int i, j, k;
  234. int n1 = m - l + 1;
  235. int n2 = r - m;
  236.  
  237. int L[n1], R[n2];
  238.  
  239. for (i = 0; i < n1; i++)
  240. L[i] = arr[l + i];
  241. for (j = 0; j < n2; j++)
  242. R[j] = arr[m + 1 + j];
  243.  
  244.  
  245. i = 0;
  246. j = 0;
  247. k = l;
  248. while (i < n1 && j < n2) {
  249. if (L[i] <= R[j]) {
  250. arr[k] = L[i];
  251. i++;
  252. }
  253. else {
  254. arr[k] = R[j];
  255. j++;
  256. }
  257. k++;
  258. }
  259.  
  260. while (i < n1) {
  261. arr[k] = L[i];
  262. i++;
  263. k++;
  264. }
  265.  
  266.  
  267. while (j < n2) {
  268. arr[k] = R[j];
  269. j++;
  270. k++;
  271. }
  272. }
  273.  
  274. void mergeSort(int arr[], int l, int r)
  275. {
  276. if (l < r) {
  277. int m = l + (r - l) / 2;
  278.  
  279. mergeSort(arr, l, m);
  280. mergeSort(arr, m + 1, r);
  281.  
  282. merge(arr, l, m, r);
  283. }
  284. }
  285.  
  286. void printArray(int A[], int size)
  287. {
  288. int i;
  289. for (i = 0; i < size; i++)
  290. printf("%d ", A[i]);
  291. printf("\n");
  292. }
  293.  
  294.  
  295. int main()
  296. {
  297. int i, arr[10];
  298. for (i=0;i<=9;i++) {
  299. scanf("%d",&arr[i]);
  300. }
  301. int arr_size = sizeof(arr) / sizeof(arr[0]);
  302.  
  303. printf("");
  304. scanf("");
  305.  
  306. printf("Given array is \n");
  307. printArray(arr, arr_size);
  308.  
  309. mergeSort(arr, 0, arr_size - 1);
  310.  
  311. printf("\nSorted array is \n");
  312. printArray(arr, arr_size);
  313. return 0;
  314. }
  315.  
  316. Quick Sort:
  317.  
  318. #include <stdio.h>
  319.  
  320. void swap(int *a, int *b) {
  321. int t = *a;
  322. *a = *b;
  323. *b = t;
  324. }
  325.  
  326. int partition(int array[], int low, int high) {
  327.  
  328. int pivot = array[high];
  329.  
  330. int i = (low - 1);
  331.  
  332. for (int j = low; j < high; j++) {
  333. if (array[j] <= pivot) {
  334.  
  335.  
  336. i++;
  337.  
  338. swap(&array[i], &array[j]);
  339. }
  340. }
  341.  
  342. swap(&array[i + 1], &array[high]);
  343.  
  344. return (i + 1);
  345. }
  346.  
  347. void quickSort(int array[], int low, int high) {
  348. if (low < high) {
  349.  
  350. int pi = partition(array, low, high);
  351.  
  352. quickSort(array, low, pi - 1);
  353.  
  354. quickSort(array, pi + 1, high);
  355. }
  356. }
  357.  
  358. void printArray(int array[], int size) {
  359. for (int i = 0; i < size; ++i) {
  360. printf("%d ", array[i]);
  361. }
  362. printf("\n");
  363. }
  364.  
  365. int main() {
  366. int z, data[10];
  367. for (z=0;z<=10;z++) {
  368. scanf("%d",&data[z]);
  369. }
  370. int n = sizeof(data) / sizeof(data[0]);
  371.  
  372. printf("Unsorted Array\n");
  373. printArray(data, n);
  374.  
  375. quickSort(data, 0, n - 1);
  376.  
  377. printf("Sorted array in ascending order: \n");
  378. printArray(data, n);
  379. }
  380.  
  381. Knapsack:
  382.  
  383. #include<bits/stdc++.h>
  384. using namespace std;
  385.  
  386. int main()
  387. {
  388.     int n;
  389.     cout << "Enter the number of items in a knapsack: ";
  390.     cin >> n;
  391.     vector< pair<int,int> > w,v;
  392.     vector< pair<float,int> > knap;
  393.     for(int i=0;i<n;i++)
  394.     {
  395.         int value,weight;
  396.         cout << "Enter the value and weight: ";
  397.         cin >> value >>  weight;
  398.         w.push_back(make_pair(weight,i+1));
  399.         v.push_back(make_pair(value,i+1));
  400.         knap.push_back(make_pair((float)value/(float)weight, i+1));
  401.     }
  402.     sort(w.begin(), w.end());
  403.     sort(v.rbegin(), v.rend());
  404.     sort(knap.rbegin(), knap.rend());
  405.  
  406.     for(int i=0;i<n;i++)
  407.     {
  408.     //  cout << w[i].first << " " << w[i].second << endl;
  409.      // cout << v[i].first << " " << v[i].second << endl;
  410.        cout << knap[i].first << " " << knap[i].second << endl;
  411.     }
  412.     int num,W,sum=0;
  413.     cout << "Enter the maximum value of weight : ";
  414.     cin >> W;
  415.     cout << endl;
  416.     cout << "Choose the operation to perform: \n 1.Most valuable first \n 2.Lightest first \n 3.Largest value per weight \n";
  417.     cin >> num;
  418.  
  419.     switch(num)
  420.     {
  421.     case 1: //Most valuable first
  422.         for(int i=0;i<n;i++)
  423.         {
  424.             if(W > 0 && W >= w[v[i].second-1].first)
  425.             {
  426.  
  427.                 sum += v[i].first;
  428.                 W = W - w[v[i].second-1].first;
  429.             }
  430.             else if(W < w[v[i].second-1].first && W != 0)
  431.             {
  432.                 sum +=  ((w[v[i].second-1].first - W)*v[i].first)/w[v[i].second-1].first;
  433.                 W=0;
  434.             }
  435.             if(W == 0)
  436.                 break;
  437.         }
  438.         cout << "Max Value : " << sum << endl;
  439.         break;
  440.  
  441.     case 2:
  442.  
  443.         for(int i=0;i<n;i++)
  444.         {
  445.  
  446.           if(W > 0 && W >= w[i].first)
  447.              {
  448.                   for(int j=0;j<n;j++)
  449.                   {
  450.                       if(w[i].second == v[j].second)
  451.                     {
  452.                      sum += v[j].first;
  453.                      W -= w[i].first;
  454.                      cout << sum << " ";
  455.                      break;
  456.                     }
  457.                   }
  458.              }
  459.           else if (W > 0 && W < w[i].first)
  460.           {
  461.                for(int j=0;j<n;j++)
  462.                   {
  463.                       if(w[i].second == v[j].second)
  464.                     {
  465.                      sum += (W*v[j].first) / w[i].first;
  466.                     }
  467.                   }
  468.           }
  469.         }
  470.          cout << "Max Value : " << sum << endl;
  471.         break;
  472.  
  473.     case 3:
  474.          for(int i=0;i<n;i++)
  475.          {
  476.              for(int j=0;j<n;j++){
  477.                          if(knap[i].second == w[j].second)
  478.                          {
  479.                             if(W > 0 && w[j].first <= W){
  480.                              W -= w[j].first;
  481.                              for(int k=0;k<n;k++)
  482.                              {
  483.                                  if(knap[i].second == v[k].second)
  484.                                  {
  485.                                      sum += v[k].first;
  486.  
  487.                                  }
  488.                              }
  489.                             }
  490.                             else if(W > 0 && W < w[j].first)
  491.                             {
  492.                                 for(int k=0;k<n;k++)
  493.                              {
  494.                                  if(knap[i].second == v[k].second)
  495.                                  {
  496.                                      sum +=  (W * v[k].first) / w[j].first;
  497.                                      cout << W << " " << (w[j].first - W) * v[k].first << " ";
  498.                                      W=0;
  499.                                  }
  500.                              }
  501.                             }
  502.                          }
  503.          }
  504.          }
  505.           cout << "Max Value : " << sum << endl;
  506.         break;
  507.     }
  508. }
  509.  
  510.  
  511.  
  512. Floyd Warshall:
  513. #include<iostream>
  514. #include<iomanip>
  515. #define NODE 4
  516. #define INF 999
  517. using namespace std;
  518. //Cost matrix of the graph
  519. int costMat[NODE][NODE] = {
  520.    {0, INF, -2, INF},
  521.    {4, 0, 3, INF},
  522.    {INF, INF, 0, 2},
  523.    {INF, -1, INF, 0},
  524. };
  525. void floydWarshal(){
  526.    int cost[NODE][NODE]; //defind to store shortest distance from any node to any node
  527.    for(int i = 0; i<NODE; i++)
  528.       for(int j = 0; j<NODE; j++)
  529.          cost[i][j] = costMat[i][j]; //copy costMatrix to new matrix
  530.          for(int k = 0; k<NODE; k++){
  531.             for(int i = 0; i<NODE; i++)
  532.                for(int j = 0; j<NODE; j++)
  533.                   if(cost[i][k]+cost[k][j] < cost[i][j])
  534.                      cost[i][j] = cost[i][k]+cost[k][j];
  535.    }
  536.    cout << "The matrix:" << endl;
  537.    for(int i = 0; i<NODE; i++){
  538.       for(int j = 0; j<NODE; j++)
  539.          cout << setw(3) << cost[i][j];
  540.       cout << endl;
  541.    }
  542. }
  543. int main(){
  544.    floydWarshal();
  545. }
  546.  
  547.  
  548. TSP:
  549.  
  550. #include <bits/stdc++.h>
  551. using namespace std;
  552. #define V 4
  553.  
  554. // implementation of traveling Salesman Problem
  555. int travllingSalesmanProblem(int graph[][V], int s)
  556. {
  557.     // store all vertex apart from source vertex
  558.     vector<int> vertex;
  559.     for (int i = 0; i < V; i++)
  560.         if (i != s)
  561.             vertex.push_back(i);
  562.  
  563.     // store minimum weight Hamiltonian Cycle.
  564.     int min_path = INT_MAX;
  565.     do {
  566.  
  567.         // store current Path weight(cost)
  568.         int current_pathweight = 0;
  569.  
  570.         // compute current path weight
  571.         int k = s;
  572.         for (int i = 0; i < vertex.size(); i++) {
  573.             current_pathweight += graph[k][vertex[i]];
  574.             k = vertex[i];
  575.         }
  576.         current_pathweight += graph[k][s];
  577.  
  578.         // update minimum
  579.         min_path = min(min_path, current_pathweight);
  580.  
  581.     } while (
  582.         next_permutation(vertex.begin(), vertex.end()));
  583.  
  584.     return min_path;
  585. }
  586.  
  587. // Driver Code
  588. int main()
  589. {
  590.     // matrix representation of graph
  591.     int graph[][V] = { { 0, 10, 15, 20 },
  592.                        { 10, 0, 35, 25 },
  593.                        { 15, 35, 0, 30 },
  594.                        { 20, 25, 30, 0 } };
  595.     int s = 0;
  596.     cout << "The minimum cost path is: "<< travllingSalesmanProblem(graph, s) << endl;
  597.     return 0;
  598. }
  599.  
  600.  
  601.  
  602.  
  603. Graph Color:
  604.  
  605. #include <iostream>
  606. using namespace std;
  607.  
  608. #define V 6
  609.  
  610. void printSolution(int color[]);
  611.  
  612. bool isSafe(int v, bool graph[V][V],
  613.             int color[], int c)
  614. {
  615.     for(int i = 0; i < V; i++)
  616.         if (graph[v][i] && c == color[i])
  617.             return false;
  618.  
  619.     return true;
  620. }
  621.  
  622. bool graphColoringUtil(bool graph[V][V], int m,
  623.                     int color[], int v)
  624. {
  625.  
  626.     if (v == V)
  627.         return true;
  628.  
  629.     for(int c = 1; c <= m; c++)
  630.     {
  631.  
  632.         if (isSafe(v, graph, color, c))
  633.         {
  634.             color[v] = c;
  635.  
  636.             if (graphColoringUtil(
  637.                 graph, m, color, v + 1) == true)
  638.                 return true;
  639.  
  640.             color[v] = 0;
  641.         }
  642.     }
  643.  
  644.     return false;
  645. }
  646.  
  647. bool graphColoring(bool graph[V][V], int m)
  648. {
  649.  
  650.     int color[V];
  651.     for(int i = 0; i < V; i++)
  652.         color[i] = 0;
  653.  
  654.     if (graphColoringUtil(graph, m, color, 0) == false)
  655.     {
  656.         cout << "Solution does not exist";
  657.         return false;
  658.     }
  659.  
  660.     printSolution(color);
  661.     return true;
  662. }
  663.  
  664. void printSolution(int color[])
  665. {
  666.     cout << "Solution Exists:"
  667.         << " Following are the assigned colors"
  668.         << "\n";
  669.     for(int i = 0; i < V; i++)
  670.         cout << " " << color[i] << " ";
  671.  
  672.     cout << "\n";
  673. }
  674.  
  675. int main()
  676. {
  677.  
  678.     bool graph[V][V] = { { 0, 1, 1, 0, 0, 0 },
  679.                          { 1, 0, 1, 1, 0, 0 },
  680.                          { 1, 1, 0, 1, 1, 1 },
  681.                          { 0, 1, 1, 0, 1, 0 },
  682.                          { 0, 0, 1, 1, 0, 1 },
  683.                          { 0, 0, 1, 0, 1, 0 },};
  684.  
  685.     int m = 3;
  686.     graphColoring(graph, m);
  687.     return 0;
  688. }
  689.  
  690.  
  691.  
  692. 8 Queen’s:
  693.  
  694. #include <bits/stdc++.h>
  695. #define N 8
  696. using namespace std;
  697.  
  698. void printSolution(int board[N][N])
  699. {
  700.     for (int i = 0; i < N; i++) {
  701.         for (int j = 0; j < N; j++)
  702.             cout << " " << board[i][j] << " ";
  703.         printf("\n");
  704.     }
  705. }
  706.  
  707. bool isSafe(int board[N][N], int row, int col)
  708. {
  709.     int i, j;
  710.  
  711.     for (i = 0; i < col; i++)
  712.         if (board[row][i])
  713.             return false;
  714.  
  715.     for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
  716.         if (board[i][j])
  717.             return false;
  718.  
  719.     for (i = row, j = col; j >= 0 && i < N; i++, j--)
  720.         if (board[i][j])
  721.             return false;
  722.  
  723.     return true;
  724. }
  725.  
  726. bool solveNQUtil(int board[N][N], int col)
  727. {
  728.  
  729.     if (col >= N)
  730.         return true;
  731.  
  732.     for (int i = 0; i < N; i++) {
  733.  
  734.         if (isSafe(board, i, col)) {
  735.             board[i][col] = 1;
  736.  
  737.              if (solveNQUtil(board, col + 1))
  738.                 return true;
  739.  
  740.             board[i][col] = 0; // BACKTRACK
  741.         }
  742.     }
  743.  
  744.     return false;
  745. }
  746.  
  747. bool solveNQ()
  748. {
  749.     int board[N][N] = { { 0, 0, 0, 0, 0, 0, 0, 0 },
  750.                         { 0, 0, 0, 0, 0, 0, 0, 0 },
  751.                         { 0, 0, 0, 0, 0, 0, 0, 0 },
  752.                         { 0, 0, 0, 0, 0, 0, 0, 0 },
  753.                         { 0, 0, 0, 0, 0, 0, 0, 0 },
  754.                         { 0, 0, 0, 0, 0, 0, 0, 0 },
  755.                         { 0, 0, 0, 0, 0, 0, 0, 0 },
  756.                         { 0, 0, 0, 0, 0, 0, 0, 0 } };
  757.  
  758.     if (solveNQUtil(board, 0) == false) {
  759.         cout << "Solution does not exist";
  760.         return false;
  761.     }
  762.  
  763.     printSolution(board);
  764.     return true;
  765. }
  766.  
  767. int main()
  768. {
  769.     solveNQ();
  770.     return 0;
  771. }
  772.  
  773.  
  774.  
  775.  
Advertisement
Add Comment
Please, Sign In to add comment