Advertisement
Guest User

Question 1 code

a guest
Dec 7th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include<iostream>
  2. #include <stdio.h> /* printf, NULL */
  3. #include <stdlib.h> /* srand, rand */
  4. #include <time.h> /* time */
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. void selection_sort (int ar[], int size)
  9. {
  10. int min_ele_loc;
  11. for (int i = 0; i < size; ++i)
  12. {
  13. //Find minimum element in the unsorted array
  14. //Assume it's the first element
  15. min_ele_loc = i;
  16. //Loop through the array to find it
  17. for (int j = i + 1; j < size; ++j)
  18. if (ar[j] < ar[min_ele_loc])
  19. //Found new minimum position, if present
  20. min_ele_loc = j;
  21. //Swap the values
  22. int temp = ar[i];
  23. ar[i] = ar[min_ele_loc];
  24. ar[min_ele_loc] = temp;
  25. }
  26. }
  27.  
  28. int binary_search(int array[],int first,int last, int search_key)
  29. {
  30. int index;
  31.  
  32. if (first > last)
  33. index = -1;
  34.  
  35. else
  36. {
  37. int mid = (first + last)/2;
  38.  
  39. if (search_key == array[mid])
  40. index = mid;
  41. else
  42.  
  43. if (search_key < array[mid])
  44. index = binary_search(array,first, mid-1, search_key);
  45. else
  46.  
  47. index = binary_search(array, mid+1, last, search_key);
  48.  
  49. } // end if
  50. return index;
  51. }// end binarySearch
  52.  
  53. int linear_search(int a[],int size,int search_key){
  54. int i;
  55. for(i=0;i<size;i++)
  56. if(a[i] == search_key)
  57. return i;
  58. return -1;
  59. }
  60. int main(){
  61. ofstream myfile;
  62. int A[200],i,n;
  63. srand (time(NULL));
  64. for(i=0;i<200;i++){
  65. A[i] = rand()%200;
  66. printf("%d ",A[i]);
  67. }
  68. printf("\n");
  69. selection_sort(A,200);
  70. myfile.open ("output.txt");
  71. // myfile << "Writing this to a file.\n";
  72.  
  73. for(i=0;i<200;i++){
  74. // A[i] = rand()%200;
  75. printf("%d ",A[i]);
  76. myfile << A[i]<<" ";
  77. }
  78. // myfile.open ("output.txt");
  79. // myfile << "Writing this to a file.\n";
  80. //myfile.close();
  81. cout<<"Enter an integer in between 1 to 200 (inclusive):";
  82. cin>>n;
  83. int index = binary_search(A,0,200,n);
  84. if(index == -1){
  85. cout<<"element not found";
  86. myfile << "Element not found in Binary Search";
  87. }
  88. else {
  89. cout<< "binary search results for "<<n<<" is in the index : "<<index<<endl;
  90.  
  91. myfile<< "\nbinary search results for "<<n<<" is in the index : "<<index<<endl;
  92.  
  93. }
  94. index = linear_search(A,200,n);
  95. if(index == -1){
  96. cout<<"element not found";
  97. myfile << "Element not found in linear Search";
  98. }
  99. else{
  100. cout<< "linear search results for "<<n<<" is in the index : "<<index<<endl;
  101. myfile<< "\nlinear search results for "<<n<<" is in the index : "<<index<<endl;
  102. }
  103. myfile.close();
  104.  
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement