Lesson 15 of 34

Array Algorithms: Reverse, Search, Sort

10 min 30 XP

The java.util.Arrays class has handy helpers: Arrays.toString() prints an array neatly and Arrays.sort() sorts it.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Interviews often ask you to do it without library help. To reverse an array, swap from both ends and meet in the middle (the two-pointer technique).

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Swapping two values needs a temp variable to hold one of them.

Challenge

+30 XP

Read n and n numbers, then print them in reverse order on one line, separated by spaces. Input: 5 1 2 3 4 5 Output: 5 4 3 2 1

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.