C language sorting algorithm learning

Sorting algorithms are a classic problem in computer science, often used in interviews or course assignments. Several common sorting algorithms and their practical examples are introduced below.

Bubble Sort

Bubble sorting is a basic sorting algorithm. Its principle is to start from the first element of the array, compare the two adjacent elements in turn, and exchange their positions if the previous element is larger than the latter element. In this way, the largest element will be swapped to the last position of the array, and then the process will be repeated from the first element until the entire array is sorted.

The following is the C language code implementation of bubble sort:

void bubbleSort(int arr[], int n) {
    int i, j;
    for (i = 0; i < n-1; i++) {
        for (j = 0; j < n-i-1; j++) {
            if (arr[j] > arr[j+1]) {
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

Selection Sort

Selection sort is also a basic sorting algorithm. Its principle is to find the smallest element from the array, and then exchange it with the first element of the array. Then find the smallest element among the remaining elements and swap it with the second element of the array. And so on until the entire array is sorted.

The following is the C language code implementation of selection sort:

void selectionSort(int arr[], int n) {
    int i, j, min_idx;
    for (i = 0; i < n-1; i++) {
        min_idx = i;
        for (j = i+1; j < n; j++) {
            if (arr[j] < arr[min_idx]) {
                min_idx = j;
            }
        }
        int temp = arr[i];
        arr[i] = arr[min_idx];
        arr[min_idx] = temp;
    }
}

Insertion Sort

The principle of insertion sort is to divide the array into sorted and unsorted parts, and then insert the unsorted elements into the sorted part one by one until the entire array is sorted.

The following is the C language code implementation of insertion sort:

void insertionSort(int arr[], int n) {
    int i, j, key;
    for (i = 1; i < n; i++) {
        key = arr[i];
        j = i - 1;
        while (j >= 0 && arr[j] > key) {
            arr[j+1] = arr[j];
            j = j - 1;
        }
        arr[j+1] = key;
    }
}

Quick sort (Quick Sort)

Quick sort is an efficient sorting algorithm. Its principle is to select a reference element, and then place elements in the array that are smaller than the reference element to the left of the reference element, and elements that are larger than the reference element to the right of the reference element. Then recursively perform quick sort on the left and right parts, until the entire array is sorted.

The following is the C language code implementation of quick sort:

void quickSort(int arr[], int low, int high) {
    if (low < high) {
        int pi = partition(arr, low, high);
        quickSort(arr, low, pi-1);
        quickSort(arr, pi+1, high);
    }
}

int partition(int arr[], int low, int high) {
    int pivot = arr[high];
    int i = low - 1;
    for (int j = low; j < high; j++) {
        if (arr[j] < pivot) {
            i++;
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
    int temp = arr[i+1];
    arr[i+1] = arr[high];
    arr[high] = temp;
    return i+1;
}

Shell sort (Shell Sort)

Hill sort is an efficient sorting algorithm, which is an improved version of insertion sort. Its principle is to divide the elements in the array with a certain interval into one group, perform insertion sort on each group, then gradually reduce the interval, and repeat the above process until the interval is 1, that is, insertion sort. The time complexity of Hillsort is O(n 2) or O(n 3/2), depending on the choice of interval sequence.

The following is the C language code implementation of Hill sort:

void shellSort(int arr[], int n) {
    int i, j, gap;
    for (gap = n/2; gap > 0; gap /= 2) {
        for (i = gap; i < n; i++) {
            int temp = arr[i];
            for (j = i; j >= gap && arr[j-gap] > temp; j -= gap) {
                arr[j] = arr[j-gap];
            }
            arr[j] = temp;
        }
    }
}

Merge Sort

Merge sort is an efficient sorting algorithm. Its principle is to recursively divide the array into two sub-arrays, then sort the two sub-arrays separately, and finally merge them into an ordered array. The time complexity of merge sort is O(nlogn).

The following is the C language code implementation of merge sort:

void mergeSort(int arr[], int l, int r) {
    if (l < r) {
        int m = l + (r-l)/2;
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);
        merge(arr, l, m, r);
    }
}

void merge(int arr[], int l, int m, int r) {
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;
    int L[n1], R[n2];
    for (i = 0; i < n1; i++) {
        L[i] = arr[l+i];
    }
    for (j = 0; j < n2; j++) {
        R[j] = arr[m+1+j];
    }
    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        } else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}

Guess you like

Origin blog.csdn.net/weixin_51624736/article/details/129622324