Sorting Algorithms: Selection Sort, Insertion Sort, Hill Sort

Regular functions take the following form (character sort):

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* swap string two items */
void exchange(char *in, int i1, int i2)
{
    char ch = (char)in[i1];
    in[i1] = in[i2];
    in[i2] = ch;
}
/*string length*/
int length(char *in)
{
    int i=0;
    while(in[i] != '\0')
        i++;
    return i;
}
/* output string */
void showString(char *str)
{
    int N = length(str);
    int i=0;
    while(i<N)
    {
        printf("%c ",str[i]);
        i++;
    }
    printf("\n");
}

selection sort

Find the smallest element in the array, swap it with the first element of the array. Find the smallest element from the remaining elements and swap it with the second element of the array. Keep doing this until the entire array is sorted.

/*selection sort*/
void selectSort(char *in)
{
    int i, j, min;
    int N = length(in);
    for(i=0;i<N-1;i++)
    {
        min = i;
        for(j=i+1;j<N;j++)
        {
            if(in[j]<in[min]) min = j;
        }
        exchange(in,i,min);
        showString(in);
    }
}

   

Selection sort requires ~N^2/2 comparisons and ~N swaps, and its running time is independent of the input, a feature that makes it require so many comparisons and swaps for an already sorted array.

Insertion sort

Insertion sort is performed from left to right, inserting the current element into the sorted array on the left each time, so that the left array is still in order after insertion.

/*insertion sort*/
void insertSort(char *in)
{
    int i, j;
    int N = length(in);
    for(i=1;i<N;i++)
    {
        for(j=i; j>0 && in[j]<in[j-1]; j--)
        {
            exchange(in,j,j-1);
            showString(in);
        }
    }
}

   

The complexity of insertion sort depends on the initial order of the array, if the array is already partially sorted, then insertion sort will be fast.
Insertion sort requires ~N^2/4 comparisons and ~N^2/4 swaps on average;
worst case requires ~N^2/2 comparisons and ~N^2/2 swaps, worst case Yes the array is in reverse order; in the
best case N-1 comparisons and 0 swaps are required, and in the best case the array is already sorted.

Insertion sort is particularly efficient for partially sorted and small arrays.

Hill Sort
Insertion sort is slow for large arrays because it can only swap adjacent elements, requiring many operations to move elements from one end to the other.
The appearance of Hill sort is to improve this limitation of insertion sort. It makes elements move to the correct position faster by exchanging non-adjacent elements.

Hill sort uses insertion sort to sort sequences at intervals of h. If h is large, elements can be moved very quickly to great distances. By continuously reducing h, and finally setting h = 1, the entire array can be made in order.

/* Hill sort */
void shellSort(char *in)
{
    int i, j;
    int N = length(in);
    int h = 1;
    while(h < N / 3) {
        h = 3 * h + 1; // 1, 4, 13, 40, ...
    }
    while (h >= 1)
    {
        for (i = h; i < N; i++)
        {
            for (j = i; j >= h && in[j]<in[j - h]; j -= h)
            {
                exchange(in, j, j - h);
                printf("(%d,%d)%d, ",i,j,h);showString(in);
            }
        }
        h = h / 3;
    }
}

   

The running time of Hill sort does not reach the square level, and the number of comparisons required for Hill sort using the increasing sequence 1, 4, 13, 40, ... will not exceed several times N times the length of the increasing sequence. The advanced sorting algorithm described later will only be about twice as fast as the Hill sort.

Full code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/* swap string two items */
void exchange(char *in, int i1, int i2)
{
    char ch = (char)in[i1];
    in[i1] = in[i2];
    in[i2] = ch;
}
/*string length*/
int length(char *in)
{
    int i=0;
    while(in[i] != '\0')
        i++;
    return i;
}
/* output string */
void showString(char *str)
{
    int N = length(str);
    int i=0;
    while(i<N)
    {
        printf("%c ",str[i]);
        i++;
    }
    printf("\n");
}
/*selection sort*/
void selectSort(char *in)
{
    int i, j, min;
    int N = length(in);
    for(i=0;i<N-1;i++)
    {
        min = i;
        for(j=i+1;j<N;j++)
        {
            if(in[j]<in[min]) min = j;
        }
        exchange(in,i,min);
        showString(in);
    }
}

/*insertion sort*/
void insertSort(char *in)
{
    int i, j;
    int N = length(in);
    for(i=1;i<N;i++)
    {
        for(j=i; j>0 && in[j]<in[j-1]; j--)
        {
            exchange(in,j,j-1);
            showString(in);
        }
    }
}

/* Hill sort */
void shellSort(char *in)
{
    int i, j;
    int N = length(in);
    int h = 1;
    while(h < N / 3) {
        h = 3 * h + 1; // 1, 4, 13, 40, ...
    }
    while (h >= 1)
    {
        for (i = h; i < N; i++)
        {
            for (j = i; j >= h && in[j]<in[j - h]; j -= h)
            {
                exchange(in, j, j - h);
                showString(in);
            }
        }
        h = h / 3;
    }
}

/*main function*/
int main(int argc, char**argv)
{
    char str[] = "hgfedbca";
    showString(str);
    shellSort(str);
    showString(str);
    
    return 1;
}
More content: https://github.com/CyC2018/Interview-Notebook

This article only rewrites the Java code in C language, if there is any infringement, please contact to delete!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326529931&siteId=291194637