Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SelectionSort {
- private static void swap(Comparable[] a, int i, int j) {
- Comparable t = a[i];
- a[i] = a[j];
- a[j] = t;
- }
- private static boolean checkMin(Comparable v, Comparable w) {
- return v.compareTo(w) < 0;
- }
- private static void display(Comparable[] a) {
- for (int i = 0; i < a.length; i++) {
- System.out.print(a[i] + "\t");
- }
- }
- private static void SelectionSort(Comparable[] a) {
- int n = a.length, min;
- for (int i = 0; i < n; i++) {
- min = i;
- for (int j = i + 1; j < n; j++) {
- if (checkMin(a[j], a[min])) {
- min = j;
- }
- }
- swap(a, i, min);
- }
- }
- public static void main(String[] args) {
- String a;String[] b;
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter the data to be sorted seperated by space :-");
- a = sc.nextLine();
- System.out.println("\nSort using Selection Sort");
- b = a.trim().split(" ");
- SelectionSort(b);
- display(b);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement