That is O(n).

You swap elements n/2 times: (0 with n-1), (1 with n-2), ... (n/2 - 1 with n/2 + 1)

Answer from Jean Logeart on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ program-to-reverse-an-array
Array Reverse - GeeksforGeeks
Time Complexity: O(n), the loop runs through half of the array, so it's linear with respect to the array size. Auxiliary Space: O(1), no extra space is required, therefore we are reversing the array in-place.
Published ย  August 8, 2025
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 52746514 โ€บ time-complexity-of-bit-reverse-function-in-c
Time complexity of bit reverse function in C - Stack Overflow
I am concerned about the time complexity of the bit reverse function for this problem. ... O(n), for n bits. For uint_8, the algorithm runs in 8 steps. For uint_16, the alrogithm runs in 16 steps.
Discussions

What is the time complexity of this "reverse words" algorithm? - Computer Science Stack Exchange
I had to write an algorithm that, given the input ['h', 'a', 'r', 'd', ' ', 'i', 's', ' ', 'c', 's'] would return ['c', 's', ' ', 'i', 's', ' ', 'h', 'a', 'r', 'd']. I found an algorithm that I thi... More on cs.stackexchange.com
๐ŸŒ cs.stackexchange.com
January 24, 2020
algorithm - How to reverse a string in O(1) complexity (runtime)? - Stack Overflow
I faced an interview today and I was asked to reverse a string in single operation . I was feeling like it is impossible in less than O(n) . By the way , I was given a clue , "Swapping" ! So . . .... More on stackoverflow.com
๐ŸŒ stackoverflow.com
What is the time complexity of reversing a string?
It should be simply O(n) where n is the length of string. If you think of a string as an array, you simply swap characters between left and right ends until they cross over the mid-point โ€” which means O(n/2) and simplified to O(n). More on reddit.com
๐ŸŒ r/algorithms
13
10
October 23, 2020
What is the time complexity of reverse() ?
You should use asReversed() instead of reversed(). It does not actually reverse the array, it's only a reversed view of the array. More on reddit.com
๐ŸŒ r/Kotlin
16
10
November 6, 2022
Top answer
1 of 2
1

This algorithm runs in linear time in the size of the input .

To see why try to count how many times you read/write to a position in the string. Note that asymptotically this is equal to the total running time.

2 of 2
0

Have you run the code?

If you reverse the array ['d', 'r', 'a', 'h', ' ', 's', 'i', ' ', 's', 'c'] you get the desired result of ['c', 's', ' ', 'i', 's', ' ', 'h', 'a', 'r', 'd']. If you then reverse the words you get ['s', 'c', ' ', 's', 'i', ' ', 'd', 'r', 'a', 'h'], which is not what you want.

You can't get better than an complexity for reversing an array of size . If you want an explanation as to why this is the case, then we must be slightly more precise. Below I do the analysis for an easy to analyze implementation, not an inplace version. (The analysis is similar for an inplace version, and I think that you can work it out.)

Let's assume that the operations which have a cost are "reading a value from an array", "writing a value to an array", and "comparing two values". The input to our algorithm is an array input of size and the output is an array output also of size , and we must ensure that when the algorithm terminates that output[i] == input[n-i-1] where . (Take a minute and convince yourself that this is a correct description of the problem, and that the equality is correct for both even and odd values of .) To make the analysis as simple as possible, we will assume that there is an array named output of size just waiting for us, and that output[i] == 0 for all .

Therefore, any algorithm must assign to output[i] the value input[n-i-1] for all possible values of , unless input[n-i-1] == 0, in which case no assignment needs to be made, as the correct value is already present. So one possible implementation would loop through the different values of and check if input[n-i-1] == 0 and continue when the condition was true and assign the value to output[i] when the condition was false.

