Reading notes-Introduction to Algorithms (Part II sequencing and sequential statistics)

Source: Internet
Author: User
Tags array length

structure of the input dataIn practice, the number to be sorted is rarely an isolated value, which is usually part of a dataset called a record. Each record has a keyword key, which is the value to be sorted. Other recorded data are known as satellite data, i.e. they are usually transmitted as key-centric. In a sorted algorithm, the satellite data must also be exchanged when the keyword is exchanged. If the records are large, we can exchange a set of pointers to individual records rather than the record itself, in order to minimize the amount of data movement. In a sense, formal implementation details make a complete program different from the algorithm. Whether we want to sort a single value or a large record that contains data, they are the same for the sort method. Because, in order to focus on the sorting problem, we generally assume that the input is composed only of numeric values. It is straightforward to convert the sorting algorithm of numbers to a program that sorts records. Of course, in the specific engineering conditions, the actual program design may also encounter other elusive challenges.   Why study sequencing? Many computer scientists believe that the sorting algorithm is the most basic problem in algorithm learning. There are several reasons for this:
    1. Sometimes the application itself needs to sort the information. e.g. customer account, Bank's check number
    2. Many algorithms usually take the sort as a key subroutine.
    3. Now there are a lot of sorting algorithms, and they employ a variety of techniques. In fact, many of the important techniques used in algorithmic design have long been used in sequencing algorithms that have evolved over many years. So, sorting is a historic issue.
    4. Many engineering problems are surfaced when sorting algorithms are implemented. For a particular scenario, the quickest sorting algorithm may be related to a number of factors, such as the prior knowledge of the keyword value and satellite data, the host memory hierarchy (cache and virtual storage), and the software environment.
