Data structure-8.4 sort

Preface-Data Structure

The data structure needs to be chewed repeatedly, and the answers to the problems encountered in the development can be obtained at any time.

Simple selection sort (similar to bubble sort)

  • Basic idea
  • Select the record with the smallest key from each record with sorting, and place it at the end of the sorted record sequence in order until the sequence is all in order.
  • Algorithm steps
  • From all n records, select the record with the smallest key value and exchange bit Y with the first record.
  • From all n-i+1 records, select the record with the smallest key value and exchange positions with the i-th record.
  • i take 2, 3,… n-1, repeat ②.
  • For example, given n 8, the sort code of the 8 elements in the array R is: (8, 3, 2, 1, 7, 4, 6, 5), then the direct selection and sorting process is shown in the figure below.
    Insert picture description here
  • Performance analysis
  • Consider the number of comparisons of the key code and the number of data object movements. The number of comparisons has nothing to do with the initial arrangement of data objects. The total number of times is n*(n 1)/2. The number of movements is related to the initial arrangement of data objects. The minimum is 0 and the maximum is 3(n-1), so the time complexity of simple selection sorting is 0(n2).
  • The space complexity of simple selection sort is 0 (1).
  • Simple selection sort is an unstable sorting method.

Simple selection sort

  • Basic idea The
    tree selection and sorting method is a method of selecting and sorting according to the idea of ​​the tournament.
  • Algorithm steps
  • Compare the keywords of all n records in pairs, take out [n/2] smaller keyword records, as the comparison result of the first step, and then compare the keywords of these [n/2] records in pairs ,. Repeat this until the smallest keyword record is found. In fact, this creates a complete binary tree.
  • Select the next smallest keyword record. That is, first change the key value of the record with the smallest key obtained last time in the leaf node to ∞, and then modify the key value of the record represented by all nodes on the path from the leaf node to the root node.
  • Repeat ② until all n records to be sorted are taken out.
  • example
    Insert picture description here
  • Performance analysis
  • Tree selection sorting is like operating on a complete binary tree, so the number of key code comparisons and the number of data object movement times will not exceed log2n, so the time complexity of tree selection sorting is 0 (nlog2n).
  • Tree selection and sorting requires a lot of space. The space except for the leaf nodes on the complete fork tree is all additional space, so its space complexity is 0(n).
  • Tree selection sorting is a stable sorting method.

Heap sort (important)

  • definition

  • Heap sorting is actually a modification of tree sorting. It overcomes the huge additional space required by tree sorting. A sequence of n elements H={k, k2,. Kn,}, for all i=1, 2... Ln/2], when it satisfies the following relationship:
    Insert picture description here

  • All roots are bigger than children

  • All roots are smaller than children
    Insert picture description here

  • The nature of heap According to the definition of heap, heap is a completely binary tree.

  • The root node of the complete binary tree corresponding to the heap is the smallest or largest element in the sequence of elements.

  • The sequence of elements on the path from the root node to each leaf node increases or decreases according to the element value.

  • The heap can be stored in a one-dimensional array.
    Insert picture description here

  • Basic idea

  • Algorithm steps

  • For the keywords of the group records to be sorted, build a minimum heap according to the definition of heap:

  • Output the record with the smallest key value:

  • For the remaining records to be sorted, repeat the first and second steps until all the records are arranged.

  • In the above steps, there are two key issues:

    • How to form an initial heap for a given sorted record?
    • After outputting the record with the smallest key value, how to organize the remaining records into a new heap?
  • Filtering method adjustment heap: select the larger keyword from r[2s] and r[2s+1], assuming that the keyword of r[2s] is larger, compare the keywords of r[s] and r[2s]

    • If r[s]. key>=r[2s]. key, it means that the subtree rooted at r[s] and r[2s] is already a heap and no adjustment is necessary
    • If r[s]. key<r[2s]. key. After the exchange, the subtree rooted at r[2s+1] is still a heap. If the subtree rooted at r[2s] is not a heap, repeat the above process and adjust the subtree rooted at r[2s] to Pile until the leaves are knotted.
  • Build the initial reactor:

    • For the unordered sequence r[1. .n], starting from i=n/2, the filtering method is repeatedly called, and the roots are r[i], r[i-1],.. ., r[1] in turn The sub-pile of is adjusted to a pile.
  • Example 1
    Insert picture description here

  • Example 2
    Insert picture description here

  • Example 3 Heap Sorting After sorting codes 46, 55, 13, 42, 94, 05, 17, 70, after building a large root heap as shown in a better picture, the heap sorting process
    Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

  • It can be seen from the above process that if the result is output in the form of a complete binary tree, the result is: 05, 13, 17, 42, 46, 55, 70, 94, which is the result of the heap sort.
  • Performance analysis
  • Heap sorting is like operating on a complete binary tree, so the number of key code comparisons and the number of data object moves will not exceed log2n, so the time complexity of heap sorting is O(nlog2n).
  • The additional space required by heap sort is 1, so its space complexity is 0 (1).
  • Heap sorting is an unstable sorting method.

Guess you like

Origin blog.csdn.net/weixin_41732253/article/details/109892136