Data structures and algorithms (Interview)

Sorting Algorithm

 

Direct insertion sort : all the elements in the array in turn with the front row has been good compared to the elements, if the selected element is smaller than the ordered elements, the exchange until all elements have been compared

Hill sorting : Sort the array to be grouped by the step gap, then the elements of each group using the direct insertion method to sort the sort; halved each time gap is reduced, the above-described operation cycle; when 1 gap =, by direct insert, complete sequencing

Simply choose Sort : Compare + exchange

HEAPSORT : Building Big Top heap sort

Handwriting how a stack
 1 Insert a number heap [size ++] = X; up (size);
 2 . Qiu set minimum value from among the heap [ 1 ]
 3 . Remove the minimum heap [ 1 ] = heap [size]; size -; Down ( . 1 );
 . 4 delete any element of a heap [K] = heap [size]; size -. ; Down (K); up (K); 
 . 5 modify any element of the heap [k] =. x; down (k); up (k); 

heapsort 

int n-, m;
 int H [ 1000 ], CNT; 

void Down ( int U) 
{ 
    int T = U;
     IF (U * 2 <= CNT && H [ u *2 ] < h[ t ] ) t = u * 2;
    if( u * 2 + 1 <= cnt && h[ u * 2 + 1 ] < h[ t ] ) t = u * 2 + 1;
    if( u != t )
    {
        swap( h[ u ], h[ t ] );
        down( t );
    }
}

void up( int u )
{
    while( u / 2 && h[ u ] < h[ u / 2 ] )
    {
        swap( h[ u ], h[ u / 2 ] );
        u >> = 1;
    }
}
 
int main()
{
    cin >> n >> m;
    for( int i = 1; i <= n; i ++ ) cin >> h[ i ];
    cnt = n;
    
    for( int i = n / 2; i; i -- ) down( i );

    while( m -- )
    {
        cin >> h[ 1 ] << " ";
        h[ 1 ] = h[ cnt -- ];
        down( 1 );
    }    
    return 0;
}

 

Bubble sort :

1. The sequence around among the elements compared in turn, guarantee the right of the element is always greater than the elements of the left

2. Step 1 is performed again sequence elements among the n-1 remaining

3. For a sequence of length n, a total of n-1 need to perform comparative wheel

Quick Sort : Number of digging fill + Divide and Conquer

vector< int > quick_sort( vector<int> q, int l, int r )
{
    if( l >= r ) return;
    int i = l - 1;
    int j = r + 1;
    int x = q[l + r >> 1];
    while( i < j )
    {
          do i ++; while( q[ i ] < x );
          do j --; while( q[ j ] > x );
          if( i < j ) swap( q[ i ], q[ j ] );
    }
    quick_sort( q, l, j );
    quick_sort( q, j + 1, r );
}

 

Merge sort :

1. The sequence of each binary decomposition ---- split

---- 2. The combined sequence segments after a sort-merge divided twenty-two

vector< int > tmp;
void merge_sort( vector< int > q, int l, int r )
{
    if( l >= r ) return;
    auto mid = l + r >> 1;
    merge_sort( q, l, mid );
    merge_sort( q, mid + 1, r );
    int i = l;
    int j = mid + 1;
    int k = 0;
    while( i <= j )
    {
          if( q[i] <= q[j] ) tmp[ k ++ ] = q[ i ++ ];
          else                   tmp[ k ++ ] = q[ j ++ ];
    }
    while( i <= mid ) tmp[ k ++ ] = q[ i ++ ];
    while( j <= r )  tmp[ k ++ ] = q[ j ++ ];
    for( int i = l, j = 0; i <= r; i ++, j ++ ) q[ i ]  = tmp[ j ];
}

 

kmp algorithm

KMP algorithm using the key information after the matching fails, to minimize the number of main string pattern string match to achieve the purpose of fast matching. Specific implementation is to implement a next () function, the function itself contains partial information pattern string matching. The time complexity of O (m + n)

In simple algorithm, every time we match all match schedule before failures had to give, so the time complexity is very high, while K M P essence of the KMP algorithm lies not proceed from the beginning of the string after each failure mode match match, but the match according to the known data, jump back to a particular pattern string matching location continues, and for each bit, has a unique "snap-back position of a specific" pattern string, thereby saving time.

Pointer mismatch: position i value is the pattern string S (subscript a to 1 i) mismatch array the maximum length of the common prefix and proper suffix true

// seek mismatch array
 // is the string itself a pattern matching process itself, just say the array is built with a negligence within the meaning of the pattern string, with the same text string matching ideas 
int the n-= s1.length ();
 int m = s2.legnth ();
 for ( int I = . 1 ; I <m; I ++ ) 
{ 
    the while (! && S2 K [I] = S2 [K]) K = KMP [K];
     IF (S2 [I] = S2 [k]) KMP [I + . 1 ] ++ = k; 
} 

// find patterns strings in the text string
 // where k can be seen that the current mode has been finished to match a string of final position, you can also be understood as a schematic of several of the matched string 

K = 0 ;
 for ( int I = 0; I <n-; ++ I) 
{ 
    the while (S1 && K [I] = S2 [K]) = K! KMP [K];
     // match fails to set back along mismatch pointer jump pattern string the first would not jump up. 
    IF (S1 [I] == S2 [K]) K ++;     // successful match, the match pattern string to position + 1'd 
    IF (K == m) COUT << I - m + 2 << endl;     / / find a pattern string, output location. 
}

 

Critical path diagram

No analysis of the critical path is a more important use of FIG. Each node represents a time to give the action that must be performed as well as it takes to complete the action. Thus, the operation of the node graph of FIG.

Side in the figure represents a priority relationship: an edge (v, w) v means that action must be completed before the start of the action w.

 

 Conditions began last thing is to complete all pre-event, pre-event can be specified in parallel with each other, but the earliest completion time is the longest path

 

Guess you like

Origin www.cnblogs.com/byene/p/12640382.html