Kamrul13981

Untitled

Jun 29th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. //problem-1
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. void kamrul_insertionsort(int n, string kamrul_a[])
  6. {
  7.     int i, j;
  8.     string key;
  9.     for(int i=1; i<n; i++)
  10.     {
  11.         key=kamrul_a[i];
  12.         j=i-1;
  13.         while(j>=0 && kamrul_a[j]>key)
  14.         {
  15.             kamrul_a[j+1]=kamrul_a[j];
  16.             j=j-1;
  17.         }
  18.        kamrul_a[j+1]=key;
  19.     }
  20.  
  21.     for (int i=0; i < n; i++)
  22.     {
  23.         cout<<kamrul_a[i]<<"\n";
  24.     }
  25. }
  26.  
  27.  
  28.  
  29. void kamrul_binarysearch(int n, string kamrul_a[])
  30. {
  31.     string element;
  32.     cin>>element;
  33.  
  34.     int low,high,middle;
  35.     low=0;
  36.     high=n-1;
  37.  
  38.     while(low<=high)
  39.     {
  40.         middle=(low+high)/2;
  41.  
  42.         if(kamrul_a[middle]==element)
  43.         {
  44.             cout<<"\n\nsearching item found at index :"<<middle+1;
  45.  
  46.             return;
  47.         }
  48.  
  49.         else if(kamrul_a[middle]<element)
  50.         {
  51.             low = middle+1;
  52.         }
  53.         else if(kamrul_a[middle]>element)
  54.         {
  55.             high=middle-1;
  56.         }
  57.     }
  58.        cout<<"Element not found in the data set";
  59.  
  60. }
  61.  
  62. void kamrul_delete(int n, string kamrul_a[])
  63. {
  64.     int i,j,k;
  65.     for(i=0; i<n; ++i)
  66.         for(j=i+1; j<n;)
  67.         {
  68.             if(kamrul_a[i]==kamrul_a[j])
  69.             {
  70.                 for(k=j; k<n-1; ++k)
  71.                     kamrul_a[k]=kamrul_a[k+1];
  72.                 --n;
  73.             }
  74.             else
  75.                 ++j;
  76.  
  77.         }
  78.  
  79.     cout<<"\n";
  80.     for(i=0; i<n; ++i)
  81.         cout<<kamrul_a[i]<<" ";
  82.  
  83. }
  84.  
  85. int main()
  86. {
  87.     int n;
  88.     cout<<"Enter the size of array : ";
  89.     cin>>n;
  90.     cout<<"Enter the Array : \n";
  91.     string a[n];
  92.     for(int i=0; i<n; i++)
  93.     {
  94.         cin>>a[i];
  95.     }
  96.     cout<<"\n\nSorting elements are:"<<endl;
  97.     kamrul_insertionsort(n, a);
  98.      cout<<"\n\n Enter search element:"<<endl;
  99.    kamrul_binarysearch(n, a);
  100.     cout<<"\n\n deleting all duplicate elements:"<<endl;
  101.     kamrul_delete(n, a);
  102.     cout<<endl;
  103.  
  104.  
  105. }
  106.  
  107.  
  108.  
  109. //problem-2
  110. #include <iostream>
  111. #include <cstring>
  112. #include <algorithm>
  113.  
  114. using namespace std;
  115.  
  116. string arr[50];
  117. int x;
  118.  
  119. bool kamrul_compare(string a, string b)
  120.  { return (a.length() <= b.length()); }
  121.  
  122. void kamrul_split(string word)
  123. {
  124.  int size = word.length();
  125.  int i = 0, x = 0, j = 0;
  126.  string s;
  127.  
  128.  while(i <= size)
  129.  {
  130.   if(word[i] == ' ' || word[i] == '\0'){
  131.    s = word.substr(j, i - j);
  132.    arr[x] = s;
  133.    j = i + 1;
  134.    x++;
  135.   }
  136.   i++;
  137.  }
  138.  
  139.  stable_sort(arr, arr + x, kamrul_compare);
  140.  reverse(arr, arr + x);
  141.  
  142.  for (i = 0; i < x; ++i)
  143.  {
  144.   cout << arr[i];
  145.   if(i != (x - 1))
  146.    cout << " ";
  147.  }
  148.  
  149.  cout << endl;
  150. }
  151.  
  152. int main(int argc, char const *argv[])
  153. {
  154.  int n, i;
  155.  string word;
  156.  
  157.  cin >> n;
  158.   cin.ignore();
  159.  
  160.  while(n--)
  161.  {
  162.   getline(cin, word);
  163.   kamrul_split(word);
  164.  }
  165.  
  166.  return 0;
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment