Advertisement
Guest User

cutiebadoodle

a guest
Apr 26th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class SelectionSort {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. int size = scan.nextInt();
  6. int[] unsorted = shuffle(size);
  7. // for (int z : unsorted) {
  8. // System.out.println(z);
  9. // }
  10. selectionSort(unsorted);
  11. for (int z : unsorted) {
  12. System.out.println(z);
  13. }
  14.  
  15. }
  16.  
  17. public static int[] shuffle(int n) {
  18. int[] ans = new int[n];
  19. for (int i = 0; i < n; i++) {
  20. ans[i] = (int) (Math.random() * n);
  21. }
  22. return ans;
  23. }
  24.  
  25. public static void selectionSort(int[] orig) {
  26. int temp = 0;
  27. int tempId = 0;
  28. for (int i = 0; i < orig.length; i++) {
  29. for (int j = i; j < orig.length; j++) {
  30. temp = orig[i];
  31. tempId = i;
  32. if (orig[j] < temp) {
  33. temp = orig[j];
  34. tempId = j;
  35. }
  36. }
  37. int temp2 = orig[i];
  38. orig[i] = temp;
  39. orig[tempId] = temp2;
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement