Advertisement
ridjis

Big O notation

Nov 10th, 2014
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.04 KB | None | 0 0
  1. // Big O notation is a way to measure how well a
  2. // computer algorithm scales as the amount of data
  3. // involved increases. It is not always a measure
  4. // of speed as you'll see below
  5.  
  6. // This is a rough overview of Big O and doesn't
  7. // cover topics such as asymptotic analysis, which
  8. // covers comparing algorithms as data approaches
  9. // infinity
  10.  
  11. // What we are defining is the part of the algorithm
  12. // that has the greatest effect. For example
  13. // 45n^3 + 20n^2 + 19 = 84 (n=1)
  14. // 45n^3 + 20n^2 + 19 = 459 (n=2) Does 19 matter?
  15. // 45n^3 + 20n^2 + 19 = 47019 (n=10)
  16. // Does the 20n^2 matter if 45n^3 = 45,000?
  17. // Has an O(n^3) Order of n-cubed
  18.  
  19. public class BigONotation {
  20.  
  21.     private int[] theArray;
  22.  
  23.     private int arraySize;
  24.  
  25.     private int itemsInArray = 0;
  26.  
  27.     static long startTime;
  28.  
  29.     static long endTime;
  30.  
  31.     public static void main(String[] args) {
  32.  
  33.         /*
  34.          * 0(1) Test BigONotation testAlgo = new BigONotation(10);
  35.          *
  36.          * testAlgo.addItemToArray(10);
  37.          *
  38.          * System.out.println(Arrays.toString(testAlgo.theArray));
  39.          */
  40.  
  41.         BigONotation testAlgo2 = new BigONotation(100000);
  42.         testAlgo2.generateRandomArray();
  43.  
  44.         BigONotation testAlgo3 = new BigONotation(200000);
  45.         testAlgo3.generateRandomArray();
  46.  
  47.         BigONotation testAlgo4 = new BigONotation(30000);
  48.         testAlgo4.generateRandomArray();
  49.  
  50.         BigONotation testAlgo5 = new BigONotation(400000);
  51.         testAlgo5.generateRandomArray();
  52.  
  53.         /*
  54.          * O(N) Test
  55.          *
  56.          * testAlgo2.linearSearchForValue(20);
  57.          *
  58.          * testAlgo3.linearSearchForValue(20);
  59.          *
  60.          * testAlgo4.linearSearchForValue(20);
  61.          *
  62.          * testAlgo5.linearSearchForValue(20);
  63.          */
  64.  
  65.         // O(N^2) Test
  66.         /*
  67.          * testAlgo2.bubbleSort();
  68.          *
  69.          * testAlgo3.bubbleSort();
  70.          *
  71.          * testAlgo4.bubbleSort();
  72.          *
  73.          * // 0 (log N) Test
  74.          *
  75.          * testAlgo2.binarySearchForValue(20);
  76.          * testAlgo3.binarySearchForValue(20);
  77.          */
  78.  
  79.         // O (n log n) Test
  80.  
  81.         startTime = System.currentTimeMillis();
  82.  
  83.         testAlgo2.quickSort(0, testAlgo2.itemsInArray);
  84.  
  85.         endTime = System.currentTimeMillis();
  86.  
  87.         System.out.println("Quick Sort Took " + (endTime - startTime));
  88.  
  89.     }
  90.  
  91.     // O(1) An algorithm that executes in the same
  92.     // time regardless of the amount of data
  93.     // This code executes in the same amount of
  94.     // time no matter how big the array is
  95.  
  96.     public void addItemToArray(int newItem) {
  97.  
  98.         theArray[itemsInArray++] = newItem;
  99.  
  100.     }
  101.  
  102.     // 0(N) An algorithm thats time to complete will
  103.     // grow in direct proportion to the amount of data
  104.     // The linear search is an example of this
  105.  
  106.     // To find all values that match what we
  107.     // are searching for we will have to look in
  108.     // exactly each item in the array
  109.  
  110.     // If we just wanted to find one match the Big O
  111.     // is the same because it describes the worst
  112.     // case scenario in which the whole array must
  113.     // be searched
  114.  
  115.     public void linearSearchForValue(int value) {
  116.  
  117.         boolean valueInArray = false;
  118.         String indexsWithValue = "";
  119.  
  120.         startTime = System.currentTimeMillis();
  121.  
  122.         for (int i = 0; i < arraySize; i++) {
  123.  
  124.             if (theArray[i] == value) {
  125.                 valueInArray = true;
  126.                 indexsWithValue += i + " ";
  127.             }
  128.  
  129.         }
  130.  
  131.         System.out.println("Value Found: " + valueInArray);
  132.  
  133.         endTime = System.currentTimeMillis();
  134.  
  135.         System.out.println("Linear Search Took " + (endTime - startTime));
  136.  
  137.     }
  138.  
  139.     // O(N^2) Time to complete will be proportional to
  140.     // the square of the amount of data (Bubble Sort)
  141.     // Algorithms with more nested iterations will result
  142.     // in O(N^3), O(N^4), etc. performance
  143.  
  144.     // Each pass through the outer loop (0)N requires us
  145.     // to go through the entire list again so N multiplies
  146.     // against itself or it is squared
  147.  
  148.     public void bubbleSort() {
  149.  
  150.         startTime = System.currentTimeMillis();
  151.  
  152.         for (int i = arraySize - 1; i > 1; i--) {
  153.  
  154.             for (int j = 0; j < i; j++) {
  155.  
  156.                 if (theArray[j] > theArray[j + 1]) {
  157.  
  158.                     swapValues(j, j + 1);
  159.  
  160.                 }
  161.             }
  162.         }
  163.  
  164.         endTime = System.currentTimeMillis();
  165.  
  166.         System.out.println("Bubble Sort Took " + (endTime - startTime));
  167.     }
  168.  
  169.     // O (log N) Occurs when the data being used is decreased
  170.     // by roughly 50% each time through the algorithm. The
  171.     // Binary search is a perfect example of this.
  172.  
  173.     // Pretty fast because the log N increases at a dramatically
  174.     // slower rate as N increases
  175.  
  176.     // O (log N) algorithms are very efficient because increasing
  177.     // the amount of data has little effect at some point early
  178.     // on because the amount of data is halved on each run through
  179.  
  180.     public void binarySearchForValue(int value) {
  181.  
  182.         startTime = System.currentTimeMillis();
  183.  
  184.         int lowIndex = 0;
  185.         int highIndex = arraySize - 1;
  186.  
  187.         int timesThrough = 0;
  188.  
  189.         while (lowIndex <= highIndex) {
  190.  
  191.             int middleIndex = (highIndex + lowIndex) / 2;
  192.  
  193.             if (theArray[middleIndex] < value)
  194.                 lowIndex = middleIndex + 1;
  195.  
  196.             else if (theArray[middleIndex] > value)
  197.                 highIndex = middleIndex - 1;
  198.  
  199.             else {
  200.  
  201.                 System.out.println("\nFound a Match for " + value
  202.                         + " at Index " + middleIndex);
  203.  
  204.                 lowIndex = highIndex + 1;
  205.  
  206.             }
  207.  
  208.             timesThrough++;
  209.  
  210.         }
  211.  
  212.         // This doesn't really show anything because
  213.         // the algorithm is so fast
  214.  
  215.         endTime = System.currentTimeMillis();
  216.  
  217.         System.out.println("Binary Search Took " + (endTime - startTime));
  218.  
  219.         System.out.println("Times Through: " + timesThrough);
  220.  
  221.     }
  222.  
  223.     // O (n log n) Most sorts are at least O(N) because
  224.     // every element must be looked at, at least once.
  225.     // The bubble sort is O(N^2)
  226.     // To figure out the number of comparisons we need
  227.     // to make with the Quick Sort we first know that
  228.     // it is comparing and moving values very
  229.     // efficiently without shifting. That means values
  230.     // are compared only once. So each comparison will
  231.     // reduce the possible final sorted lists in half.
  232.     // Comparisons = log n! (Factorial of n)
  233.     // Comparisons = log n + log(n-1) + .... + log(1)
  234.     // This evaluates to n log n
  235.  
  236.     public void quickSort(int left, int right) {
  237.  
  238.         if (right - left <= 0)
  239.             return;
  240.  
  241.         else {
  242.  
  243.             int pivot = theArray[right];
  244.  
  245.             int pivotLocation = partitionArray(left, right, pivot);
  246.  
  247.             quickSort(left, pivotLocation - 1);
  248.             quickSort(pivotLocation + 1, right);
  249.  
  250.         }
  251.  
  252.     }
  253.  
  254.     public int partitionArray(int left, int right, int pivot) {
  255.  
  256.         int leftPointer = left - 1;
  257.         int rightPointer = right;
  258.  
  259.         while (true) {
  260.  
  261.             while (theArray[++leftPointer] < pivot)
  262.                 ;
  263.  
  264.             while (rightPointer > 0 && theArray[--rightPointer] > pivot)
  265.                 ;
  266.  
  267.             if (leftPointer >= rightPointer) {
  268.  
  269.                 break;
  270.  
  271.             } else {
  272.  
  273.                 swapValues(leftPointer, rightPointer);
  274.  
  275.             }
  276.  
  277.         }
  278.  
  279.         swapValues(leftPointer, right);
  280.  
  281.         return leftPointer;
  282.  
  283.     }
  284.  
  285.     BigONotation(int size) {
  286.  
  287.         arraySize = size;
  288.  
  289.         theArray = new int[size];
  290.  
  291.     }
  292.  
  293.     public void generateRandomArray() {
  294.  
  295.         for (int i = 0; i < arraySize; i++) {
  296.  
  297.             theArray[i] = (int) (Math.random() * 1000) + 10;
  298.  
  299.         }
  300.  
  301.         itemsInArray = arraySize - 1;
  302.  
  303.     }
  304.  
  305.     public void swapValues(int indexOne, int indexTwo) {
  306.  
  307.         int temp = theArray[indexOne];
  308.         theArray[indexOne] = theArray[indexTwo];
  309.         theArray[indexTwo] = temp;
  310.  
  311.     }
  312.  
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement