Skip to content

Commit e14f64d

Browse files
committed
complexity analysis to reverse array
1 parent 02cdc3a commit e14f64d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package BasicPractice;
2+
3+
// complexity: O(N)
4+
//This algorithm runs in O( N) time. The fact that it only goes through half of the array
5+
//(in terms of iterations) does not impact the Big O time.
6+
7+
public class complexityAnalysis4 {
8+
9+
public static void main(String[] args) {
10+
11+
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
12+
display(arr);
13+
reverseArray(arr);
14+
System.out.println();
15+
display(arr);
16+
}
17+
18+
public static void reverseArray(int[] arr) {
19+
for (int i = 0; i < arr.length / 2; i++) {
20+
int other = arr.length - i - 1;
21+
int temp = arr[i];
22+
arr[i] = arr[other];
23+
arr[other] = temp;
24+
}
25+
}
26+
27+
public static void display(int[] arr) {
28+
for (int i = 0; i < arr.length; i++) {
29+
System.out.print(arr[i] + " ");
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)