If the input array is 0s then this implementation would perform exactly operations, one operation to get the value at input[n-i-1] and one operation to compare it to 0. However, if there are no values of 0 in the input, then there are exactly operations performed, of the operations are the same as before, but now there are assignments of the value to the correct position in the output array (here we assume we don't need to read the value from the input array again). More generally if 0 occurs times in the input then there are exactly operations performed.

Now this part is important: if we don't check the value, but instead just do the assignment then there are always operations, one to read from the input and one to write to the output. Checking the value each time to avoid an unnecessary copy does more work than not checking the value in the majority of cases. Since any algorithm which uses these operations must do at least this much work, you can't do better than operations.

But what about other operations? For an inplace version you might consider an operation which swaps two values in the array swap(i, j). Such an operation allows you to reverse the array in operations (for even ), which is still .

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-reversed-vs-1-which-one-is-faster
Python - reversed() VS [::-1] , Which one is faster? - GeeksforGeeks
June 19, 2024 - The format [a : b : c] in slicing states that from an inclusive to b exclusive, count in increments of c. In the above code, a and b are blank and c is -1. So it iterates the entire list counting from the last element to the first element resulting in a reversed list.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ reverse-a-string
Reverse a String - GeeksforGeeks
The idea is to use recursion and define a recursive function that takes a string as input and reverses it. Inside the recursive function, Swap the first and last element. Recursively call the function with the remaining substring.
Published ย  3 weeks ago
๐ŸŒ
Quora
quora.com โ€บ How-can-you-reverse-a-string-in-a-way-to-get-a-time-and-space-complexity-that-is-better-than-O-n
How to reverse a string in a way to get a time and space complexity that is better than O(n) - Quora
A string is a sequence, and reversing the sequence always entails the entire sequence, thus \mathcal{O}(n). If the string is 100 characters long, it will take processing 100 chars to reverse it.
๐ŸŒ
Quora
quora.com โ€บ Why-does-reversing-an-array-take-linear-time
Why does reversing an array take linear time? - Quora
Answer (1 of 5): Well letโ€™s remember what the definition of Big-Oh is: Let f(n) and g(n) be complexity/growth functions. Then, f(n) \in O(g(n)) if and only if there exist positive real constant c and positive integer constant n_0, such that for all n \geq n_0, f(n) \leq c \cdot g(n). Let n be t...
Find elsewhere
๐ŸŒ
Medium
gauravssah.medium.com โ€บ reverse-an-array-in-c-efficient-array-reversal-algorithm-56f014d072ae
Problem: Reverse an Array. Learn how to reverse an array in C++โ€ฆ | by Gaurav Sah | Medium
September 20, 2024 - Final Output: After the loop completes, the reversed array revarr is returned. Time Complexity: The time complexity is O(n) where n is the number of elements in the array, because each element is accessed and copied exactly once.
๐ŸŒ
AlgoCademy
algocademy.com โ€บ link
Reverse String in C++ | AlgoCademy
This approach involves swapping characters from the beginning and end of the string, moving towards the center. This method ensures that we reverse the string in-place, achieving O(n) time complexity.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ reverse-a-string-in-c-cpp-different-methods
Different Methods to Reverse a String in C++ - GeeksforGeeks
C++ #include <bits/stdc++.h> using ... s.end()); cout << s; return 0; } Output ยท dlroW olleH ยท Time Complexity: O(n), where n is the length of string....
Published ย  July 23, 2025
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ c program to reverse an array
C Program to Reverse an Array | Scaler Topics
May 4, 2023 - No auxiliary space is used. Therefore its space complexity is ... In the above implementation, we are reversing the array by assigning the element from reverse traversing the ... The whole of the array is traversed while reversing it. Therefore, the time complexity of this method is
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ c program to reverse a number
C Program to Reverse a Number - Scaler Topics
February 26, 2024 - Explanation: Let's break down the operation of the while loop for the given reverse number program in c, input number n is 12345. Finally, reversed_number, now holding the reversed sequence of digits, is displayed as output. Complexity Analysis ยท Time complexity: O ยท
๐ŸŒ
Reddit
reddit.com โ€บ r/algorithms โ€บ what is the time complexity of reversing a string?
r/algorithms on Reddit: What is the time complexity of reversing a string?
October 23, 2020 -

I'm getting ready for an interview and practicing time complexity and I'm ok at recognizing the complexity for the code I've written myself but have a harder time when I throw in methods from the java library.

For example, I read that the method concat was similar to += and that concat time complexity was O(n^2). So if I reverse a string with the code below does that mean my time complexity is O(n^2)? With a space complexity of O(n) since? Where n is the length of the string.

String str="hello"
String s="";
for(int i =str.length()-1; i>=0;i--){
s+=str.substring(i,i+1);
}

With this code is the space complexity O(n) and the time complexity O(n)? Where n is the length of the string.

String str="hello";
char[] cArr = str.toCharArray();
int i =0;
int j = cArr.length-1;
char temp;
while(i<j){
    temp =cArr[i];
    cArr[i]=cArr[j];
    cArr[j]=temp;
    i++;
    j--;
}
str=String.valueOf(cArr);
Top answer
1 of 5
17
It should be simply O(n) where n is the length of string. If you think of a string as an array, you simply swap characters between left and right ends until they cross over the mid-point โ€” which means O(n/2) and simplified to O(n).
2 of 5
8
I don't know the time complexity of internal java lib functions by heart- I'll leave it to someone else to comment on that. What I do want to offer is a technique to sanity check your understanding. Asymptotic time complexity is very much on the numerical / theoretical side of things, but the actual computation time will reflect this behavior once the input size grows large enough. What you can do is repeatedly time the function on various input sizes spanning many orders of magnitude until you reach some limit (probably where the computation starts taking multiple seconds). You can then graph input size vs computation time, and see if its constant, linear, quadratic, logarithmic, etc., by eye. If you want a more precise measurement, take the log of both the input sizes and the computation times (so its a log-log plot), and a best-fit line through your data points (using a linear regression), the slope is the exponent of your variable in your measured time complexity (so a slope of 2 corresponds to observed quadratic time behavior). This isn't perfect, there can easily be huge constants throwing off your measured time complexity, you'll likely drop any logarithmic factors, etc. Its more of a coarse sanity check- if you think the algorithm is O(N2 ) but you experiment and see a near-straight line, and the log-log plot has a slope of 1, then you can reason that its likely not quadratic, its behaving more as a linear-time algorithm, and you should recheck your calculations.
๐ŸŒ
DEV Community
dev.to โ€บ emilossola โ€บ reversing-a-vector-in-c-efficient-techniques-and-best-practices-436o
Reversing a Vector in C++: Efficient Techniques and Best Practices - DEV Community
July 17, 2023 - Generally efficient for small to medium-sized vectors. ... Inefficient for large vectors, as it requires swapping elements individually. Time complexity is O(n/2), which can lead to slower execution for larger vectors.
๐ŸŒ
Wyzant
wyzant.com โ€บ resources โ€บ ask an expert
Array reversal in O( log n) time. | Wyzant Ask An Expert
January 19, 2024 - Reversing an array in O(log n) time complexity is not possible. The reason for this is that reversing an array involves swapping elements, and to perform a swap operation for each element in an array, you would need to traverse the entire array at least once.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ reverse-a-stack
Reverse a Stack - GeeksforGeeks
Since stacks are LIFO, pushing all elements into the auxiliary stack will naturally reverse their order. Finally, we replace the original stack with the auxiliary stack. C++ #include <iostream> #include <stack> using namespace std; void ...
Published ย  February 20, 2026
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ algorithm โ€บ reverse
std::reverse
template <class BidirectionalIterator> void reverse (BidirectionalIterator first, BidirectionalIterator last);