Sorting Algorithms
    1. Insert sort: The worst case run time is (n^2), but its algorithm is tight within the loop, and when it comes to small-scale input, a fast in-place sorting algorithm.
    2. Merge sort: Has a good progressive run time (NLGN), but the merge program is not in-situ operation.
    3. Heap Sort: The n number can be sorted in place in O (NLGN) time. This algorithm uses an important data structure called a heap, and uses it to implement a priority queue.
    4. Quick sort: In-situ sorting algorithm, but the worst-case run time is n^2, the average running time is (NLGN), in practice is often better than the heap sorting algorithm. This is a very common algorithm for sorting large input arrays.
      1. 1-4 is the comparison sort (comparison sort): In order to study the performance limit of the sorting algorithm, the decision tree model is used to prove that the lower bound of the worst-case run time of any n-input comparison sorting algorithm is NLGN, which shows that the heap ordering and merging sort are the asymptotic optimal comparison sort algorithms.
  Sixth chapter Heap sorting  The Heap sort combines the merge sort and insertion sort advantages: Run time is NLG (n) and in place.   6.1 Stacks  (binary) The heap data structure is an array object that can be treated as a complete binary tree. Each node in the tree corresponds to the element that holds the node value in the array. Each layer of the tree is filled and the last layer may be excluded (the last layer is filled out from the Zuozi of a node) to indicate that the heap array A is an object with two attributes: the number of elements in the length[a]--array, the number of elements in the heap that heap-size[a]--stored in a tree, Given the subscript I of a node, there is: PARENT (i) return [I/2]left (i) return 2iRIGHT (i) return 2i+1 on most computers, the left process can calculate 2i in one instruction by the Binary tab of I Left 1 bits. The right process can quickly calculate the 2i+1 by moving the binary representation of I to the left by 1 bits and adding 1 to the low. The parent process can be i/2 by moving the I right by 1 bits. In the implementation of a good heap sort, these three processes are usually implemented using the "macro" process or the "inline" process. There are two types of two fork heaps: the maximum heap and the minimum heap (Keng Gen). In both of these heaps, the values in the nodes meet the heap characteristics, and the details depend on the type of heap. Maximum heap: The largest element in the heap is stored in the root node. The smallest heap is the reverse. In the heap sorting algorithm, we are using the largest heap. The minimum heap is typically used when constructing a priority queue.
    1. Max-heapify
      1. -Operating time O (LGN) is the key to maintaining maximum heap properties
    2. Build-max-heap
      1. -linear time running, can construct the maximum heap on the basis of unordered input array
    3. Heapsort
      1. -Run time O (NLGN), sort an array in situ
    4. Max-heap-insert,heap-extract-max, Heap-increase-key, heap-maximum the run time of the process is O (LGN), allowing the heap structure to be used as a priority queue
  6.2 Preserving the nature of the heapMax-heapify: Input as an array A and subscript I, when max-heapify is called, we assume that the two binary trees with left (i) and right (i) are the maximum heap, but then a[i] may be smaller than their children, which violates the nature of the maximum heap.    Max-heapify (A, i) {var L = left (i);    var r = Right (i);    var largest = i;    If L <= Heap-size[a] and a[l] > a[i] then largest = l;    If R <= Heap-size[a] and A[r] > a[largest] then largest = r; If largest! = I then swap (A[i], a[largest]) max-heapify (A, largest)} run Time is O (LGN), or max-heapify acts on a node with a height of h The required run time is O (h) 6.3 Building a heap  We can use Max-heapify to turn an array A[1..N] into a maximum heap from the bottom up.    Build-max-heap (A) {heap-size[a] = a.length; for (int i = A.LENGTH/2; I >= 1; i) {max-heapify (A, I)}} Ideally, we call max-heapify each time O (LGN), a total of O (n) calls, so run The time is O (NLGN), although the world is right, but from the asymptotic sense is not tight enough. In fact, we can get a more compact boundary, because the time to run max-heapify at nodes at different heights in the tree is different, and most nodes have a smaller height. -O (N) 6.4 Heap Sorting algorithmAt the beginning, the heap sorting algorithm first uses BUILD-MAX-HEAP to construct the input array into a maximum heap, because the largest element in the array is at root a[1], it can be swapped with a[n to achieve the final correct position. Now, if you remove the node n (by reducing heap-size[a]) from the heap, you can easily build the largest heap of a[1..n-1].        Heapsort (a) {build-max-heap (a) for (int i = a.length; i > 2; i--) {swap (a[1], a[i]);        Heap-size[a] = heap-size[a]-1;    Max-heapify (a,1); }} The time cost of the heapsort process is O (NLGN). 6.5 Priority Queue  Although the heap sorting algorithm is a very nice algorithm, in practice, a good implementation of fast sequencing is often better than heap sorting. A priority queue is a data structure that maintains a set of sets of elements, each of which has a keyword key. A maximum priority queue supports the following operations:
    1. INSERT (s,x)
    2. MAXIMUM (S)
    3. Extract-max (S)
    4. Increase-key (S,x,k)
  One application of the maximum priority queue is to schedule jobs on a single time-sharing computer. This queue records the jobs to be performed and the relative precedence between them. When a job is finished or interrupted, use the Extract-max operation to select the job with the highest priority from all the waiting jobs. At any time, a new job can be added to the queue with insert. The   priority queue can be implemented in heaps. In a given simulation application, such as job scheduling or event-driven, the elements of the priority queue correspond to the objects in the application. In general, we need to determine the application object corresponding to the elements in a given queue, and vice versa. When a heap is used to implement a priority queue, the handle of the corresponding Application object needs to be stored in each element of the heap.  heap-maximum with O (1) Time for MAXIMUM operation: Heap-maximum (A) {    return a[1];}  heap-extract-max with O (LGN) Run time: (assigns the last node to the first, the array length minus one, runs the max-heapify) Heap-extract-max (a) {    if heap-size [A] < 1        then error  "Heap underflow";    max = a[1];    A[1] = a[heap-size[a]];    Heap-size[a] = Heap-size[a]-1;    max-heapify (A, 1);    return MAX;}  heap-increase-key run time O (LGN):(on the path from this node to the root node, and constantly compared to their parents, if the key word of this element is larger, then exchange their keywords and continue to move, if less than their parents, then the maximum heap nature is established) Heap-increase-key (A,i,key) {    If KEY < a[i]        then error  "New KEY is Smal Ler than current key ";    a[i] = key    While I > 1 and a[parent (i)] < a[i]        swap (A[i], a[parent (i)])     &nbs P   i = PARENT (i)} max-heap-insert to implement an insert operation, you can extend the maximum heap by adding a leaf node with a key value of negative infinity. Then call Heap-increase-key to set the correct value for the new node's keyword and maintain the maximum heap nature. Run time O (LGN) Max-heap-insert (a,key) {    Heap-size[a] = Heap-size[a] + 1;    A[heap-size[a]] = 9999999 999999999;    Heap-increase-key (A, Heap-size[a], KEY);}   7th Chapter Quick SortAlthough the worst run time is O (n^2), fast sorting is often the best practical choice for sorting, because its average performance is good: O (NLGN) is expected to run, and it can be sorted in place and works well in the need environment.    QUICKSORT (A, p, r) {if p > r then q = PARTITION (A, p, R);    QUICKSORT (A, p, q-1); QUICKSORT (A, q+1, R);}    PARTITION (A, p, r) {x = A[r];    i = p-1;            for (var j = p; j < R-1; J + +) {if (A[j] <= x) {i++;        Swap (A[i], a[j]);    }} Swap (A[i + 1], a[r]); return i + 1;}   8th Chapter Linear time Sequencing  The counting sort, cardinal sort and bucket sort are introduced. These algorithms use some non-comparison operations to determine the sort order. Therefore, Nether O (NLGN) is not applicable to them.

Reading notes-Introduction to Algorithms (Part II sequencing and sequential statistics)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.