Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Random;
  5.  
  6. public class Ld1161RDB092 {
  7.  
  8. public static void quickSort(int[] a,int l,int r){
  9. int i=l, j=r, m=a[l+(r-l)/2];
  10. do{
  11. while(a[i]<m) ++i;
  12. while(a[j]>m) --j;
  13. if (i<=j){
  14. int temp=a[i];
  15. a[i]=a[j];
  16. a[j]=temp;
  17. i++;j--;
  18. }
  19. }while (i<=j);
  20. if (j>l)
  21. quickSort(a,l,j);
  22. if (r>i)
  23. quickSort(a,i,r);
  24. }
  25.  
  26. public static void randomNum(int[] a){
  27. Random rnd = new Random();
  28. for(int i=0;i<a.length;i++)
  29. a[i] =rnd.nextInt(1000)+50;
  30. }
  31. // Random numbers generator
  32.  
  33.  
  34. public static void decreaseNum(int[] a) {
  35. Random rnd =new Random();
  36. int b = rnd.nextInt(1000);
  37. for(int i=0;i<a.length;i++)
  38. a[i]=b--;
  39. }
  40. // decreaseNum(A);
  41.  
  42.  
  43. public static void increaseNum(int[] a){
  44. Random rnd = new Random();
  45. int b = rnd.nextInt(100);
  46. for (int i=0;i<a.length;i++)
  47. a[i]=b++;
  48. a[7]=35;a[17]=78;
  49. }
  50. // increaseNum(A);
  51.  
  52.  
  53. public static void firstMethod(int[] a){
  54. int i; boolean b = true;
  55. while (b=true){
  56. b=false;
  57. for (i=0; i<a.length;i=i+2) {
  58. if (a[i]>a[i+1]);
  59. a[i]=a[i+1];
  60. a[i+1]=a[i];
  61. b=true;
  62. }
  63. }
  64. for (i=1; i<a.length;i=i+2) {
  65. if (a[i]>a[i+1]);
  66. a[i]=a[i+1];
  67. a[i+1]=a[i];
  68. b=true;
  69. }
  70. }
  71.  
  72.  
  73.  
  74. public static void secondMethod(int[] a){
  75. quickSort(a,0,a.length-1);
  76. }
  77.  
  78.  
  79. public static void main(String[] args) throws Exception {
  80. System.out.println("Lauris Ungurs RDBI01 161RDB092");
  81. String s;int x,n,i;
  82. BufferedReader reader = new BufferedReader(new
  83. InputStreamReader(System.in));
  84. try{
  85. System.out.println("Method:");
  86. s=reader.readLine();
  87. x=Integer.parseInt(s);
  88. } catch (IOException e){
  89. System.out.println("input-output error");
  90. return;}
  91. if (x!=1 && x!=2){
  92. System.out.println("input-output error");
  93. return;}
  94. try{
  95. System.out.println("Count:");
  96. s=reader.readLine();
  97. n=Integer.parseInt(s);
  98. } catch (Exception e){
  99. System.out.println("input-output error");
  100. return;
  101. }
  102. int A[] = new int[n];
  103. System.out.println("Items:");
  104. for(i=0;i<n;i++){
  105. try{
  106. s=reader.readLine();
  107. A[i]=Integer.parseInt(s);
  108. } catch (Exception e){
  109. System.out.println("input-output error");
  110. return;}
  111. }
  112. // long t1 = System.nanoTime();
  113. if (x==1){
  114. firstMethod(A);
  115. }
  116. else {
  117. secondMethod(A);
  118. }
  119. // long t2 = System.nanoTime();
  120. // long t = t2-t1;
  121. // System.out.println("Time:");
  122. // System.out.println("t=" + t);
  123. System.out.println("Sorted:");
  124. for (i=0;i<n;i++)
  125. System.out.print(A[i] + " ");
  126. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement