Guest User

Untitled

a guest
Dec 5th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.12 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. public class Main
  4. {
  5. private static int parent(int n) {return(n-1)/2;}
  6. private static int leftChild(int n) {return(2*n)+1;}
  7. private static int rightChild(int n) {return(2*n)+2;}
  8. static int comparisons;
  9. public static void main(String[] args)
  10. {
  11. int selectionArray[] = new int[100];
  12. createArray(selectionArray);
  13. System.out.println("Array Unsorted(Selection Sort) : ");
  14. printArray(selectionArray);
  15. selectionsort(selectionArray,1,selectionArray.length-2);
  16. System.out.println();
  17. System.out.println("Array Sorted(Selection Sort): ");
  18. printArray(selectionArray);
  19. System.out.println();
  20.  
  21. int insertArray[] = new int[100];
  22. createArray(insertArray);
  23. System.out.println("Array Unsorted(Insert Sort): ");
  24. printArray(insertArray);
  25. insertionsort(insertArray,1,insertArray.length-2);
  26. System.out.println();
  27. System.out.println("Array Sorted(Insert Sort): ");
  28. printArray(insertArray);
  29. System.out.println();
  30.  
  31. int mergeArray[] = new int[100];
  32. createArray(mergeArray);
  33. System.out.println("Array Unsorted(Merge Sort): ");
  34. printArray(mergeArray);
  35. mergesort(mergeArray,0,mergeArray.length-1);
  36. System.out.println();
  37. System.out.println("Array Sorted(Merge Sort): ");
  38. printArray(mergeArray);
  39. System.out.println();
  40.  
  41. int quickArray[] = new int[100];
  42. createArray(quickArray);
  43. System.out.println("Array Unsorted(Quick Sort): ");
  44. printArray(quickArray);
  45. quicksort(quickArray,0,quickArray.length-1);
  46. System.out.println();
  47. System.out.println("Array Sorted(Quick Sort): ");
  48. printArray(quickArray);
  49. System.out.println();
  50.  
  51. int heapArray[] = new int[100];
  52. createArray(heapArray);
  53. System.out.println("Array Unsorted(Heap Sort): ");
  54. printArray(heapArray);
  55. heapsort(heapArray,heapArray.length);
  56. System.out.println();
  57. System.out.println("Array Sorted(Heap Sort): ");
  58. printArray(heapArray);
  59. System.out.println();
  60.  
  61.  
  62. comparisonTable();
  63. //SelectionSort Ns
  64. int N = 1000;
  65. int compareSelect[] = new int[N];
  66. createArray(compareSelect);
  67. int compareInsert[] = new int[N];
  68. createArray(compareInsert);
  69. int compareMerge[] = new int[N];
  70. createArray(compareMerge);
  71. int compareQuick[] = new int[N];
  72. createArray(compareQuick);
  73. int compareHeap[] = new int[N];
  74. createArray(compareHeap);
  75. System.out.println("1000: "+sort(1,compareSelect,compareSelect.length-2) + " " + sort(2,compareInsert,compareInsert.length-2) + " " + sort(3,compareMerge,compareMerge.length-2)+ " " + sort(4,compareQuick,compareQuick.length-1) + " " + sort(5,compareHeap,compareHeap.length) + " " + nlogcalc(N));
  76.  
  77.  
  78.  
  79. // while(N <= 10000)
  80. // {
  81. // N = N + 1000;
  82. // int[] compareArray = new int[N];
  83. // createArray(compareArray);
  84. // System.out.println("1000: "+sort(1,compareArray,compareArray.length-2) + " " + sort(2,compareArray,compareArray.length-2) + " " + sort(3,compareArray,compareArray.length-2)+ " " + sort(4,compareArray,compareArray.length-1) + " " + sort(5,compareArray,compareArray.length) + " " + nlogcalc(N));
  85. // }
  86.  
  87. }
  88. public static int[] createArray(int[] Array)
  89. {
  90. Random r = new Random();
  91. for(int i = 0; i < Array.length;i++)
  92. {
  93. Array[i] = r.nextInt(100);
  94. }
  95. return Array;
  96. }
  97. private static void printArray(int[] arr)
  98. {
  99. for(int i = 0; i < arr.length;i++)
  100. {
  101. if(i>0)
  102. {
  103. System.out.print(", ");
  104. }
  105. System.out.print(arr[i]);
  106. }
  107. }
  108. public static int selectionsort(int[ ] data, int first, int n)
  109. {
  110. comparisons = 0;
  111. int i, j; // Loop control variables
  112. int min;
  113. int mindex;// Index of largest value in data[first]...data[first+i]
  114. int temp; // Used during the swapping of two array values
  115. for (i = 0; i < data.length; i++)
  116. {
  117. // Calculate big as the index of the largest value in data[first]...data[first+i]:
  118. min = data[i];
  119. mindex = i;
  120. for(j=i;j<data.length;j++)
  121. {
  122. if(data[j] < min)
  123. {
  124. min = data[j];
  125. mindex = j;
  126. }
  127. comparisons++;
  128. }
  129. if(min < data[i])
  130. {
  131. temp = data[i];
  132. data[i] = data[mindex];
  133. data[mindex] = temp;
  134. }
  135. }
  136. return comparisons;
  137. }
  138. public static int insertionsort(int[ ] data, int first, int n)
  139. {
  140. comparisons = 0;
  141. int i, j,key,temp;// Loop control variables
  142. for(i=1;i<data.length;i++)//outer loop start @1 b/c it has no left(sorted)
  143. {
  144. key = data[i];
  145. j = i-1;
  146. while(j >=0 && key < data[j])
  147. {
  148. temp = data[j];
  149. data[j] = data[j+1];
  150. data[j+1] = temp;
  151. j--;
  152. comparisons++;
  153. }
  154. }
  155. return comparisons;
  156. }
  157. public static int mergesort (int[] list, int low, int high)
  158. {
  159. int comparisons = 0;
  160. if (low == high)
  161. return 0;
  162. else {
  163. int mid = (low + high) / 2;
  164. mergesort(list, low, mid);
  165. mergesort(list, mid + 1, high);
  166. comparisons = comparisons + merge(list, low, mid, high);
  167. }
  168. return comparisons;
  169. }
  170. public static int merge(int[] data, int low, int mid, int high)
  171. {
  172. int[] Left = new int[mid - low + 2];
  173. for (int i = low; i <= mid; i++)
  174. {
  175. Left[i - low] = data[i];
  176. // populates Left
  177. }
  178. Left[mid - low + 1] = Integer.MAX_VALUE;
  179. int[] Right = new int[high - mid + 1];
  180.  
  181. for (int i = mid + 1; i <= high; i++)
  182. {
  183. Right[i - mid - 1] = data[i];
  184. // populates right
  185. }
  186. Right[high - mid] = Integer.MAX_VALUE;
  187. int i = 0, j = 0;
  188. for (int k = low; k <= high; k++)
  189. {
  190. if (Left[i] <= Right[j])
  191. {
  192. data[k] = Left[i];
  193. i++;
  194. comparisons++;
  195. }
  196. else {
  197. data[k] = Right[j];
  198. j++;
  199. comparisons++;
  200. }
  201. }
  202. return comparisons;
  203. }
  204. private static int quicksort(int[] A, int low, int high)
  205. {
  206. comparisons = 0;
  207. if (low < high+1)
  208. {
  209. int p = partition(A, low, high);
  210. quicksort(A, low, p-1);
  211. quicksort(A, p+1, high);
  212. }
  213. return comparisons;
  214. }
  215. private static int partition(int[] A, int low, int high)
  216. {
  217. swap(A, low, getPivot(low, high));
  218. int limit = low + 1;
  219. for (int i = limit; i <= high; i++)
  220. {
  221. if (A[i] < A[low])
  222. {
  223. swap(A, i, limit++);
  224. comparisons++;
  225. }
  226. }
  227. swap(A, low, limit-1);
  228. return (limit-1);
  229. }
  230. private static int getPivot(int low, int high)
  231. {
  232. Random rand = new Random();
  233. return rand.nextInt((high - low) + 1) + low;
  234. }
  235. private static void swap(int[] A, int index1, int index2)
  236. {
  237. int temp = A[index1];
  238. A[index1] = A[index2];
  239. A[index2] = temp;
  240. }
  241. public static int heapsort(int[ ] data, int n)
  242. {
  243. int unsorted; // Size of the unsorted part of the array
  244. int temp; // Used during the swapping of two array locations
  245. comparisons=0;
  246. makeHeap(data, n);
  247.  
  248. unsorted = n;
  249.  
  250. while (unsorted > 1)
  251. {
  252. unsorted--;
  253.  
  254. // Swap the largest element (data[0]) with the final element of unsorted part
  255. temp = data[0];
  256. data[0] = data[unsorted];
  257. data[unsorted] = temp;
  258. comparisons++;
  259.  
  260. reheapifyDown(data, unsorted);
  261. }
  262. comparisons++;
  263. return comparisons;
  264. }
  265. private static void makeHeap(int[ ] data, int n)
  266. // Precondition: data is an array with at least n elements.
  267. // Postcondition: The elements of data have been rearranged so that the
  268. // complete binary tree represented by this array is a heap.
  269. {
  270. for(int i = 1; i < n; i++)
  271. {
  272. int k = i;
  273. while(data[k] != data[0] && data[k] > data[parent(k)])
  274. {
  275. int temp = data[k];
  276. data[k] = data[(k-1)/2];
  277. data[(k-1)/2] = temp;
  278. k = (k-1)/2;
  279. }
  280. }
  281. }
  282. private static void reheapifyDown(int[ ] data, int n)
  283. // Precondition: n > 0, and data is an array with at least n elements. These
  284. // elements form a heap, except that data[0] may be in an incorrect
  285. // location.
  286. // Postcondition: The data values have been rearranged so that the first
  287. // n elements of data now form a heap.
  288. {
  289. int current = 0;
  290. int bigChild;
  291. boolean heapOK=false;
  292. while((!heapOK) && leftChild(current)<n)
  293. {
  294. if(rightChild(current)>=n)
  295. bigChild = leftChild(current);
  296. else if (data[leftChild(current)] > data[rightChild(current)])
  297. bigChild=leftChild(current);
  298. else
  299. bigChild = rightChild(current);
  300. if(data[current]<data[bigChild])
  301. {
  302. int temp = data[current];
  303. data[current] = data[bigChild];
  304. data[bigChild] = temp;
  305. current = bigChild;
  306. comparisons++;
  307. }else{
  308. heapOK = true;
  309. }
  310. }
  311. }
  312. private static void comparisonTable()
  313. {
  314. System.out.println("---------------------------------------------------------------------");
  315. System.out.println(" N selectionsort insertionsort mergesort quicksort heapsort NlogN");
  316. System.out.println("---------------------------------------------------------------------");
  317. }
  318. public static int sort(int i,int[] data,int n)
  319. {
  320. int compareNum = 0;
  321. switch(i)
  322. {
  323. case 1:
  324. compareNum = selectionsort(data,0,n);
  325. break;
  326. case 2:
  327. compareNum = insertionsort(data,0,n);
  328. break;
  329. case 3:
  330. compareNum = mergesort(data,0,n);
  331. break;
  332. case 4:
  333. compareNum = quicksort(data,0,n);
  334. break;
  335. case 5:
  336. compareNum = heapsort(data,n);
  337. break;
  338. }
  339. return compareNum;
  340. }
  341. public static int nlogcalc(int n)
  342. {
  343. n = n * log(n,2);
  344. return n;
  345. }
  346. public static int log(int x,int base)
  347. {
  348. return (int)(Math.log(x)/Math.log(base));
  349. }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment