saykotislam

Untitled

Jun 29th, 2021
35
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. void insertionSort(string ar[],int n)
  4. {
  5. string key;
  6. int j=0;
  7. for(int i=1;i<n;i++){
  8. key=ar[i];
  9. j=i-1;
  10. while(j>=0 && ar[j]>key){
  11. ar[j+1]=ar[j];
  12. j=j-1;
  13. }
  14. ar[j+1]=key;
  15. }
  16. }
  17.  
  18. int search(string ar[],int n,string e)
  19. {
  20. int f,l,m;
  21. f=0;
  22. l=n-1;
  23.  
  24. while(f<=l){
  25. m=(f+l)/2;
  26. if(e==ar[m])
  27. return(m);
  28. else
  29. if(e>ar[m])
  30. f=m+1;
  31. else
  32. l=m-1;
  33. }
  34.  
  35. return -1;
  36.  
  37.  
  38. }
  39. int main()
  40. {
  41. int n;
  42. cout<<"Enter string number: ";
  43.  
  44. cin>>n;
  45. string ar[n];
  46. for(int i=0;i<n;i++){
  47. cin>>ar[i];
  48. }
  49. insertionSort(ar,n);
  50. cout<<"After sorting: "<<endl;
  51. for(int i=0;i<n;i++){
  52. cout<<ar[i]<<endl;
  53. }
  54. cout<<"How many data you want to search: "<<endl;
  55. int z;
  56. cin>>z;
  57. string e;
  58. for(int i=0;i<z;i++){
  59. cout<<"Case: "<<i+1<<endl;
  60. cout<<"\nEnter Data You want to search: ";
  61. cin>>e;
  62. int ans;
  63. ans=search(ar,n,e);
  64.  
  65. if(ans!=-1)
  66. cout<<e<<" is found at Index = "<<ans<<endl;
  67. else
  68. cout<<e<<" is not found in the Array";
  69. }
  70.  
  71.  
  72.  
  73. }
  74.  
RAW Paste Data