//COMPUTER SCIENCE 161.01 //MONDAY NOVEMBER 11TH 2013 //SEQUENTIAL SEARCH (a.k.a. LINEAR SEARCH) //THE PROGRAM BELOW PERFORMS THE SEQUENTIAL SEARCH ALGORITHM //IT IS ALSO KNOWN AS THE 'LINEAR' SEARCH ALGORITHM //BECAUSE IT REQUIRES AT MOST N COMPARISONS WHERE N IS SIZE OF LIST //HERE, N is 15. firstNames = new Array("Daniel", "Jeffrey", "Stephen", "Marcus", "Bryce", "Marcus", "Tanner", "Tyler", "Michael", "Charles", "Christian", "Paul", "John", "George", "Ringo"); lastNames = new Array("Fogleman", "Decker", "Vassor", "Williams", "Jones", "Goodman", "Mullins", "Walton", "Willis", "Barham", "Wilder", "McCartney", "Lennon", "Harrison", "Starr"); i=0; desiredName = prompt("Enter the last name of the person you wish to greet:"); while (i < lastNames.length) { if (lastNames[i] == desiredName) //IF CURRENT lastName MATCHES desiredName alert("Happy Monday to " + firstNames[i]); //THEN send a friendly greeting //using HIS firstName i++; } alert("ALL DONE"); //WE NEED TO IMPROVE THIS, BECAUSE THE ABOVE PROGRAM ALWAYS MAKES 15 COMPARISONS //EVEN IF WE WERE LOOKING FOR Starr. //1) I WANT TO NOT MAKE ANY UNNECESSARY COMPARISONS ONCE I FIND A MATCH //2) AS A SECOND IMPROVEMENT, LET'S REPORT WHETHER OR NOT THE desired NAME WAS FOUND! firstNames = new Array("Daniel", "Jeffrey", "Stephen", "Marcus", "Bryce", "Marcus", "Tanner", "Tyler", "Michael", "Charles", "Christian", "Paul", "John", "George", "Ringo"); lastNames = new Array("Fogleman", "Decker", "Vassor", "Williams", "Jones", "Goodman", "Mullins", "Walton", "Willis", "Barham", "Wilder", "McCartney", "Lennon", "Harrison", "Starr"); i=0; desiredName = prompt("Enter the last name of the person you wish to greet:"); found = false; while ( !found && i < lastNames.length) { //while not found yet and still more names if (lastNames[i] == desiredName) //if current lastName matches desiredName found = true; //set found to true, thus ending the loop else //otherwise move along to the next name. i++; } if (found) alert("Happy Monday to " + firstNames[i]); else alert(desiredName + " was not found."); alert("ALL DONE");