Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package algoritmemodul1;
  7.  
  8. import java.util.Arrays;
  9.  
  10. /**
  11. *
  12. * @author Mikael
  13. */
  14. public class AlgoritmeModul1 {
  15.  
  16.  
  17. private int[] theArray;
  18.  
  19. private int arraySize;
  20.  
  21. private int itemsInArray = 0;
  22.  
  23. private int incrementCounter = 0;
  24.  
  25. static long startTime;
  26.  
  27. static long endTime;
  28.  
  29.  
  30.  
  31. AlgoritmeModul1(int size) {
  32.  
  33. arraySize = size;
  34.  
  35. theArray = new int[size];
  36.  
  37. }
  38.  
  39.  
  40.  
  41. /**
  42. * @param args the command line arguments
  43. */
  44. public static void main(String[] args) {
  45. // TODO code application logic here
  46.  
  47. AlgoritmeModul1 testAlgo2 = new AlgoritmeModul1(100000);
  48.  
  49. testAlgo2.generateRandomArray();
  50.  
  51. AlgoritmeModul1 testAlgo3 = new AlgoritmeModul1(200000);
  52.  
  53. testAlgo3.generateRandomArray();
  54.  
  55. AlgoritmeModul1 testAlgo4 = new AlgoritmeModul1(100000);
  56.  
  57. testAlgo4.generateRandomArray();
  58.  
  59. AlgoritmeModul1 testAlgo5 = new AlgoritmeModul1(200000);
  60.  
  61. testAlgo4.generateRandomArray();
  62.  
  63. //MÅ HUSKE Å ENDRE NAVN SÅ ALGORITMEN IKKE GÅR OVER LISTEN SOM ALLEREDERE
  64. //ER SORTERT XD
  65.  
  66.  
  67. testAlgo2.bubbleSort();
  68. testAlgo3.bubbleSort();
  69.  
  70. testAlgo4.insertionSort();
  71. testAlgo4.insertionSort();
  72.  
  73. /* for(int i : theArray){
  74. System.println(i);
  75. } */
  76.  
  77.  
  78. // System.out.println(testAlgo2.theArray);
  79.  
  80. // System.out.println(testAlgo2.toString());
  81.  
  82. // testAlgo4.insertionSort();
  83.  
  84. }
  85.  
  86.  
  87. public void bubbleSort() {
  88.  
  89. startTime = System.currentTimeMillis();
  90.  
  91. for (int i = arraySize - 1; i > 1; i--) {
  92.  
  93. for (int j = 0; j < i; j++) {
  94.  
  95. if (theArray[j] > theArray[j + 1]) {
  96.  
  97. swapValues(j, j + 1);
  98.  
  99.  
  100. }
  101.  
  102. }
  103.  
  104. }
  105.  
  106.  
  107. endTime = System.currentTimeMillis();
  108.  
  109. System.out.println("Bubble Sort Took " + (endTime - startTime));
  110.  
  111. }
  112.  
  113.  
  114. public void insertionSort(){
  115.  
  116. startTime = System.currentTimeMillis();
  117.  
  118. // int j, i, temp;
  119. for(int i = 1; i < theArray.length; i++){
  120. int temp = theArray[i];
  121. int j = i-1; //change - don't -1
  122. while(j >= 0 && theArray[j] > temp){ //change here, must go from 0 and note change to index
  123. theArray[j+1] = theArray[j];
  124. j--;
  125. }
  126. theArray[j+1] = temp;
  127. }
  128.  
  129. endTime = System.currentTimeMillis();
  130.  
  131. System.out.println("InsertionSort Sort Took " + (endTime - startTime));
  132. }
  133.  
  134.  
  135.  
  136.  
  137. public void generateRandomArray() {
  138.  
  139. for (int i = 0; i < arraySize; i++) {
  140.  
  141. theArray[i] = (int) (Math.random() * 1000) + 10;
  142.  
  143. }
  144.  
  145. itemsInArray = arraySize - 1;
  146.  
  147. }
  148.  
  149.  
  150. public void swapValues(int indexOne, int indexTwo) {
  151.  
  152. int temp = theArray[indexOne];
  153.  
  154. theArray[indexOne] = theArray[indexTwo];
  155.  
  156. theArray[indexTwo] = temp;
  157.  
  158. }
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement