Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. /**
  2. * Created by Lesley on 18-1-2017.
  3. */
  4.  
  5. import java.util.Random;
  6.  
  7. /**
  8. *
  9. */
  10. @SuppressWarnings("Duplicates")
  11. class Sorter implements Runnable {
  12. private int[] array;
  13. private int amount = 200000;
  14.  
  15. Sorter(int[] array) {
  16. this.array = array;
  17. }
  18.  
  19. Sorter() {
  20.  
  21. }
  22.  
  23. private void sortArray(int n, int[] array) {
  24. int c, d, swap;
  25. for (c = 0; c < (n - 1); c++) {
  26. for (d = 0; d < n - c - 1; d++) {
  27. if (array[d] > array[d + 1]) /* For descending order use < */ {
  28. swap = array[d];
  29. array[d] = array[d + 1];
  30. array[d + 1] = swap;
  31. }
  32. }
  33. }
  34. }
  35.  
  36. @Override
  37. public void run() {
  38. sortArray(amount / 2, array);
  39. }
  40.  
  41. int[] randomArray(int amount) {
  42. Random random = new Random();
  43. int[] array = new int[amount];
  44.  
  45. for (int i = 0; i < array.length; i++) {
  46. array[i] = i;
  47. }
  48.  
  49. for (int i = array.length - 1; i > 0; i--) {
  50. int index = random.nextInt(i + 1);
  51. int a = array[index];
  52. array[index] = array[i];
  53. array[i] = a;
  54. }
  55.  
  56. return array;
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement