Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. package ld1;
  2. import java.util.Scanner;
  3. public class ld1161rdb107 {
  4. int co = 0;
  5. int a[] = new int [co];
  6. int b[] = new int [co];
  7. int c[] = new int [co];
  8. int tmp[] = new int[co];
  9. public static void firstMethod(int[] a) {
  10. boolean b = true;
  11. while (b == true) {
  12. b = false;
  13. for(int i = 0; i < a.length-1; i = i+2){
  14. if(a[i] > a[i+1]){
  15. a[i+1]=a[i]+a[i+1];
  16. a[i]=a[i+1]-a[i];
  17. a[i+1]=a[i+1]-a[i];
  18. b = true;
  19. }
  20. }
  21. for(int i = 1;i < a.length-1; i = i+2){
  22. if(a[i] > a[i+1]){
  23. a[i+1]=a[i]+a[i+1];
  24. a[i]=a[i+1]-a[i];
  25. a[i+1]=a[i+1]-a[i];
  26. b = true;
  27. }
  28. }
  29. }
  30. }
  31.  
  32. public static void secondMethod(int[] a) {
  33. int k, i, j, ri, rj;
  34.  
  35. int b[] = new int [a.length];
  36.  
  37. int c[] = new int [a.length];
  38.  
  39. int tmp[] = new int [a.length];
  40.  
  41. int len = 1;
  42.  
  43.  
  44.  
  45. for (i=0;i<a.length;i++)
  46.  
  47. b[i] = a[i];
  48.  
  49.  
  50.  
  51. while(len<a.length){
  52.  
  53. int N=0;
  54.  
  55. for(k=0;k<a.length;k=k+2*len){
  56.  
  57. N=k;
  58.  
  59. i=k;
  60.  
  61. j=k+len;
  62.  
  63.  
  64.  
  65. if ((k+len)<a.length)
  66.  
  67. ri=k+len;
  68.  
  69. else
  70.  
  71. ri=a.length;
  72.  
  73.  
  74.  
  75. if ((k+2*len)<a.length)
  76.  
  77. rj = k+2*len;
  78.  
  79. else
  80.  
  81. rj = a.length;
  82.  
  83.  
  84.  
  85. while((i<ri)&&(j<rj)){
  86.  
  87. if (b[i]<b[j]){
  88.  
  89. c[N] = b[i];
  90.  
  91. i++;
  92.  
  93. N++;
  94.  
  95. } else {
  96.  
  97. c[N] = b[j];
  98.  
  99. j++;
  100.  
  101. N++;
  102.  
  103. }
  104.  
  105. }
  106.  
  107.  
  108.  
  109. while (i<ri){
  110.  
  111. c[N] = b[i];
  112.  
  113. i++;
  114.  
  115. N++;
  116.  
  117. }
  118.  
  119.  
  120.  
  121. while (j<rj){
  122.  
  123. c[N] = b[j];
  124.  
  125. j++;
  126.  
  127. N++;
  128.  
  129. }
  130.  
  131. }
  132.  
  133.  
  134.  
  135. len=2*len;
  136.  
  137. for (i=0; i<a.length; i++){
  138.  
  139. tmp[i] = b[i];
  140.  
  141. b[i] = c[i];
  142.  
  143. c[i] = tmp[i];
  144.  
  145. }
  146.  
  147.  
  148.  
  149. }
  150.  
  151.  
  152.  
  153. for (i=0; i<a.length; i++)
  154.  
  155. a[i] = b[i];
  156.  
  157.  
  158.  
  159. }
  160.  
  161. public static void main(String[] args) {
  162.  
  163. System.out.println("Heinrihs Skrodelis RDBI0 161RDB107");
  164. int i, met, mSize, z;
  165. System.out.println("method:");
  166. Scanner sc = new Scanner((System.in));
  167. if (sc.hasNextInt()) {
  168. met = sc.nextInt();
  169. if (met > 2 || met < 1) {
  170. System.out.println("input-output-error");
  171. sc.close();
  172. return;
  173. }
  174. } else {
  175. System.out.println("input-output-error");
  176. sc.close();
  177. return;
  178. }
  179. System.out.println("count:");
  180. if (sc.hasNextInt()) {
  181. z = sc.nextInt();
  182. mSize = z;
  183. } else {
  184. System.out.println("input-output-error");
  185. sc.close();
  186. return;
  187. }
  188.  
  189. System.out.println("items:");
  190. int a[] = new int [mSize];
  191. if (sc.hasNextInt()) {
  192. for (i = 0; i < mSize; i++) {
  193. a[i] = sc.nextInt();
  194. }
  195. } else {
  196. System.out.println("input-output-error");
  197. sc.close();
  198. return;
  199. }
  200. sc.close();
  201. long startTime = System.nanoTime();
  202. if (met == 1) {
  203. firstMethod(a);
  204. } else {
  205. secondMethod(a);
  206. }
  207. System.out.println("sorted:");
  208. for(i = 0; i < z; i++) {
  209. System.out.print(a[i] + " ");
  210. }
  211.  
  212. long endTime = System.nanoTime();
  213. System.out.println("");
  214. System.out.println("Took "+(endTime - startTime)/1000 + " microseconds");
  215.  
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement