Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SelectionSort {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int size = scan.nextInt();
- int[] unsorted = shuffle(size);
- // for (int z : unsorted) {
- // System.out.println(z);
- // }
- selectionSort(unsorted);
- for (int z : unsorted) {
- System.out.println(z);
- }
- }
- public static int[] shuffle(int n) {
- int[] ans = new int[n];
- for (int i = 0; i < n; i++) {
- ans[i] = (int) (Math.random() * n);
- }
- return ans;
- }
- public static void selectionSort(int[] orig) {
- int temp = 0;
- int tempId = 0;
- for (int i = 0; i < orig.length; i++) {
- for (int j = i; j < orig.length; j++) {
- temp = orig[i];
- tempId = i;
- if (orig[j] < temp) {
- temp = orig[j];
- tempId = j;
- }
- }
- int temp2 = orig[i];
- orig[i] = temp;
- orig[tempId] = temp2;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement