Guest User

Untitled

a guest
May 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. template <typename Type>
  2. class NeatArray
  3. {
  4. public:
  5. //constructor
  6. // if no values are passed the
  7. // default size should be 200
  8. // default length should be 0
  9. NeatArray(int size = 200, int length = 0)
  10. {
  11. if(size <= 0)
  12. _size = 200;
  13. else
  14. _size = size;
  15.  
  16. _array = new Type[_size];
  17. _length = length;
  18. };
  19. // Destructor
  20. ~NeatArray()
  21. {
  22. delete [] _array;
  23. };
  24. //###############################################
  25. // insert an element
  26. //
  27. void insert(Type element)
  28. {
  29. if(!full())
  30. {
  31. _array[_length] = element;
  32. _length++;
  33. }
  34. else
  35. {
  36. Type* _new_array;
  37. int _new_size;
  38. _new_size = _size + 10000;
  39. _new_array = new Type[_new_size];
  40. for (int i = 0; i < _length; i++)
  41. _new_array[i] = _array[i];
  42. _size = _new_size;
  43. delete [] _array;
  44. _array = _new_array;
  45. }
  46. };
  47. //###############################################
  48. // return size of array
  49. //
  50. int size()
  51. {
  52. return(_length);
  53. }
  54. //###############################################
  55. // is this baby full ?
  56. //
  57. bool full()
  58. {
  59. return(_length == _size);
  60. }
  61. //###############################################
  62. // is this less
  63. //
  64. bool less(Type A, Type B)
  65. {
  66. return(A < B);
  67. }
  68. //###############################################
  69. // operator
  70. //
  71. //bool operator==(const Type &A, const Type &B)
  72. //{ return !less(A, B) && !less(B, A); }
  73. //###############################################
  74. // operator
  75. //
  76. Type& operator[](int idx)
  77. { return _array[idx]; }
  78. const Type& operator[](int idx) const
  79. { return _array[idx]; }
  80. //###############################################
  81. // swap
  82. //
  83. void swap(Type array[], int first, int second)
  84. {
  85. Type tmp;
  86. tmp = array[first];
  87. array[first] = array[second];
  88. array[second] = tmp;
  89. }// end swap
  90. //###############################################
  91. // partition
  92. //
  93. int partition( Type array[], int first, int last)
  94. {
  95. Type pivot;
  96. int ix;
  97. int sm_ix;
  98. swap( array, first, (first+last)/2 );
  99. pivot = array[first];
  100. sm_ix = first;
  101.  
  102. for( ix = first+1; ix <= last; ix++ )
  103. if( array[ix] < pivot)
  104. {
  105. sm_ix++;
  106. swap( array,sm_ix,ix );
  107. }
  108. swap( array,first,sm_ix );
  109. return sm_ix;
  110. }// end partition
  111. //###############################################
  112. // recursive quick sort
  113. //
  114. void rec_quick_sort(Type array[], int first, int last)
  115. {
  116. int pivot;
  117. if(first < last)
  118. {
  119. pivot = partition(array, first, last);
  120. rec_quick_sort(array, first, pivot-1);
  121. rec_quick_sort(array, pivot+1, last);
  122. }
  123. }
  124. //###############################################
  125. // quick sort
  126. //
  127. void quick_sort()
  128. {
  129. rec_quick_sort( _array, 0 , _length-1);
  130. }
  131. //###############################################
  132. // sorting for the obese
  133. //
  134. void obese_sort()
  135. {
  136. NeatArray<pointer<Type> > p_array(_size,_length);
  137. int i;
  138. int j;
  139. int nx_j;
  140. //p_array._length = _length;
  141. // copy the contents of the calling array into a temp array of pointers to the objects.
  142. for( int i = 0; i < _length; i++ )
  143. p_array[i] = &_array[i];
  144. // sort the array of pointers
  145. //p_array.quick_sort();
  146. rec_quick_sort(*p_array._array,0,p_array._length-1);
  147. // now we will move around all of the pointers
  148. for( int i = 0; i < _length; i++ )
  149. {
  150. if( p_array[i] != &_array[i])
  151. {
  152. Type tmp = _array[i];
  153. for( int j = i; p_array[j] != &_array[i]; j = nx_j )
  154. {
  155. nx_j = p_array[j] - &_array[0];
  156. _array[j] = *p_array[j];
  157. p_array[j] = &_array[j];
  158. }//end for j
  159. _array[j] = tmp;
  160. p_array[j] = &_array[j];
  161. }//end if
  162. }// end for i
  163. }// end obese_sort
  164. //private:
  165. Type *_array;
  166. int _length;
  167. int _size;
  168. };
Add Comment
Please, Sign In to add comment