Optimal time complexity of inverse string conversion operation: O (n) or O (n / 2)?

Below code snippet for String reverse

private static String reverseString(String originalString){
char arr[]= originalString.toCharArray();
char temp;

for(int i= 0,j=arr.length-1;i<(arr.length/2);i++,j--){

 temp=arr[i];
 arr[i]=arr[j];
 arr[j]=temp;
}
return new String(arr);

I saw a lot of discussion about time complexity for the aforementioned String reverse, where some mentioned complexity as O (n / 2) and some O (n).

I would like to understand which one will really be the right time complexity for the return line.

Any insight would be really helpful for understanding the confusion here.

+4
source share
2 answers

Asymptotically there is no difference between O(n)and O(n/2). The difference between them is constant.

, 3n/2, 3 . , char , .

+9

Big O ( )

, , O (n). , T (n) = n/1 n/2 n/3, .

+2

All Articles