KDT85

Untitled

Oct 27th, 2022
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void forLoop(int arr[], int min, int sizeofArray);
  5. void bubble(int arr[], int sizeOfUnsorted);
  6. void linearSearch(int sorted[],int sortedSize);
  7. void binarySearch(int sorted[],int sortedSize);
  8.  
  9.  
  10. int main()
  11. {
  12.     //the data
  13.     int unsorted[] = {36,5,-1,27,4454};
  14.     cout << unsorted[4] << endl; // prints correct value
  15.     int sizeOfUnsorted = sizeof(unsorted)/sizeof(unsorted[0]);
  16.     int sorted[] = {1,2,4,5,6,7,8,9,10};
  17.     int sortedSize = sizeof(sorted)/sizeof(sorted[0]);
  18.  
  19.     //comment or uncoment the functions below as needed
  20.  
  21.     bubble(unsorted,sizeOfUnsorted);
  22.     //linearSearch(sorted,sortedSize);
  23.     //binarySearch(sorted, sortedSize);
  24.     //forLoop(sorted,0,sortedSize);
  25.  
  26. }
  27.  
  28. void forLoop(int arr[], int min, int sizeofArray)
  29. //function to print out elements of an array
  30. {
  31.     for (int i = min; i < sizeofArray; i++)
  32.     {
  33.         cout << arr[i];
  34.         if (i < sizeofArray -1)
  35.         {
  36.             cout << ", ";
  37.         }
  38.     }
  39.     cout << endl;
  40.     return;
  41. }
  42. void bubble(int arr[], int sizeOfUnsorted)
  43. //function to sort an array using the bubble algorthym
  44. {
  45.     int finished = -1; // flag to check if its finished
  46.     int temp = 0;
  47.  
  48.     while (finished != 0) // while not finished
  49.     {
  50.         finished = 0; //set flag to 0, if no changes take place this breaks the loop
  51.         for (int i = 0; i < 5; i++)
  52.         {
  53.             if (arr[i] > arr[i+1])
  54.             {
  55.                 finished++; // if we make a change increase flag
  56.                 temp = arr[i];
  57.                 arr[i] = arr[i+1];
  58.                 arr[i+1] = temp;
  59.  
  60.             }
  61.         }
  62.     }
  63. /*     for (int j = 0; j < 5; j++)
  64.     {
  65.         cout << arr[j] << ", ";
  66.    
  67.     } */
  68.     cout << "Bubble sort output: ";
  69.     forLoop(arr,0,5);
  70. }
  71. void linearSearch(int sorted[],int sortedSize)
  72. {
  73.     int index = -1;
  74.     int search = 0;
  75.  
  76.     cout << "Enter a number: ";
  77.     cin >> search;
  78.     for (int i = 0; i < sortedSize; i++)
  79.     {
  80.         if (sorted[i] == search)
  81.         {
  82.             index = i;
  83.         }
  84.        
  85.     }
  86.     if (index > -1)
  87.     {
  88.         cout << index << endl;
  89.     }
  90.     else
  91.     {
  92.         cout << "Element not there!" << endl;
  93.  
  94.    
  95.     }
  96.     return;
  97.  
  98. }
  99. void binarySearch(int sorted[],int sortedSize)
  100. {
  101.     //setting up some variables
  102.     int target = 0;
  103.     int highest = sortedSize;
  104.     int lowest = 0;
  105.     int half = highest / 2;
  106.     int found= -1;
  107.  
  108.     //get the search target
  109.     forLoop(sorted, lowest, sortedSize); // call function to print array
  110.     cout << "Enter a number: ";
  111.     cin >> target;
  112.     //keep dividing the array untill we find the target
  113.     while (found == -1)
  114.     {
  115.     if (target > sorted[highest -1] || target < sorted[lowest])
  116.     {
  117.         cout << "Not found :(" << endl;
  118.         found = 1;
  119.         break;
  120.     }
  121.    
  122.         // find half way point        
  123.         half = (lowest + highest) / 2;
  124.  
  125.         //check if target is middle, second half or first half of the array
  126.         if (target == sorted[half])
  127.         {
  128.             cout << "Found " << sorted[half];
  129.             cout << " at index " << half << " :D" << endl;
  130.             break; //if we are lucky it is at half way point, so we break the loop
  131.             found = 1;
  132.         }
  133.         else if (target > sorted[half])
  134.         {
  135.             lowest = half;
  136.             forLoop(sorted, lowest, highest);// print search range
  137.    
  138.         }
  139.         else if (target < sorted[half])
  140.         {
  141.             highest = half;
  142.             forLoop(sorted, lowest, highest); //to visualise what the program is doing
  143.         }
  144.         else
  145.         {
  146.             cout << target << "was not found :(" << endl;
  147.         }
  148.     }
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment