Guest User

Untitled

a guest
Nov 18th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. /**
  2. * Sorts the array
  3. *
  4. * @Laura
  5. * @1.0
  6. */
  7.  
  8. import java.util.*;
  9.  
  10. public class Sort
  11. {
  12. public Sort() {
  13. int[] primeArr = generatePrimes();
  14. System.out.println("Original Array: " + Arrays.toString(primeArr));
  15. System.out.println("Sorted Array: " + Arrays.toString(insertionSort(primeArr)));
  16. }
  17. public int[] insertionSort(int[] a) {
  18. for (int i = 0; i < a.length-1; i++) {
  19. while (a[i+1] < a[i]) {
  20. int temp = a[i];
  21. a[i] = a[i+1];
  22. a[i+1] = temp;
  23. if (i >= 1) {
  24. i -= 1;
  25. }
  26. }
  27. }
  28. return a;
  29. }
  30. public boolean isPrime(int y) {
  31. for (int i = 2; i < y/2; i++) {
  32. if (y % i == 0) {
  33. return false;
  34. }
  35. }
  36. return true;
  37. }
  38. public int[] generatePrimes() {
  39. int[] primes = new int[10];
  40. int count = 0;
  41.  
  42. while (count < 10) {
  43. int rand = (int)(Math.random() * 200 + 1);
  44. if (isPrime(rand)) {
  45. primes[count] = rand;
  46. count++;
  47. }
  48. }
  49. return primes;
  50. }
  51. }
Add Comment
Please, Sign In to add comment