Original link: https://yousazoe.top/archives/8c47f151.html
introduction
The purpose of this course is to focus on the design and implementation of various data structures, revealing the laws, principles and methods, and at the same time, aiming at algorithm design and performance analysis, to enable students to understand and master the main routines and methods. This article will explain the data structure vector and finding and sorting.
abstract data type
interface and implementation
Vectors belong to the most basic linear structure, which we generally call linear sequences.
In this chapter we will demonstrate and discuss two issues around this data structure:
- How to customize and implement a data structure according to a unified interface specification
- Around this data structure, it shows how to use more efficient algorithms to make our external interfaces work more efficiently: search, sort
First we have to distinguish abstract data types and data structures:
- abstract data type = data model + a set of operations defined on the model
- Data structure = a set of algorithms that implement ADT based on a specific language
More vividly, we can compare the data structure to a certain product such as a car. As a user Application, he only cares about the functions that the external features of this product can provide; the implementer Implementation needs to be responsible for these functions and how the features are implemented.
Vector ADT
The so-called vector is actually a generalization and generalization of the data organization form of array in high-level programming languages such as C++.
rank access
The so-called array in these high-level programming languages is actually a continuous memory space, which is evenly divided into several units, and each unit will respond to each other with a number and can be accessed directly.
The vector can be considered as the abstraction and generalization of the array, which is also encapsulated by a set of abstract elements in the linear order just now. The difference is that the original access method through the subscript i
becomes the rank rank.
In addition, the type of elements in the vector has been expanded, not limited to a specific basic type, and all its operations, management and maintenance are more simplified, and can be completed through a unified interface.
Vector ADT Interface
Various operations can be performed on vectors through these operation interfaces, and vector operations can only be performed through these operation interfaces.
Interface operation example
Construction and Destruction
# ifndef SRC_VECTOR_H |
The whole Vector is encapsulated, and the operation interface interface from various user applications is provided outside, which is equivalent to the instruction manual of a Vector structure.
/* Constructor*/ |
copy
template < typename T> |
The copy operation doubles the _elem
space, and then copies the interval elements in sequence.
scalable vector
scalable vector
Now we use _size
for actual size and _capacity
for total capacity.
T* _elem; // data |
The problem here is that once _capacity
is determined it will be immutable in the current scheme, and such a strategy obviously has obvious shortcomings. This deficiency is reflected in two aspects:
- Overflow (overflow):
_elem[]
is not enough to hold all elements, although the system still has enough space at this time - Underflow: few elements in
_elem[]
, fill factor = _size/_capacity << 50%
Dynamic space management
Appropriately expand the internal array capacity when an overflow is imminent.
Incremental expansion
Double expansion
unordered vector
ordered vector
unique
binary search
Fib lookup
interpolated lookup
bubble sort
merge sort
bitmap
This article is reproduced from: https://yousazoe.top/archives/8c47f151.html
This site is for inclusion only, and the copyright belongs to the original author.