Advertisement
ChaiTeaNunes

ArrayLists and Sorting Algorithims in C++

Jul 24th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.89 KB | None | 0 0
  1. /*
  2. Lists that can be manipulated qucikly and easily. With influence of Java ArrayLists (java.util.ArrayList).
  3. Author: Chaiyawat Nunes (chaiteanunes@gmail.com) (https://github.com/ChaiTeaNunes).
  4. Lisence: GNU GPL v2.0 (http://www.gnu.org/licenses/gpl.txt).
  5. */
  6.  
  7. #ifndef __LIST__
  8. #define __LIST__
  9.  
  10. #include <typeinfo>
  11.  
  12. /**
  13.  * Lists that can be manipulated qucikly and easily. With influence of Java ArrayLists (java.util.ArrayList).
  14.  *
  15.  * @author  Chaiyawat Nunes thaiberius.code@gmail.com
  16.  * @version 1.0
  17.  */
  18. template <typename TYPE>
  19. class List {
  20. private:
  21.  
  22.     /**
  23.      * The object that will be manipulated for the list.
  24.      */
  25.     TYPE * objects;
  26.  
  27.     /**
  28.      * Size of the list.
  29.      */
  30.     int size;
  31.  
  32. public:
  33.  
  34.     /**
  35.      * Returns the size of the list, which is crucial for sorting.
  36.      *
  37.      * @return  the size of the list
  38.      */
  39.     int getSize() {
  40.         return size;
  41.     }
  42.  
  43.     /**
  44.      * Returns an object that can be manipulated and accessed.
  45.      *
  46.      * @param   index   the index to locate the object within the list
  47.      * @return  the object at the give index of the list
  48.      */
  49.     TYPE &operator[](int index) {
  50.         return objects[index];
  51.     }
  52.  
  53.     /**
  54.      * Returns an object that can be manipulated and accessed.
  55.      *
  56.      * @param   index   the index to locate the object within the list
  57.      * @return  the object at the give index of the list
  58.      */
  59.     TYPE get(int index) {
  60.         return objects[index];
  61.     }
  62.  
  63.     /**
  64.      * Sets the size of the list, trimming objects from the end and seting new objects to 0.
  65.      *
  66.      * @param   newSize the new size of the list
  67.      */
  68.     void setSize(int newSize) {
  69.         TYPE * newObjects = new TYPE[newSize];
  70.         if (objects != nullptr) {
  71.             for (int i = 0; i < newSize && i < getSize(); i++) {
  72.                 newObjects[i] = get(i);
  73.             }
  74.             delete[] objects;
  75.         }
  76.         objects = newObjects;
  77.         size = newSize;
  78.     }
  79.  
  80.     /**
  81.      * Sets the object at the given index of the list.
  82.      *
  83.      * @param   index   the index to locate the object within the list
  84.      * @param   value   the new value for the selected object
  85.      */
  86.     void set(int index, TYPE value) {
  87.         objects[index] = value;
  88.     }
  89.  
  90.     /**
  91.      * Adds an object to the end of the list.
  92.      *
  93.      * @param   value   the new value for the new object
  94.      */
  95.     void add(TYPE value) {
  96.         setSize(getSize() + 1);
  97.         set(getSize() - 1, value);
  98.     }
  99.  
  100.     /**
  101.      * Adds an object at the given index and moves the objects after the index of the list.
  102.      *
  103.      * @param   index   the location of the new object
  104.      */
  105.     void insert(int index) {
  106.         setSize(getSize() + 1);
  107.         for (int i = getSize() - 1; i > index; i--) {
  108.             set(i, get(i - 1));
  109.         }
  110.         set(index, 0);
  111.     }
  112.  
  113.     /**
  114.      * Adds an object at the given index and moves the objects after the index of the list.
  115.      *
  116.      * @param   index   the location of the new object
  117.      */
  118.     void insert(int index, TYPE value) {
  119.         setSize(getSize() + 1);
  120.         for (int i = getSize() - 1; i > index; i--) {
  121.             set(i, get(i - 1));
  122.         }
  123.         set(index, value);
  124.     }
  125.  
  126.     /**
  127.      * Removes and object at the given index and moves the object after the index of the list.
  128.      *
  129.      * @param   index   the location of the new object
  130.      */
  131.     void removeAt(int index) {
  132.         for (int i = index; i < getSize(); i++) {
  133.             set(i, get(i - 1));
  134.         }
  135.         setSize(getSize() - 1);
  136.     }
  137.  
  138.     /**
  139.      * Swaps two objects in the list.
  140.      *
  141.      * @param   index1  first index to swap
  142.      * @param   index2  second index to swap
  143.      */
  144.     void swap(int index1, int index2) {
  145.         TYPE temp = get(index2);
  146.         set(end, get(index1));
  147.         set(index1, temp);
  148.     }
  149.  
  150.     /**
  151.      * Moves an object to a new place in the list.
  152.      *
  153.      * @param   start   the beginning location of the object
  154.      * @param   end     the ending location of the object
  155.      */
  156.     void move(int start, int end) {
  157.         insert(end, get(start));
  158.         removeAt(start);
  159.     }
  160.  
  161.     /**
  162.      * Moves an object to the front of the list.
  163.      *
  164.      * @param   index   the location of the object to move to the front
  165.      */
  166.     void moveToFront(int index) {
  167.         move(index, 0);
  168.     }
  169.  
  170.     /**
  171.      * Sets an object of the list to 0.
  172.      *
  173.      * @param   index   the location of the object to set to 0
  174.      */
  175.     void reset(int index) {
  176.         set(index, 0);
  177.     }
  178.  
  179.     /**
  180.      * Checks of the list is empty or not.
  181.      *
  182.      * @returns true if the size is 0
  183.      */
  184.     bool isEmpty() {
  185.         return getSize() == 0;
  186.     }
  187.  
  188.     /**
  189.      * Checks if an object in the list is equal to the given value.
  190.      *
  191.      * @param   index   the location of the object to compare
  192.      * @param   value   the value to compare
  193.      * @returns true if the two objects have the same value
  194.      */
  195.     bool equals(int index, TYPE value) {
  196.         return get(index) == value;
  197.     }
  198.  
  199.     /**
  200.      * Flips all of the objects in the list.
  201.      */
  202.     void flip() {
  203.         for (int i = 0; i < getSize() / 2; i++) {
  204.             swap(i, getSize() - 1 - i);
  205.         }
  206.     }
  207.  
  208.     /**
  209.      * Checks to see if the given object of the list is 0.
  210.      *
  211.      * @param   index   the location of the object to compare
  212.      * @returns true if the object's value is 0
  213.      */
  214.     bool isZero(int index) {
  215.         return equals(i, 0);
  216.     }
  217.  
  218.     /**
  219.      * Checks if 0 is present in the list.
  220.      *
  221.      * @returns true if a zero is present
  222.      */
  223.     bool isZeroPresent() {
  224.         for (int i = 0; i < getSize(); i++) {
  225.             bool foundZero;
  226.             if (isZero(i)) {
  227.                 foundZero = true;
  228.                 break;
  229.             }
  230.             else {
  231.                 foundZero = false;
  232.             }
  233.         }
  234.         return foundZero;
  235.     }
  236.  
  237.     /**
  238.      * Gets the index of the first 0 in the list.
  239.      *
  240.      * @returns the location of the first 0 in the list or 0 if there is not 0 present.]
  241.      */
  242.     int getZeroIndex() {
  243.         if (isZeroPresent()) {
  244.             for (int i = 0; i < getSize(); i++) {
  245.                 if (isZero(i)) {
  246.                     return i;
  247.                 }
  248.             }
  249.         }
  250.         else {
  251.             return 0;
  252.         }
  253.     }
  254.  
  255.     /**
  256.      * Clears the list.
  257.      */
  258.     void clear(){
  259.         if (objects != nullptr) {
  260.             delete[] objects;
  261.             objects = nullptr;
  262.             setSize(0);
  263.         }
  264.     }
  265.  
  266.     /**
  267.      * O(n) and o(n^2) Bubble Sort.
  268.      */
  269.     void bubbleSort() {
  270.         if (getSize() > 1){
  271.             for (int i = 0; i < getSize(); i++) {
  272.                 for (int j = 0; j < getSize() - j; j++) {
  273.                     if (get(j) > get(j + 1)) {
  274.                         swap(j, j + 1);
  275.                     }
  276.                 }
  277.             }
  278.         }
  279.     }
  280.  
  281.     /**
  282.      * O(n) and o(n^2) Insertion Sort.
  283.      */
  284.     void insertionSort() {
  285.         for (int i = 0; i < getSize(); i++) {
  286.             TYPE temp = get(i);
  287.             int j;
  288.             for (int j = i - 1; j >= 0 && temp < get(j); j--) {
  289.                 set(j + 1, get(j));
  290.             }
  291.             set(j + 1, temp);
  292.         }
  293.     }
  294.  
  295.     /**
  296.      * O(nlog(n)) and o(n^2) Quick Sort.
  297.      */
  298.     void quickSort(int left, int right) {
  299.         int i = left, j = right;
  300.         TYPE temp;
  301.         TYPE pivot = get(j / 2);
  302.  
  303.         while(i <= j) {
  304.             while(get(i) < pivot) {
  305.                 i++;
  306.             }
  307.             while(get(j) > pivot) {
  308.                 j--;
  309.             }
  310.             if(i <= j) {
  311.                 temp = get(i);
  312.                 set(i, get(j));
  313.                 set(j, temp);
  314.                 i++;
  315.                 j--;
  316.             }
  317.         }
  318.  
  319.         if(left < j) {
  320.             quickSort(left, j);
  321.         }
  322.         if(i < right) {
  323.             quickSort(i, right);
  324.         }
  325.     }
  326.  
  327.     /**
  328.      * Default Constructor.
  329.      */
  330.     List() : objects(nullptr), size(0) { }
  331.  
  332.     /**
  333.      * Parameterized Contructor.
  334.      *
  335.      * @param   size    the size of the list
  336.      */
  337.     List(int size) : objects(nullptr), size(size) {
  338.         setSize(size);
  339.     }
  340.  
  341.  
  342.     /**
  343.      * Deconstructor.
  344.      */
  345.     ~List() {
  346.         clear();
  347.     }
  348. };
  349.  
  350. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement