Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package algoritmemodul1;
- import java.util.Arrays;
- /**
- *
- * @author Mikael
- */
- public class AlgoritmeModul1 {
- private int[] theArray;
- private int arraySize;
- private int itemsInArray = 0;
- private int incrementCounter = 0;
- static long startTime;
- static long endTime;
- AlgoritmeModul1(int size) {
- arraySize = size;
- theArray = new int[size];
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- AlgoritmeModul1 testAlgo2 = new AlgoritmeModul1(100000);
- testAlgo2.generateRandomArray();
- AlgoritmeModul1 testAlgo3 = new AlgoritmeModul1(200000);
- testAlgo3.generateRandomArray();
- AlgoritmeModul1 testAlgo4 = new AlgoritmeModul1(100000);
- testAlgo4.generateRandomArray();
- AlgoritmeModul1 testAlgo5 = new AlgoritmeModul1(200000);
- testAlgo4.generateRandomArray();
- //MÅ HUSKE Å ENDRE NAVN SÅ ALGORITMEN IKKE GÅR OVER LISTEN SOM ALLEREDERE
- //ER SORTERT XD
- testAlgo2.bubbleSort();
- testAlgo3.bubbleSort();
- testAlgo4.insertionSort();
- testAlgo4.insertionSort();
- /* for(int i : theArray){
- System.println(i);
- } */
- // System.out.println(testAlgo2.theArray);
- // System.out.println(testAlgo2.toString());
- // testAlgo4.insertionSort();
- }
- public void bubbleSort() {
- startTime = System.currentTimeMillis();
- for (int i = arraySize - 1; i > 1; i--) {
- for (int j = 0; j < i; j++) {
- if (theArray[j] > theArray[j + 1]) {
- swapValues(j, j + 1);
- }
- }
- }
- endTime = System.currentTimeMillis();
- System.out.println("Bubble Sort Took " + (endTime - startTime));
- }
- public void insertionSort(){
- startTime = System.currentTimeMillis();
- // int j, i, temp;
- for(int i = 1; i < theArray.length; i++){
- int temp = theArray[i];
- int j = i-1; //change - don't -1
- while(j >= 0 && theArray[j] > temp){ //change here, must go from 0 and note change to index
- theArray[j+1] = theArray[j];
- j--;
- }
- theArray[j+1] = temp;
- }
- endTime = System.currentTimeMillis();
- System.out.println("InsertionSort Sort Took " + (endTime - startTime));
- }
- public void generateRandomArray() {
- for (int i = 0; i < arraySize; i++) {
- theArray[i] = (int) (Math.random() * 1000) + 10;
- }
- itemsInArray = arraySize - 1;
- }
- public void swapValues(int indexOne, int indexTwo) {
- int temp = theArray[indexOne];
- theArray[indexOne] = theArray[indexTwo];
- theArray[indexTwo] = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement