Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package datastructures;
  7.  
  8. import java.util.Arrays;
  9.  
  10. /**
  11. *
  12. * @author victormmorant
  13. */
  14. public class Sortings {
  15.  
  16. private final int size;
  17.  
  18. private int[] numbers;
  19.  
  20. private int[] result;
  21.  
  22. public Sortings(int size) {
  23. this.size = size;
  24. this.numbers = new int[size];
  25. this.result = new int[size];
  26. }
  27.  
  28. public void generateTest() {
  29. for(int i = 0;i<size;i++) {
  30. int random = (int) (Math.random()*100+1);
  31. numbers[i] = random;
  32. result[i] = random;
  33. }
  34. }
  35.  
  36. public void generateSolution() {
  37. Arrays.sort(result);
  38. }
  39.  
  40.  
  41. public void quickSort() {
  42. partition(0,0,numbers.length);
  43. }
  44.  
  45. public void partition(int pivot,int l, int r) {
  46. int i = l;
  47. int j = r;
  48. while(i<=j) {
  49. while(numbers[++i] < numbers[pivot]);
  50. while(numbers[--j] > numbers[pivot]);
  51. if(i<j)
  52. swap(i,j);
  53. }
  54.  
  55. swap(pivot,j);
  56.  
  57. if(l<j)
  58. partition(0,l,j);
  59. if(j<r)
  60. partition(j+1,j,r);
  61. }
  62. public void swap(int i,int j) {
  63. int aux = numbers[i];
  64. numbers[i] = numbers[j];
  65. numbers[j] = aux;
  66. }
  67.  
  68. public String toString() {
  69. String res = "";
  70. for(int i = 0;i<numbers.length;i++) {
  71. res += numbers[i]+" ";
  72. }
  73. return res;
  74. }
  75.  
  76. public static void main(String[] args) {
  77. Sortings s = new Sortings(10);
  78. s.generateTest();
  79. s.generateSolution();
  80. s.quickSort();
  81. System.out.println(s.toString());
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement