Programming languages have made life so much easier over the last ten years. What had to be done at home on a computer can now be done with a device that can fit in your hand or on your wrist. But none of those new devices would be able to perform any of your favorite tasks without implementing algorithms and data structures.
So how would we apply algorithms and data structures? First, we need to have a unique problem that needs to be solved without there already being an answer to it. Many programming languages have built-in libraries that contain algorithms to help with the coding process, but what if there are no libraries? This is where data structures and algorithms come into play.
Let's take a look at data structures first. In Java, the most common data structures are:
- Arrays
- Stacks
- Linked List
These data structures can be designed to be a certain size or can be the maximum size allowed by Java. Within these data structures, multiple variables can be stored in a unique order, known as elements. Here is an example using an Array that can contain strings and integers:
import java.util.Arrays;
class test {
public static void main(String[] args) {
Object[] myArray = new Object[10]; //Implement an array with 10 element max.
myArray[0] = "Hot dogs";
myArray[1] = 23;
System.out.println(Arrays.toString(myArray));
}
}
The array would print out as [Hot dogs, 23]. We can also implement it as:
import java.util.Arrays;
class test {
public static void main(String[] args) {
Object[] myArray = new Object[]{"Hot dogs", 23, 18, 100.23, "soda",
"fries", "$50", 2,"Italian beef", "Summer"};
System.out.println(Arrays.toString(myArray));
}
}
This array would print out as [Hot dogs, 23, 18, 100.23, soda, fries, $50, 2, Italian beef, Summer]
Now that we understand what a data structure is, we can now come up with an algorithm. Let's take the array above, remove the numbers, and put caps on the first letter of the strings. We can create an algorithm to find a string within the array. Let's look for the string "Soda":
import java.util.Arrays;
class test {
public static String findString(Object[] testArray, String check) {
for (Object o : testArray) {
if (o == check) {
return "String '" + check + "' is in the array.";
}
}
return "The string '" + check + "' was not found in the array.";
}
public static void main(String[] args) {
Object[] myArray = new Object[]{"Hot dogs", "Soda", "Fries", "$50", "Italian beef", "Summer"};
System.out.println("My array is: \n\n" + Arrays.toString(myArray) + "\n\n" +
"Searching for the string 'Soda' in the array: \n");
System.out.println(findString(myArray, "Soda"));
}
}
The output will be:
----------------------------------------------------------------
My array is:
[Hot dogs, Soda, Fries, $50, Italian beef, Summer]
Searching for the string 'Soda' in the array:
String 'Soda' is in the array.
----------------------------------------------------------------
Now the question lies: Are some data structure designs and algorithms better than others? The answer is yes because some data structures are modified differently depending on the need to modify the stored elements. Also, because some algorithms are slower than others. The above algorithm has to go through all the array elements to find the string and could be redesigned to split the array to search a smaller portion of it (see binary search).
As you learn more about data structures and algorithms, you will see just how important they are and how much more they can increase your productivity while maintaining the same amount of effort.
No comments:
Post a Comment