PageRenderTime 28ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/SortingMethods/src/AverageCase.java

https://bitbucket.org/GeoTrif/misc
Java | 342 lines | 207 code | 90 blank | 45 comment | 18 complexity | fb0ca9ab3ce086a9e8a8c6f22ad28698 MD5 | raw file
  1. /**
  2. * Metoda BubbleSort este cel mai simplu algoritm de sortare care functioneaza interschimband in mod repetat elementele adiacente daca nu sunt in ordine crescatoare.
  3. * Time complexity in cazul cel mai defavorabil este atunci cand vectorul este in ordine descrescatoare.Cazul mediu este atunci cand vectorul are elementele in ordine aleatoare,dar nu descrescatoare si crescatoare.
  4. * Cazul cel mai favorabil este atunci cand vectorul nostru este deja sortat.
  5. * Complexitatea in cazul cel mai defavorabil si in cazul mediu este de O(n * n).In cazul cel mai favorabil este O(n).
  6. * Acest algoritm este folosit doar pentru introducerea in conceptul algoritmilor de sortare.
  7. *
  8. *
  9. * Metoda Counting Sort este un algoritm de sortare bazat pe niste chei dintr-un interval specificat.Functioneaza numarand numarul de obiecte care au valori ale cheilor distincte,dupa care se calculeaza pozitia fiecarui obiect
  10. * in secventa care va afisa vectorul sortat.
  11. * Complexitatea timpului de rulare a algoritmului este in toate cele 3 cazuri(cel mai defavorabil,mediocru si cel mai favorabil) O(n + k).
  12. * Acest algoritm este eficient daca numarul datelor de intrare nu este cu mult mai mare decat numarul de obiecte care este sortat.Nu este un algoritm bazat pe comparatii.
  13. * De obicei este folosit ca si o subrutina pentru algoritmul Radix Sort.
  14. *
  15. *
  16. * Metoda Insertion Sort este un algoritm simplu de sortare care functioneaza la fel cum sortam niste carti de joc in mana.
  17. * Time complexity in cazul cel mai defavorabil este atunci cand vectorul este in ordine descrescatoare.Cazul mediu este atunci cand vectorul are elementele in ordine aleatoare,dar nu descrescatoare si crescatoare.
  18. * Cazul cel mai favorabil este atunci cand vectorul nostru este deja sortat.
  19. * Complexitatea in cazul cel mai defavorabil si in cazul mediu este de O(n * n).In cazul cel mai favorabil este O(n).
  20. * Paradigma algoritmului este abordarea incrementarii.
  21. * Acest algoritm este folosit atunci cand avem un vector cu putine elemente.
  22. *
  23. *
  24. * Algoritmul QuickSort este un algoritm cu paradigmul "Divide annd Conquer".Acest algoritm alege un element ca si pivot si partitioneaza
  25. * vectorul dat in jurul pivotului ales.Sunt mai multe versiuni ale acestui algoritm care alege pivotul in mod diferit.
  26. * De exemplu:
  27. * 1.Alege pivotul ca fiind primul element din vector.
  28. * 2.Alege pivotul ca fiind ultimul element din vector(implementata in programul de mai jos).
  29. * 3.Alege pivotul ca fiind un element arbitrar din vector.
  30. * 4.Alege pivotul ca fiind elementul median din vector.
  31. * Procesul cheie al acestui algoritm este reprezentat de metoda partition().Scopul acestei metode este acela de a pune un element x ales ca si pivot din vectorul dat in pozitia lui corecta in vectorul sortat si sa puna toate
  32. * elementele mai mici decat pivotul inaintea pivotului,iar cele mai mari decat pivotul dupa el.
  33. * Cazul cel mai defavorabil al timpului de rulare este atunci cand metoda partition() intotdeauna alege ultimul sau primul element ca si pivot.
  34. * Cazul cel mai favorabil este atunci cand metoda partition() alege intotdeauna elementul median al vectorului.
  35. * Cazul mediocru este atunci cand elementul pivot nu este primul,ultimul sau elementul median.
  36. */
  37. import java.io.File;
  38. import java.lang.reflect.Array;
  39. import java.util.Arrays;
  40. import java.util.Random;
  41. import java.util.Scanner;
  42. import org.omg.Messaging.SyncScopeHelper;
  43. import jxl.Workbook;
  44. import jxl.write.Label;
  45. import jxl.write.WritableSheet;
  46. import jxl.write.WritableWorkbook;
  47. public class AverageCase {
  48. public static int swapContorBubbleSort;
  49. public static int assigmentBubbleSort;
  50. public static int operationsSumBubbleSort;
  51. public static int swapContorCountingSort;
  52. public static int assigmentCountingSort;
  53. public static int operationsSumCountingSort;
  54. public static int swapContorInsertionSort;
  55. public static int assigmentInsertionSort;
  56. public static int operationsSumInsertionSort;
  57. public static int swapContorQuickSort;
  58. public static int assigmentQuickSort;
  59. public static int operationsSumQuickSort;
  60. public static void main(String[] args) throws Exception {
  61. File f = new File("C:\\Users\\GeoTrif\\Desktop\\excel.xlss");
  62. Scanner input = new Scanner(System.in);
  63. Random random = new Random();
  64. WritableWorkbook myExcel = Workbook.createWorkbook(f);
  65. WritableSheet mySheet = myExcel.createSheet("mySheet", 0);
  66. System.out.println("Input the number of elements of the array: ");
  67. int elementNum = input.nextInt();
  68. int[] myArrayBubbleSort = new int[elementNum];
  69. char[] myArrayCountingSort = new char[elementNum];
  70. int[] myArrayInsertionSort = new int[elementNum];
  71. int[] myArrayQuickSort = new int[elementNum];
  72. for (int i = 0; i < myArrayBubbleSort.length; i++) {
  73. myArrayBubbleSort[i] = random.nextInt(10000);
  74. assigmentBubbleSort++;
  75. }
  76. for (int i = 0; i < myArrayCountingSort.length; i++) {
  77. myArrayCountingSort[i] = (char) (97 + random.nextInt(25));
  78. assigmentCountingSort++;
  79. }
  80. for (int i = 0; i < myArrayInsertionSort.length; i++) {
  81. myArrayInsertionSort[i] = random.nextInt(10000);
  82. assigmentInsertionSort++;
  83. }
  84. for (int i = 0; i < myArrayQuickSort.length; i++) {
  85. myArrayQuickSort[i] = random.nextInt(10000);
  86. assigmentQuickSort++;
  87. }
  88. System.out.println("The original array for Bubble Sort is: " + Arrays.toString(myArrayBubbleSort));
  89. System.out.println("The original array for Insertion Sort is: " + Arrays.toString(myArrayInsertionSort));
  90. System.out.println("The original array for Quick Sort is: " + Arrays.toString(myArrayQuickSort));
  91. System.out.println("-------------------------------------");
  92. int[] sortedArrayBubbleSort = bubbleSort(myArrayBubbleSort);
  93. char[] sortedArrayCountingSort = countingSort(myArrayCountingSort);
  94. int[] sortedArrayInsertionSort = insertionSort(myArrayInsertionSort);
  95. quickSort(myArrayQuickSort, 0, myArrayQuickSort.length - 1);
  96. operationsSumBubbleSort = swapContorBubbleSort + assigmentBubbleSort;
  97. operationsSumCountingSort = swapContorCountingSort + assigmentCountingSort;
  98. operationsSumInsertionSort = swapContorInsertionSort + assigmentInsertionSort;
  99. operationsSumQuickSort = swapContorQuickSort + assigmentQuickSort;
  100. System.out.println("The sorted array for Bubble Sort is: " + Arrays.toString(sortedArrayBubbleSort));
  101. System.out.println("The sorted array for Counting Sort is: " + Arrays.toString(sortedArrayCountingSort));
  102. System.out.println("The sorted array for Insertion Sort is: " + Arrays.toString(sortedArrayInsertionSort));
  103. System.out.println("The sorted array for Quick Sort is: " + Arrays.toString(myArrayQuickSort));
  104. Label start = new Label(0, 0, "Sorting algorithms comparison");
  105. Label l1BubbleSort = new Label(0, 1, "BubbleSort");
  106. Label l2BubbleSort = new Label(0, 2, "Number of swaps");
  107. Label lSwapBubbleSort = new Label(1, 2, Integer.toString(swapContorBubbleSort));
  108. Label l3BubbleSort = new Label(0, 3, "Number of assigments ");
  109. Label lAssigmentBubbleSort = new Label(1, 3, Integer.toString(assigmentBubbleSort));
  110. Label l4BubbleSort = new Label(0, 4, "Total number of operations ");
  111. Label lTotalBubbleSort = new Label(1, 4, Integer.toString(operationsSumBubbleSort));
  112. Label l1CountingSort = new Label(0, 6, "CountingSort");
  113. Label l2CountingSort = new Label(0, 7, "Number of swaps");
  114. Label lSwapCountingSort = new Label(1, 7, Integer.toString(swapContorCountingSort));
  115. Label l3CountingSort = new Label(0, 8, "Number of assigments ");
  116. Label lAssigmentCountingSort = new Label(1, 8, Integer.toString(assigmentCountingSort));
  117. Label l4CountingSort = new Label(0, 9, "Total number of operations ");
  118. Label lTotalCountingSort = new Label(1, 9, Integer.toString(operationsSumCountingSort));
  119. Label l1InsertionSort = new Label(0, 11, "InsertionSort");
  120. Label l2InsertionSort = new Label(0, 12, "Number of swaps");
  121. Label lSwapInsertionSort = new Label(1, 12, Integer.toString(swapContorInsertionSort));
  122. Label l3InsertionSort = new Label(0, 13, "Number of assigments ");
  123. Label lAssigmentInsertionSort = new Label(1, 13, Integer.toString(assigmentInsertionSort));
  124. Label l4InsertionSort = new Label(0, 14, "Total number of operations ");
  125. Label lTotalInsertionSort = new Label(1, 14, Integer.toString(operationsSumInsertionSort));
  126. Label l1QuickSort = new Label(0, 16, "QuickSort");
  127. Label l2QuickSort = new Label(0, 17, "Number of swaps");
  128. Label lSwapQuickSort = new Label(1, 17, Integer.toString(swapContorQuickSort));
  129. Label l3QuickSort = new Label(0, 18, "Number of assigments ");
  130. Label lAssigmentQuickSort = new Label(1, 18, Integer.toString(assigmentQuickSort));
  131. Label l4QuickSort = new Label(0, 19, "Total number of operations ");
  132. Label lTotalQuickSort = new Label(1, 19, Integer.toString(operationsSumQuickSort));
  133. mySheet.addCell(start);
  134. mySheet.addCell(l1BubbleSort);
  135. mySheet.addCell(l2BubbleSort);
  136. mySheet.addCell(lSwapBubbleSort);
  137. mySheet.addCell(l3BubbleSort);
  138. mySheet.addCell(lAssigmentBubbleSort);
  139. mySheet.addCell(l4BubbleSort);
  140. mySheet.addCell(lTotalBubbleSort);
  141. mySheet.addCell(l1CountingSort);
  142. mySheet.addCell(l2CountingSort);
  143. mySheet.addCell(lSwapCountingSort);
  144. mySheet.addCell(l3CountingSort);
  145. mySheet.addCell(lAssigmentCountingSort);
  146. mySheet.addCell(l4CountingSort);
  147. mySheet.addCell(lTotalCountingSort);
  148. mySheet.addCell(l1InsertionSort);
  149. mySheet.addCell(l2InsertionSort);
  150. mySheet.addCell(lSwapInsertionSort);
  151. mySheet.addCell(l3InsertionSort);
  152. mySheet.addCell(lAssigmentInsertionSort);
  153. mySheet.addCell(l4InsertionSort);
  154. mySheet.addCell(lTotalInsertionSort);
  155. mySheet.addCell(l1QuickSort);
  156. mySheet.addCell(l2QuickSort);
  157. mySheet.addCell(lSwapQuickSort);
  158. mySheet.addCell(l3QuickSort);
  159. mySheet.addCell(lAssigmentQuickSort);
  160. mySheet.addCell(l4QuickSort);
  161. mySheet.addCell(lTotalQuickSort);
  162. myExcel.write();
  163. myExcel.close();
  164. System.out.println("Excel created.");
  165. }
  166. public static int[] bubbleSort(int[] array) { // Metoda care implementeaza Bubble Sort.
  167. int arrayLen = array.length;
  168. int temp = array[0];
  169. for (int i = 0; i < arrayLen - 1; i++) { // Bubble Sort utilizeaza metoda BruteForce cu 2 for-uri.
  170. for (int j = 0; j < arrayLen - i - 1; j++) {
  171. if (array[j] > array[j + 1]) {
  172. temp = array[j]; // Facem interschimbare de variabile utilizand o a treia variabila temp.
  173. array[j] = array[j + 1];
  174. array[j + 1] = temp;
  175. swapContorBubbleSort++;
  176. }
  177. }
  178. }
  179. return array;
  180. }
  181. public static char[] countingSort(char[] array) {
  182. int length = array.length;
  183. char[] output = new char[length]; // Vectorul de caractere rezultat care va avea vectorul sortat array.
  184. int[] count = new int[256]; // Am creat un vector count sa stocam toate caracterele individuale.
  185. for (int i = 0; i < 256; ++i) {
  186. count[i] = 0; // Initializam vectorul count cu 0.
  187. }
  188. for (int i = 0; i < length; ++i) { // Stocam count pentru fiecare caracter din array.
  189. ++count[array[i]];
  190. }
  191. for (int i = 1; i <= 255; ++i) {
  192. count[i] += count[i - 1]; // Schimbam count[i] astfel incat count[i] sa contina pozitia actuala a
  193. swapContorCountingSort++; // caracterului din vectorul rezultat.
  194. }
  195. for (int i = 0; i < length; ++i) {
  196. output[count[array[i]] - 1] = array[i]; // Construim vectorul de caractere rezultat.
  197. --count[array[i]];
  198. }
  199. for (int i = 0; i < length; ++i) { // Copiem vectorul rezultat vectorului nostru array astfel incat array sa
  200. // contina caracterele sortate.
  201. array[i] = output[i];
  202. }
  203. return array;
  204. }
  205. public static int[] insertionSort(int[] array) { // Metoda pentru a sorta vectorul cu algoritmul insertion sort.
  206. int arrayLen = array.length;
  207. for (int i = 1; i < arrayLen; ++i) {
  208. int key = array[i];
  209. int j = i - 1;
  210. while (j >= 0 && array[j] > key) { // Mutarea elementelor din array[0..i-1],care sunt mai mari decat key,cu
  211. // o pozitie mai in fata decat pozitia curenta.
  212. array[j + 1] = array[j];
  213. j = j - 1;
  214. swapContorInsertionSort++;
  215. }
  216. array[j + 1] = key;
  217. }
  218. return array;
  219. }
  220. // QuickSort.
  221. public static int quickPartition(int[] array, int low, int high) {
  222. int pivot = array[high];
  223. int i = low - 1; // Indexul elementelor mai mici decat pivotul.
  224. int temp = 0;
  225. for (int j = low; j < high; j++) {
  226. if (array[j] <= pivot) { // Daca elementul curent este mai mic sau egal cu pivotul.
  227. i++;
  228. temp = array[i]; // Interschimba array[i] cu array[j];
  229. array[i] = array[j];
  230. array[j] = temp;
  231. swapContorQuickSort++;
  232. }
  233. }
  234. temp = array[i + 1]; // Interschimba array[i + 1] cu array[high] (sau array[pivot]).
  235. array[i + 1] = array[high];
  236. array[high] = temp;
  237. return i + 1;
  238. }
  239. /*
  240. * Metoda care implementeaza algoritmul QuickSort. Array[] --> vectorul ce
  241. * trebuie sortat. low --> indexul de start. high --> indexul de sfarsit.
  242. */
  243. public static void quickSort(int[] array, int low, int high) {
  244. if (low < high) {
  245. int pi = quickPartition(array, low, high); // pi este indexul de partitie,array[pi] este acuma la indexul
  246. // potrivit.
  247. quickSort(array, low, pi - 1); // Sortare recursiva a elementelor inaintea partitionarii.
  248. quickSort(array, pi + 1, high); // Sortare recursiva a elementelor dupa partitionare.
  249. }
  250. }
  251. }