Introduction to Chapter VII of the data structure of the sort

First, the concept

Data sorting: sorting a group of objects is in the order of the rearranged predetermined process, is often sorted search service.

Stable sort: if sorted, records with the same original keyword maintain their relative order, this method of sorting is called stable sort; stability characteristics sorting method itself, regardless of the data.

Sort Type:

  • Internal sequencing: All data are stored in memory;
  • External sort: the need for external memory access ordering process

Internal sorting points according to the method

  • Insertion Sort
  • Sort exchange
  • Selection Sort
  • Merge sort

Sort index (sort algorithm performance analysis):

  • storage
  • The number of comparisons

Second, insertion sort

Insertion sorting methods used direct insertion sort , binary insertion sort , the table insertion sort and Shell sort .

Direct insertion sort (Straight Insertion Sorting) is a simple sorting method, the basic idea is that each record in turn has been inserted into one row of table sorted in order to thus obtain a new, increased number of records 1 the ordered list. Direct insertion sort process similar libraries organize books.

// R & lt be sorted table n is the length of the table 
void StraightlnsertSort (List R & lt, int n) { 
     int I, J; 
     for (I = 2 ; I <= n; I ++ ) { 
        R & lt [ 0 ] = R & lt [I]; // record the i-th copy of sentry 
        J = I- . 1 ;
         the while (R & lt [ 0 ] .key < R & lt [J] .key) {// compared with sentry, sentry key until the key value is not larger than 
            R & lt [J + . 1 ] = R & lt [j]; // the records assigned to the j-th j + 1-th recording 
            j - ; 
        } 
        R & lt [j + . 1 ] R & lt = [ 0 ]; // record the i-th sequence is inserted into in 
    } 
}

Record R [0] has two functions, one is to enter the lookup cycles before, it saves R [i] value so as not shifted due to the lost contents recorded R [i] in; the other is from sentry effect, "monitoring" array subscript variable j is out of range in the while loop, once the bounds (i.e. j <1), R [0] while the end of the automatic control loop, thus avoiding the need to be monitored in each cycle while j whether cross-border. The application of this technique, so that the conditions of the test cycle time reduced by approximately half.

Algorithm Analysis : direct insertion sort algorithm is simple, easy to understand, easy to implement, time complexity is O (n2), if a large number of records to be sorted, generally do not use direct insertion sort. From the spatial point of view, it requires a record of the auxiliary space, i.e., the spatial complexity is O (1). Direct insertion sort is stable.

● storage space n + 1; (1 is the additional space), space complexity O (. 1)
● time complexity of O (n 2), if the table is given in order to be sorted, the best time complexity of O ( n-)
● stability: stable sort;

Third, exchange sort

The basic idea of the exchange sort: Comparison of two key size of the recording, if the recording size of the two keys appear in reverse order, the exchange of these two records, so that the sequence of records with smaller keys is moved to the front portion, the key larger toward the rear of the recording sequence.

Sort exchange include: bubble sort and quick sort.

1, bubble sort:

The basic idea: Through repeated comparison, achieved the sort Swap adjacent recording; the effect of each trip are the largest current key change to the last record

When the algorithm, define an integer variable endsort, before each sorting, first it is set to 0, if the exchange of the recorded trip bubbling, then it is set to 1. At the end of a cycle, we check endsort, if endsort a value of 0 will terminate the algorithm.

// R & lt n to be sorted table is a table long 
void the BubbleSort (R & lt List, int n) {
     / * endsort: flag files have been sorted * / 
    for (I = . 1 ; I <= N- . 1 ; I ++ ) { 
        endsort = 0 ; / * If the cycle is not recorded in exchange, then the sequence has been ordered * / 
        for (J = . 1 ; J <= Ni- . 1 ; J ++ ) {
             IF (R & lt [J] .key> R & lt [J + . 1 ] .key) { 
                TEMP = R & lt [J]; 
                R & lt [J] = R & lt [J + . 1 ]; 
                R & lt [J + . 1]=temp;
                endsort=1;
            }
        }
        if(endsort==0) break;
    }
}
  • Time complexity: O (n2), if the table is sorted O (n)
  • Space complexity: O (1)
  • Stability: stable sort.

