I have a String array String strs[] = {"flower", "flow", "flight"};
.
I want to find the smallest and largest lexicographically string from the array. This is what I did:
String first = strs[0], last = strs[0];
for (String str : strs) {
if (str.compareTo(first) < 0)
first = str;
if (str.compareTo(last) > 0)
last = str;
}
System.out.println("First : " + first + " Last : " + last);
Now I want find the time complexity of this algorithm. I know it will be n * (time complexity of compareTo()
). So, what is the time complexity of this algorithm?