int anArray[size]={10,90,50,70,30,40,20,60,80};//Can only set the size of an array to a const int, I did this to have a size() type usage without a vector
int temp=0;//Used in the loop to swap stuff
//We declare a bool here to decide if the loop is completed or not, it's our way of running a nested loop until the nested loop no longer needs running
bool swapped=true;
//initialize this bool to true
while(swapped){
//while this bool is true
swapped=false;
//Set the bool to false so that if this loop doesn't hit that if statment, it won't run anymore, without this it will be an endless loop
for(int i=1; i<size; i++){
//iterate through the array
if(anArray[i]< anArray[i-1]){
//This if statement compares the values at i, and i-1, if the value at i-1 is smaller..
temp = anArray[i];
//We will first set temp = the current value of anArray[i]. We need to do this because we can't* swap 2 variables without something to hold one of them
//* - The above is technically a lie, but for the sake of coding theory, we cannot. Any function we use will employ this same basic theory.
anArray[i]= anArray[i-1];
//Set the current value of anArray[i] to the value of anArray[i-1]
//(so first iteration through it will be 90 vs 10, which is false, but then 50 vs 90 is not)
anArray[i-1]= temp;
//This completes the 'swap' when both values have been assigned to their new location.
swapped =true;
//Since values are still being swapped here, we set this to true again, so the loop will continue to run until it checks out as being 'false'
}
}
}
//output loop
for(int i=0; i<size; i++){
cout<< anArray[i]<< endl;
}
//linear search needs two things- for loop, and a key term
int key =70;//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