JAVA 10
1st Lecture - BSCS 3 - Data Structure Lab Guest on 28th September 2020 03:47:28 PM
  1. Big O Notation - Determines the efficiency of the program.
  2.  
  3. Amount of execution is directly proportional to the time it takes to run the program.
  4.  
  5. For example, if there is a loop in program, it depends on how long the loop runs.
  6. 0(n)
  7.  
  8. Big O - depends on inputs
  9.  
  10. In case of two nested loops, n ~ 2 will be used - ALWAYS HAVE QUADRATIC GRAPHS
  11. In case of three nested loops, n ~ 3 will be used - ALWAYS HAVE QUADRATIC GRAPHS
  12. In case of multiple loops, 2n will be used for Big O Notation. Inputs MUST BE SAME EVERYTIME - LINEAR GRAPHS IN NORMAL
  13.  
  14. Based on the graphs, Quadratic graphs perform it more faster than Linear graphs.
  15.  
  16. Dynamic Arrays - Expand the size of an array
  17.  
  18. Best Case - the best part of the code
  19.  
  20. Code done in class:
  21. package com.company;
  22.  
  23. public class Main {
  24.  
  25.     public static void main(String[] args) {
  26.         // write your code here
  27.         // Assignment - Extend the array and find the largest number in array
  28. ArrayDS array = new ArrayDS(3);
  29. array.insert(1);
  30. array.insert(2);
  31. array.insert(3);
  32.         array.insert(4);
  33.         array.insert(20);
  34.         array.insert(50);
  35. //array.removeAt(1);
  36. //array.insert(3);
  37.         System.out.println("Index Of 2 ="+array.indexOf(2));
  38.         array.size();
  39. array.print();
  40.         System.out.println("The largest number in an array is: "+array.largestNumber());
  41.     }
  42. }
  43.  
  44.  
  45. // CLASS ARRAYDS
  46.  
  47. package com.company;
  48.  
  49. public class ArrayDS {
  50.     private int[] items;
  51.     private int count;
  52.  
  53.     public ArrayDS(int length){
  54.         items = new int[length];
  55.     }
  56.  
  57.     public void print(){
  58.         for (int i=0;i<count;i++){
  59.             System.out.println(items[i]);
  60.         }
  61.     }
  62. // Doubling the array size when array size exceeds
  63.     public void insert(int item){
  64.         if(items.length == count){
  65.             int[] newItems = new int[count*2];
  66.             for (int i=0;i<count;i++){
  67.                 newItems[i] = items[i];
  68.             }
  69.             items = newItems;
  70.         }
  71.         items[count++] = item;
  72.     }
  73.  
  74. public void size(){
  75.     System.out.println("Size of an array: "+items.length);
  76. }
  77.  
  78. public void removeAt(int index){ // index 1
  79.         if (index<0 || index >=count){
  80.             throw new IllegalArgumentException();
  81.         }
  82.         for (int i=index;i<count;i++){ // i=1
  83.             items[i] = items[i+1];
  84.         }
  85.         count--;
  86. }
  87.  
  88. public int indexOf(int item){
  89.         for (int i=0;i<count;i++){
  90.             if(items[i] == item)
  91.                 return i;
  92.     }
  93.         return -1;
  94. }
  95.  
  96. public int largestNumber(){
  97.         int largest=0;
  98.         for (int i=0;i<count;i++){
  99.             if (items[i] > largest)
  100.                 largest = items[i];
  101.         }
  102.         return largest;
  103. }
  104.  
  105.  
  106. }
  107.  
  108.  
  109. Inside an array - to lookup o(1) - exact location
  110. To insert o(n)
  111. Delete o(1) in worst case.

Coding Base is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.