Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class combinationSorter {
  4.  
  5. public static int arraySize = 10;
  6. public static int first;
  7. public static int second;
  8. public static int small = 0;
  9. public static int selection1[] = new int[arraySize];
  10. public static int selection2[] = new int[arraySize];
  11. public static int bubble[] = new int[arraySize];
  12. public static int insertion[] = new int[arraySize];
  13. public static Random rand = new Random();
  14. public static int bubbleComparison = 0;
  15. public static int selectionComparison = 0;
  16. public static int insertionComparison = 0;
  17. public static int timesChecked = 0;
  18. public static int size = 0;
  19.  
  20. public static void pause(int time)
  21. {
  22. try
  23. {
  24. Thread.sleep(time);
  25. }
  26. catch(InterruptedException ex)
  27. {
  28. Thread.currentThread().interrupt();
  29. }
  30. }
  31.  
  32. public static void selectionSort(){
  33. small = selection1[0];
  34. int place = 0;
  35. for(int outerLoop=0; outerLoop<arraySize;outerLoop++){
  36.  
  37. for(int innerLoop=0; innerLoop<arraySize; innerLoop++)
  38. {
  39. if((selection1[innerLoop]<=small) && (selection1[innerLoop]>0))
  40. {
  41. small = selection1[innerLoop];
  42. place=innerLoop;
  43.  
  44. }
  45. selectionComparison++;
  46. }
  47. selection2[outerLoop]=small;
  48. selection1[place]=0;
  49. small=Integer.MAX_VALUE;
  50. }
  51. }
  52.  
  53. public static void bubbleSort(){
  54. while(true)
  55. {
  56. timesChecked = 0;
  57. for(int bubbleLoop=0; bubbleLoop<arraySize-1;bubbleLoop++){
  58.  
  59. if(bubble[bubbleLoop]>bubble[bubbleLoop+1])
  60. {
  61. first = bubble[bubbleLoop];
  62. second = bubble[(bubbleLoop)+1];
  63. bubble[bubbleLoop]=second;
  64. bubble[(bubbleLoop)+1] = first;
  65. timesChecked++;
  66.  
  67. }
  68. bubbleComparison++;
  69. }
  70. if(timesChecked==0)
  71. {
  72. break;
  73. }
  74. }
  75. }
  76.  
  77. public static void insertionSort(){
  78. while(size!=arraySize){
  79. for(int i=size;i>0;i--){
  80. if(insertion[i]>insertion[i-1]){
  81. break;
  82. }
  83. else {
  84. int temp=insertion[i];
  85. insertion[i]=insertion[i-1];
  86. insertion[i-1]=temp;
  87. }
  88. insertionComparison++;
  89. }
  90. size++;
  91. }
  92. }
  93.  
  94.  
  95. public static void main(String args[]) {
  96. for (int randomNumbers = 0; randomNumbers<arraySize; randomNumbers++){
  97. selection1[randomNumbers] = rand.nextInt(100)+1;
  98. bubble[randomNumbers] = selection1[randomNumbers];
  99. insertion[randomNumbers] = selection1[randomNumbers];
  100. }
  101. /*
  102. for (int displayRandomNumbers = 0; displayRandomNumbers<arraySize; displayRandomNumbers++){
  103. System.out.println(bubble[displayRandomNumbers]);
  104. }
  105. */
  106. selectionSort();
  107. bubbleSort();
  108. insertionSort();
  109. System.out.println("---------------");
  110. System.out.println("Selection Comparisons: "+selectionComparison);
  111. System.out.println("Bubble Comparisons: "+bubbleComparison);
  112. System.out.println("Insertion Comparisons: "+insertionComparison);
  113. System.out.println("---------------");
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement