Selection Sort using ArrayList (Inplace) in Java

One more simple form of sorting algorithm is Selection Sort.

For in-place implementation in java, starting from 1st index, find the smallest element (takes O(n) ) and put it at the first index of unsorted sub array. So there will be 2 parts of main array, left one will have sorted element and right one will have unsorted. We choose smallest from the right one and will put it at the beginning of the unsorted sub array. Now Sorted array will have one more element and unsorted array will have one less. Continue this process till we reach the end of element. ( takes O(n)).
So finding smallest for one loop will take O(n) and there will be at most n loop so it total time in worst case will be O(n^2)

SelectionSort_ProjectStructure

SelectionSort.java

MainPractise.java

Output:

 

You can have above code from GIT.

GitHub-Mark-32px https://github.com/Niravkumar-Patel/SnippetExampleRepo.git


Leave a Reply

Your email address will not be published. Required fields are marked *