2, quicksort (Quick Sorting) is a sort of exchange, is essentially an improvement of the bubble sort; first retrieve the first record, it will be compared with the remaining records in the table and exchange, so that it is placed the correct final position of the recording, the recording sheet is divided into two portions one of {(left) such that it is less than the recorded key; the other key record Zhu (right) were greater than its}; then the two parts re-executing the above process, and so on, until a sorted

 

Analysis of Algorithms:

  • ● Space: n + log 2 n; (log 2 n is the additional space - Stack)
  • ● Time: O (nlog 2 n) Note: If the initial order or substantially ordered record table, the quick sort would degenerate into bubble sort, the time complexity is O (n 2); i.e.: Quick Sort table base when ordered, the most negative effect on their play efficiency.
  • ● Stability: Unstable sorting

 Fourth, select Sort

Selection Sort (Selection Sorting) The basic idea: In every time n-i + 1 (i = 1,2, ..., n-1) recording the minimum recording key selected as the i-th recording ordered sequence.

Category: Direct Select sort and heap sort

 1. Direct Selection Sort

Direct selection sorting algorithm is the basic idea: the i-th selection operation by comparison between ni secondary key, the key is selected from the minimum recording in the recording ni + 1, and the i-th and (1 ≦ i ≦ nl) exchange records.

void SelectSort (R & lt List, int n-) { 
     int min, I, J;
      // every cycle, a minimum key value is selected 
    for (I = . 1 ; I <= N- . 1 ; I ++ ) { 
        min = I; / / assuming the i-th smallest key records 
        for (i = J + . 1 ; J <= n-; J ++ ) {
             IF (R & lt [J] .key < R & lt [min] .key) { 
                min = J; // record button smaller value recorded subscript 
            } 
             IF (min =! i) { 
                the swap (R & lt [min], R & lt [i]); // the minimum key value and record the i-th recording switching exchange 
            } 
        }
    }
}

Analysis of Algorithms:

  • ● Space: n + 1; (1 is the additional space)
  • ● Time: O (n 2), the case is not suitable for large n
  • ● Stability: Unstable sorting

2, heapsort

Construction stack (screening method):
1) Method:
set a recording {R 1, R 2, ... , R n.}
Input (1) sequentially into a complete binary tree (an array storage)
(2) starting from the last one parent, if younger children, it is in that direction left or right of small children under the screen, until no longer sieve;
(3) successive processed each parent.

Heap sort, for n average time required for the recording order is O (nlog2n). In the worst case, its time complexity is also O (nlog2n). Relative to the quick sort, this is the biggest advantage of heap sort. Further, heap sort only a record size for exchange with an auxiliary storage. Heap sort is unstable.

Fifth, merge sort 

Merge sort (Merge Sorting) is inserted sorting, exchange sorting, selecting a different sort class sorting method, except that the column is required to be sorted by a number of ordered sub-sequence. The meaning is to merge two or more tables ordered merged into a new sorted list. The combined method of the first key-value comparing the recorded sequence, the smallest one is the first key record in the sorted sequence. Remove the record key comparison continues each sequence prior the first record, the second record can identify the row shore. So go on, ultimately to rank. So merge sort of the basis of the merger.

classification:

 

  • The combined ordered sequence
  • Way merge sort

 The time complexity of merge sorting algorithm is O (nlog2n), to be ruled out due to use and the number of records to store the result array b, merge sort is achieved requiring additional storage overhead twice. Way merge sort is stable. When n is large, the performance is better than merge sort time heap sort, but its large amount of auxiliary storage required. O (n)

 

 

Guess you like

Origin www.cnblogs.com/jalja365/p/12616537.html