C ++ classic title -Shell sort algorithm - improved insertion sort

34.Algorithm Gossip: Shell sort - Modified insertion sort

Explanation

Insertion sort out a value from the front end of the rear half of unsorted, sorted into the appropriate position of the first half, the concept of simple but fast enough.

One of the basic principles of the sort to be accelerated, is the result of a sort when let after once before ordering to make full use, in order to speed up the sort of speed, Shell sort that is based on this concept to improve insertion sort.

solution

Shell sort method when the DL was originally proposed in 1959 Shell, is assumed to be sorted has n elements, then every time the sort is not inserted all the elements at the same time, but rather, some distance.

Shell first interval is set to n / 2, then skip insertion sort, the spacer again n / 4, the operation jumps to sort, come interval is set to n / 8, n / 16, until after the last interval of 1 Sort terminated because the last time the action will be ordering elements within a fixed interval ordering good, so when the interval smaller and smaller, the higher the probability of certain elements in the correct position, so the last few sort of action will be significantly reduced.

For example, if there is a non-ordered set of numbers at right: 8,912,659,761,812,726,198

Total numbers of a total of 10, so we will first time interval is set to 10/2 = 5, then we sort of digital interval 5, as follows:
Here Insert Picture Description
part of the objects to be sorted connected together represent part, the interval is set again supplier 5/2, i.e. 2, is inserted into the second sort object as follows:
Here Insert Picture Description
again interval is set to 2/2 = 1 at this time is a simple insertion sort , because most of the elements are roughly sorted, so the last insertion sort almost did not make any sort of action:
Here Insert Picture Description
the interval is set to n / 2 is DL Shell originally proposed, the use of this interval in textbooks better description, but the key is that the Shell sort selected intervals, e.g. Sedgewick selected following interval may prove speed of Shell sort:
Here Insert Picture Description
wherein 4 * (2j) 2 + 3 * (2j) + 1 can not exceed the total number of elements the value of n, j to identify the progeny of the formula 4 * (2j) 2 + 3 * (2j) + 1 obtained by the first interval, then 2j obtained by dividing 2 into the second interval, and so on again .

Later also prove other intervals may be selected method Shell sort of speed and then accelerated; Shell sort Further concept may also be used to improve bubble sort.

The sample code

#include <stdio.h>
#include <stdlib.h> 
#include <time.h> 
#define MAX 10
#define SWAP(x,y) {int t; t = x; x = y; y = t;} void shellsort(int[]);
    int main(void) {
        int number[MAX] = {0}; int i;
        srand(time(NULL)); printf("排序前:");
        for(i = 0; i < MAX; i++) { number[i] = rand() % 100; printf("%d ", number[i]);
        }

        shellsort(number);

        return 0;
    }


    void shellsort(int number[]) { int i, j, k, gap, t;
        gap = MAX / 2; while(gap > 0) {
            for(k = 0; k < gap; k++) {
                for(i = k+gap; i < MAX; i+=gap) { for(j = i - gap; j >= k; j-=gap) {
                    if(number[j] > number[j+gap]) { SWAP(number[j], number[j+gap]);

                    }
                    else

                }
                }
            }

            break;
            printf("\ngap = %d:", gap); for(i = 0; i < MAX; i++)
                printf("%d ", number[i]); printf("\n");

            gap /= 2;
        }
    }

Released 1140 original articles · won praise 928 · views 70000 +

Guess you like

Origin blog.csdn.net/weixin_42528266/article/details/104020492