#include <iostream>
 
using namespace std;
int main(){
    const int size =9;
    //Bubble sort and linear search.
    int anArray[size]={10,90,50,70,30,40,20,60,80};
    int temp=0;
    
    int key;
    cout <<"Enter the value you would like to be the key for the linear search: ";
    cin >> key; //Key value used in linear search, this will become more valuable when we get into classes / objects
    for (int i=0; i<size; i++){//iterate through the loop
        if (anArray[i] == key){//if the value of i in the array is == to key
            cout << "\nThe position of " << key << " is at position " << i << " in the array."<<endl <<endl; //We print out where that key is in the array
            break;
        }
        else{//Else, we don't do anything.
            //null
        }
    }
    
    
    bool swapped=true;
    while(swapped){
        swapped=false;
        for(int i=1; i<size; i++){
            if (anArray[i] < anArray[i-1]){
                temp = anArray[i];
                anArray[i] = anArray[i-1];
                anArray[i-1]= temp;
                swapped = true;
            }
        }
    }
    cout << "\n\n";
    for (int i=0; i<size; i++){
        cout << anArray[i] << endl;
    }
    cout << "\n\n";
    
    
    int val;
    cout << "What is the value you're searching for? ";
    cin >> val;
    
    int max= 8, min=0, middle;
    bool found = false;
    
    while(min <= max){
        //Binary sort!
        // 10,20,30,40,50,60,70,80,90
        // 0  1  2  3  4  5  6  7  8
        // 0           4           8
        // 0              5  6     8
        
        
        // 0 + 8 / 2=  4
        //5 + 8 = 13 / 2 = 6.5 = 6
        middle = ((max+min)/2);
        if (anArray[middle] < val){
            min = middle+1;
            cout << "\nmin now equals: " << min;
            //min =0 first loop   middle = 4 + 1 =5
        }
        if (anArray[middle] > val){
            max = middle-1;
            cout <<"\nmax now equals: "<< max;
            // max =8   set it to 5  (2nd iteration looking for 60
        }
        
        if (anArray[middle] == val){
            cout << "The value: "<< val << " Was found at array location:  "<< middle;
            found = true;
            break;
        }
    }
    
    
    
    if (found == false){
        cout << "\nThe value was not found.";
    }
    
    
    
    
    
 
return 0;
}
