Skip to content

Instantly share code, notes, and snippets.

@rishi93
Created December 14, 2017 16:57
Show Gist options
  • Select an option

  • Save rishi93/d6e440c204816a72ed35b8f3a097c376 to your computer and use it in GitHub Desktop.

Select an option

Save rishi93/d6e440c204816a72ed35b8f3a097c376 to your computer and use it in GitHub Desktop.
Selection sort (Java implementation)
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