3 minutes speed read the original, "the Java data structure and algorithm" (II)

Chapter Three simple sorting

1. Simple kind of sort of

  • 1.1 bubble sort: the algorithm runs very slowly, in short, every two elements are required to perform a comparison, and ultimately the outcome.
  • 1.2 Select Sort: Select the sorting is to each number and a fixed value which is compared, large side, while small, can be understood as get a fixed minimum, all the values ​​and the values ​​are compared, complete final discharge order
  • 1.3 insertion sort: local conditions must be orderly, bubble sort and selection sort are not present among the local order, insertion sort is simply used as a marker which will, to be marked this element into the local among the ordered queue, thus continuously rotating the corresponding marker elements, thereby completing the sorting all
  • 1.4 Object Sort: according to a property of the object to sort them
  • 1.5 sort words: alphabetically sorted according to the alphabetical order of the alphabet

2. A simple comparison between the sort

  • Useful bubble sort is not used, the small amount of data time generally 2.1
  • 2.2 Selection Sort Although it minimizes the number of exchanges, the word book is still large, the time when the small amount of data, and exchange data with respect to the case of comparative data is more time-consuming, can be used to select the sort
  • 2.3 In most cases, it is assumed when a small amount of data, or when substantially ordered, insertion sort three simple sorting algorithm is the best choice

3. Summary

  • Sorting algorithm mentioned chapter 3.1 Jiading the array are stored as a data structure
  • 3.2 sorting keyword and includes data items in response to movement of the array comparison data item
  • Chapter 3.3 of the time responsible for all algorithms is O (n2), n represents the number of elements, O for complexity (see Big O notation)
  • 3.4 invariance means that the algorithm remains the same run-time conditions
  • 3.5 bubble sort algorithm is efficient algorithm for the worst, but the simplest
  • 3.6 If the data item has the same key, he sorted through their order remains the same, this is sort of expensive stable

Chapter IV stacks and queues

A stack

For example: often need to deal with in the post office message, the message will accumulate from the bottom up into a stack, is the way to deal with at this time to deal with the top of the message, in this way only able to calmly finished processing all the letters within a reasonable time this work will not have much trouble, otherwise the bottom of the letter you will never be able to be processed

1. The actual example of the stack

  • 1.1 Reverse word: it simply is based on LIFO stack features, each character of the word are pushed onto the stack space, and then one by one output, this time to form a backwards sort of effect
  • 1.2 delimiter matching: for example using stack space to parse the file is legitimate Java, braces or brackets each are required corresponding to the pair may each left parenthesis or brackets which press the stack whenever read to the next right parenthesis, or right brace pops up, no match is found, an error
  • 1.3 Stack efficiency: pushing and popping of data Stirring constant complexity O (1), means that the time consumed by stack operations does not depend on the number of data items in the stack, and therefore the operation time is very short stack. Oh and move operations need to compare

2. Queue

特性就是FIFO

3. priority queue

Priority queue way is to add keywords to determine in which each element into the team, making the priority queue internal sorting, making the entire queue to become an orderly queue, priority queue can ensure that important tasks first execution, but if you continue to insert high-priority task to the priority queue which could lead to opportunities for all low-priority queue never get executed

4. Summary

  • 4.1 Data structure of the stack, queue, a priority queue is often used to simplify some operations procedures
  • 4.2. These data structures, only one data item can be accessed
  • 4.3 stack allows access to the last data item insertion
  • 4.4 stack among the most important action is to insert a data item at the top of the stack, and to remove a data item from the top of the stack
  • 4.5 queue only allows access to the first data item inserted
  • 4.6 Important operation queue is inserted in the data bits and remove items in the first team
  • 4.7 queue may be implemented as a circular queue, which is based on an array, the array subscript array can be rewound from the position of the start end of the array
  • 4.8 priority queue only allows access to the minimum or the maximum data items

The fifth chapter lists

The list is like a train, each of its list object will refer to the next or previous list object, so the list compared to the advantage of the array is to add a list node or delete a node list will be faster than the array, the array is relatively based on the index will be faster than the query list, but if you do not know the index of linear search query, in fact, the essence of speed with the list and there is no difference

1. singly linked list

单向链表类似于栈,特点:
  • 1.1 can only be inserted in a data item list head
  • 1.2 can only delete a data item in the list head
  • 1.3 traversing the list to display its contents

2. doubly linked list

That is doubly linked list of objects which introduces a reference to the last node, a reference for operation can be the same as for the first node to the last node corresponding to, and can not find it in each list node among on a node, you can also find him next node

3. A list of efficiency

  • 3.1 fast insertion and deletion, only need to change the reference value, all it takes O (1) time
  • Another advantage of the array than 3.2 lists how much memory is required on how much memory you can use the memory array is generally at initialization time has been fixed, it will often occur due to the array is too large too inefficient or too small arrays resulting in the problem space overflow, so from this point of view, although later ArrayList automatic expansion address the deficiencies of fixed-size array initialization time, but also because of his excessive expansion without re-insert the data, it may lead to wasted space sex, compared with the characteristics of the list is the need to expand the number of how many, the number of possible expansion will be more, but the memory utilization will be higher

4. Ordered list

简单来说就是有顺序的链表结构
  • 4.1 Ordered list of efficiency

Insert or delete an item in an ordered list of which can take up to O (N) times compared to an average of N / 2 times, because of the need to go step by step to find the correct position along the chain, may be in the (1) in O find the minimum or deleted, because it is always in the header, if an application needs to access when the minimum value of frequent, fast and does not require the insertion of an ordered list that is a very effective program options, such as priority queues on can be achieved using an ordered list

The double-ended list

Doubly linked list to distinguish double-ended, double-ended linked list can be found on a node of the node, but only doubly linked list can be traversed simultaneously from both ends of the chain, it is not able to find a node in any node

6. Summary

  • 6.1 contains a list LinkedList objects and many objects Link
  • 6.2 LinkedList object contains a reference, the reference is usually called first, it points to the first node in the list
  • 6.3 Link Each object contains data and a reference, usually called next, which points to the next linked list node chain
  • 6.4 Next field is null mean the end of the list
  • 6.5 CCN inserted in the header fields need to be next new node to the chain of a chain of the original first node, and then let the first point to the new node
  • 6.6 In the header delete chain node should first point first.next
  • 6.7 In order to traverse the list, are using the list next to each field to find the next node
  • 6.8 can be found in the node has a particular value chain by traversing the linked list, once found, may be displayed, or delete operation node in the chain by other means
  • 6.9 The new chain nodes can be inserted in front of or behind a particular value chain node, the first node to traverse find this chain

Note: The quick inquiry ordered array, unordered array index query quick, quick add and delete list

  • 6.10 ordered list among CCN ascending or descending order according to the key value
  • 6.11 among the doubly linked list, each node contains a reference chain to its chain of nodes one by one, while there are references to the chain after a node
  • Reversed 6.12 doubly linked list traversal, and may be removed from the end of the table
  • 6.13 iterator is a reference, which is encapsulated in the object class, the references to the list associated with the nodes in the chain

Reproduced in: https: //www.jianshu.com/p/c1665a11bc3c

Guess you like

Origin blog.csdn.net/weixin_34324081/article/details/91103711