Skip to content

Instantly share code, notes, and snippets.

@maycuatroi1
Created October 23, 2025 07:14
Show Gist options
  • Select an option

  • Save maycuatroi1/bfba6e40de2d1c185b7970d2d534bb51 to your computer and use it in GitHub Desktop.

Select an option

Save maycuatroi1/bfba6e40de2d1c185b7970d2d534bb51 to your computer and use it in GitHub Desktop.
public class SelectionSort{
public static int[] selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
return arr;
}
public static void main(String[] args) {
int[] arr = {64, 25, 12, 22, 11};
arr = selectionSort(arr);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment