Home > Back-end >  Why I can't bubble row preface
Why I can't bubble row preface

Time:10-12

# include & lt; iostream>
# include
using namespace std;

Typedef int Rank;//rank
# define DEFAULT_CAPACITY 10//the default initial capacity (can be set up in the actual application for bigger)

The template & lt; Typename T> A class template Vector {//Vector class
Protected:
Rank _size; Int _capacity. T * _elem;//size, capacity, data area
A void copyFrom (T const * and Rank lo, Rank the hi);//copy array interval A [lo, hi)
Void expand ();//space expansion
Void the shrink ();//loading factor hours compression

Max Rank (Rank lo, Rank the hi);//select the largest element
Void selectionSort (Rank lo, Rank the hi);//selection sort algorithm
Void the merge (Rank lo, Rank mi, Rank the hi);//merge algorithm
Void mergeSort (Rank lo, Rank the hi);//merge sort algorithm
Void heapSort (Rank lo, Rank the hi);//heap (later combined heap on completely)
Rank partition (Rank lo, Rank the hi);//pivot construction algorithm
Void quickSort (Rank lo, Rank the hi);//quick sort algorithm
Void shellSort (Rank lo, Rank the hi);//hill sorting algorithm
Public:
//the constructor
Vector (c=DEFAULT_CAPACITY int, int s=0, T v=0)//capacity of c, size of s, all elements of the initial v
{_elem=new T [_capacity=c]; For (_size=0; _size & lt; s; _elem [_size + +]=v); }//s<=c
A Vector (T const * and Rank n) {copyFrom (A, 0, n); }//array overall copy
A Vector (T const * and Rank lo, Rank the hi) {copyFrom (A, lo, hi); }//interval
The Vector (Vector Const& V) {copyFrom (v. _elem, 0, v. _size); }//vector integral copy
The Vector (Vector Const& V, Rank lo, Rank hi) {copyFrom (v. _elem, lo and hi); }//interval
//destructors
~ the Vector () {delete [] _elem; }//release internal space
//read-only access interface
Rank the size () const {return _size; }//size
Const bool empty () {return! _size; }//to empty
Rank the find (T const& E) const {return find (e, 0, _size); }//disordered vector integral find
Rank the find (T const& E, Rank lo, Rank the hi) const;//disordered vector interval for
Rank search (T const& E) const//order vector integral find
{return (0 & gt; _size=)? 1: search (e, 0, _size); }
Rank search (T const& E, Rank lo, Rank the hi) const;//order vector interval for
//write access interface
T& Operator [] (Rank r);//overloaded subscript operator, each element can be similar to an array reference
Const T& Operator [] (Rank r) const;//is limited to do the right value of the overloaded versions
Vector & Operator=(Vector Const&);//overloaded assignment operator, in order to direct the cloning vector
T remove (Rank r);//delete rank r the element
Int the remove (Rank lo, Rank the hi);//delete) rank in the interval [lo, hi elements within
Rank the insert (Rank r, T const& E);//insert element
Rank the insert (T const& E) {return insert (_size, e); }//the default as an element is inserted at the end of the
Void sort (Rank lo, Rank the hi);//to sort [lo, hi)
Void the sort () {sort (0, _size); }//integral order
Void unsort (Rank lo, Rank the hi);//to) [lo, hi scrambling
Void unsort () {unsort (0, _size); }//whole scrambling
Int deduplicate ();//disorderly to heavy
Int uniquify ();//in order to heavy
Void the bubbleSort (Rank lo, Rank the hi);//blister sorting algorithm
Bool mercifully (Rank lo, Rank the hi);//scanning exchange
//traversal
Void traverse by (void (*) (T&) );//traversal (using function pointer, read-only or local)
The template & lt; Typename VST> Void traverse by (VST&);//traversal (using the function object, global changes)
};//Vector


The template & lt; Typename T> Void Vector : : expand () {//vector space expansion
If (_size & lt; _capacity) return;//not full, without expansion
If (_capacity & lt; DEFAULT_CAPACITY) _capacity=DEFAULT_CAPACITY;//is not lower than the minimum capacity
T * oldElem=_elem; _elem=new T [_capacity & lt; & lt;=1];//double capacity
for (int i=0; I & lt; _size; I++)
_elem [I]=oldElem [I];//copy the original content of the vector (T as the basic types, or already overloaded assignment operator '=')
The delete [] oldElem;//release the original space
}
The template & lt; Typename T> Void Vector : : the shrink () {//loading factor after hours of compressed vector space
If (_capacity & lt; DEFAULT_CAPACITY & lt; <1) return;//not shrink below DEFAULT_CAPACITY
If (_size & lt; <2 & gt; _capacity) return;//by a 25%
T * oldElem=_elem; _elem=new T [_capacity & gt; & gt;=1];//capacity by half
for (int i=0; I & lt; _size; I++) _elem [I]=oldElem [I];//copy the original vector content
The delete [] oldElem;//release the original space
}
The template & lt; Typename T>
Void Vector ) : the bubbleSort (Rank lo, Rank hi {
while (! Mercifully, lo, the hi -));
}
Template Bool Vector : : mercifully (Rank lo, Rank hi) {
Bool sorted=true;
While (+ + lo & lt; Hi) {
If (_elem] [lo - 1 & gt; _elem [lo]) {
Sorted=false;
Swap (_elem [] lo - 1, _elem (lo));
}
}
return sorted;
}
The template & lt; Typename T> Vector & Vector : : operator=(Vector Const& V) {//deep copy
If (_elem) delete [] _elem;//release the original content
CopyFrom (v. _elem, 0, v. considering ());//copy whole
Return * this;//returns the current object reference, so that the chain assignment
}
The template & lt; Typename T> T & amp; Vector : : operator [] (Rank r)//overloaded subscript operator
{return _elem [r]. }//assert: 0 & lt;=r & lt; _size

The template & lt; Typename T> Const T & amp; Vector : : operator [] (Rank r) const//only do right value
{return _elem [r]. }//assert: 0 & lt;=r & lt; _size
The template & lt; Typename T>//element type
Void Vector : : copyFrom (T const * A, Rank lo, Rank the hi) {//to A array interval [lo, hi) based on vector replication
[_capacity _elem=new T=2 * (hi - lo)]; _size=0;//allocate space, size zero
While (lo & lt; Hi)//A) [lo, hi the elements within each
_elem=[_size + +] A [lo++];//copy to _elem [0, hi - lo)
}

Int main () {
Vector A;

for (int i=0; I & lt; 10; I++) {
A [I]=rand ();
}
A. ubbleSort (0, A.s considering () - 1);
for (int i=0; I & lt; 10; I++) {
cout
  • Related