Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void forLoop(int arr[], int min, int sizeofArray);
- void bubble(int arr[], int sizeOfUnsorted);
- void linearSearch(int sorted[],int sortedSize);
- void binarySearch(int sorted[],int sortedSize);
- int main()
- {
- //the data
- int unsorted[] = {36,5,-1,27,4454};
- cout << unsorted[4] << endl; // prints correct value
- int sizeOfUnsorted = sizeof(unsorted)/sizeof(unsorted[0]);
- int sorted[] = {1,2,4,5,6,7,8,9,10};
- int sortedSize = sizeof(sorted)/sizeof(sorted[0]);
- //comment or uncoment the functions below as needed
- bubble(unsorted,sizeOfUnsorted);
- //linearSearch(sorted,sortedSize);
- //binarySearch(sorted, sortedSize);
- //forLoop(sorted,0,sortedSize);
- }
- void forLoop(int arr[], int min, int sizeofArray)
- //function to print out elements of an array
- {
- for (int i = min; i < sizeofArray; i++)
- {
- cout << arr[i];
- if (i < sizeofArray -1)
- {
- cout << ", ";
- }
- }
- cout << endl;
- return;
- }
- void bubble(int arr[], int sizeOfUnsorted)
- //function to sort an array using the bubble algorthym
- {
- int finished = -1; // flag to check if its finished
- int temp = 0;
- while (finished != 0) // while not finished
- {
- finished = 0; //set flag to 0, if no changes take place this breaks the loop
- for (int i = 0; i < 5; i++)
- {
- if (arr[i] > arr[i+1])
- {
- finished++; // if we make a change increase flag
- temp = arr[i];
- arr[i] = arr[i+1];
- arr[i+1] = temp;
- }
- }
- }
- /* for (int j = 0; j < 5; j++)
- {
- cout << arr[j] << ", ";
- } */
- cout << "Bubble sort output: ";
- forLoop(arr,0,5);
- }
- void linearSearch(int sorted[],int sortedSize)
- {
- int index = -1;
- int search = 0;
- cout << "Enter a number: ";
- cin >> search;
- for (int i = 0; i < sortedSize; i++)
- {
- if (sorted[i] == search)
- {
- index = i;
- }
- }
- if (index > -1)
- {
- cout << index << endl;
- }
- else
- {
- cout << "Element not there!" << endl;
- }
- return;
- }
- void binarySearch(int sorted[],int sortedSize)
- {
- //setting up some variables
- int target = 0;
- int highest = sortedSize;
- int lowest = 0;
- int half = highest / 2;
- int found= -1;
- //get the search target
- forLoop(sorted, lowest, sortedSize); // call function to print array
- cout << "Enter a number: ";
- cin >> target;
- //keep dividing the array untill we find the target
- while (found == -1)
- {
- if (target > sorted[highest -1] || target < sorted[lowest])
- {
- cout << "Not found :(" << endl;
- found = 1;
- break;
- }
- // find half way point
- half = (lowest + highest) / 2;
- //check if target is middle, second half or first half of the array
- if (target == sorted[half])
- {
- cout << "Found " << sorted[half];
- cout << " at index " << half << " :D" << endl;
- break; //if we are lucky it is at half way point, so we break the loop
- found = 1;
- }
- else if (target > sorted[half])
- {
- lowest = half;
- forLoop(sorted, lowest, highest);// print search range
- }
- else if (target < sorted[half])
- {
- highest = half;
- forLoop(sorted, lowest, highest); //to visualise what the program is doing
- }
- else
- {
- cout << target << "was not found :(" << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment