Algorithmic Design and Data Structure
Explain to another newbie how to apply algorithmic design and data structure techniques
When you start to look at a java program, you need to understand what you want it to look like and what it's going to be used for. For example, a large program will need a search that would be able to go across a larger scale in a smaller amount of time - such as a binary search.
Are some designs better than others? Explain.
In a situation where the computer needs to read, say, 100 contacts and their information, you would likely prefer to use a binary search on their specific social security number. If it's larger than 123-456-7890 than you would be able to split it off between whatever number it was found in and the largest number, otherwise it is either that specific number or in the lower from that number down. This would be a lot better than linear search at this point because the computer only has to search through a small amount of information to get to the place it's looking at.
How would you apply it?
So if I was doing a list of people from 1 - 100 people and I was looking for a person saved between 50 and 100, I would use:
if (NumberYourLookingFor == Numers.get(50)) {
*Insert code to print off exactly 50*
} else if (NumberYourLookingFor > Numers.get(50)) {
*Insert code to search for and print for any number between 50 and 100*
} else {
*Insert code to search for and print for any number between 0 and 50*
}
I would then use a linear search for that route and it would split the workload evenly. A linear search is just the code:
if (NumberYourLookingFor == Numers.get(i)) {
System.out.println(Numers.get(i));
} else {
++i;
}
This basically states that if the number I'm looking for is in the 'i' position of the list, then it will print it off. Otherwise it's going to add 1 to the variable 'i' and search again. Each time it doesn't work it'll continue going until it either finds it or brings back an error.