Javatpoint Logo
Javatpoint Logo

Selection Sort in Java

We can create a java program to sort array elements using selection sort. In selection sort algorithm, we search for the lowest element and arrange it to the proper location. We swap the current element with the next lowest number.

selection sort

How does selection sort work?

The selection sort algorithm works in a very simple way. It maintains two subarray for the given array.

  • The subarray is already sorted.
  • And the second subarray is unsorted.

With every iteration of selection sort, an element is picked from the unsorted subarray and moved to the sorted subarray.

Time Complexity

Best: ?(n^2)
Average: ?(n^2)
Worst: O(n^2)

Space Complexity


Selection Sort Java Example


Before Selection Sort
9 14 3 2 43 11 58 22 
After Selection Sort
2 3 9 11 14 22 43 58 

Selection Sort in Java (Another way)

You can also use a method where array is not predefined. Here, user has to put the elements as input.

In the following Java program, we ask user to enter the array elements or number, now compare the array's element and start swapping with the variable temp. Put the first element in the temp and the second element in the first, and then temp in the second number and continue for the next match to sort the whole array in ascending order.


Use image SelectionSort
Next TopicJava Programs

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA