Created
December 14, 2017 16:57
-
-
Save rishi93/d6e440c204816a72ed35b8f3a097c376 to your computer and use it in GitHub Desktop.
Selection sort (Java implementation)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SelectionSort{ | |
| public void selectionSort(int[] arr, int n){ | |
| int min_index; | |
| for(int i = 0; i < n-1; i++){ | |
| min_index = i; | |
| for(int j = 1+i; j < n; j++){ | |
| if(arr[j] < arr[min_index]){ | |
| min_index = j; | |
| } | |
| } | |
| int temp = arr[i]; | |
| arr[i] = arr[min_index]; | |
| arr[min_index] = temp; | |
| } | |
| } | |
| } | |
| public class Test{ | |
| public static void main(String[] args){ | |
| SelectionSort ob = new SelectionSort(); | |
| int[] arr = {5, 2, 3, 4, 1}; | |
| int n = arr.length; | |
| ob.selectionSort(arr, n); | |
| System.out.println("After sorting"); | |
| for(int i = 0; i < n; i++){ | |
| System.out.print(arr[i] + " "); | |
| } | |
| System.out.println(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment