Fast sorting algorithm implemented by using function pointers

Source: Internet
Author: User

Chapter 5 of K & R introduces the fast sorting algorithm implemented by using function pointers, and is confused about the code. So I read the introduction to fast sorting in introduction to algorithms. The induction algorithm is as follows:

Set the leftmost of each traversal to low, rightmost to high, and array name to:


1.Each time, we use the leftmost number A [low] as the reference number (low), traverse from the second number on the left (A [Low + 1]), and maintainI --> JUntil J reaches the rightmost (high ).

2.At this time,I --> highThe numbers between them are larger than those between them,Low + 1 --> I-1The number between them is smaller than the limit value.

3.Swap the limit and the number of I-1 A [I-1].

4.For low --> I-1, I --> The number between high recursively calls the above process 1, 2, 3


The following figure shows the principle and process of the quick release:



Use C to implement the above process:

#include <stdio.h>/* declare function pointer */typedef int(*fp_operation)(int a, int b);void swap(int *a, int *b) {    int temp = *a;    *a = *b;    *b = temp;}int compare(int a, int b) {    return (a-b);}void my_qsort(int arr[], int low, int high, fp_operation cmp) {    if(low >= high) {        return;    }    int pivot = arr[low];    int mid;    int i, j;    i = j = low+1;    while(j <= high) {    if((*cmp)(arr[j], pivot) < 0) {    swap(&arr[j], &arr[i]);    i++;    }    j++;    }    swap(&arr[low], &arr[i-1]);    mid = i-1;//    int k = 0;//    printf("mid: %d\n", mid);//    while(k < 16) {//    printf("%d ", arr[k]);//    k++;//    }//    printf("\n");    my_qsort(arr, low, mid-1, cmp);    my_qsort(arr, mid+1, high, cmp);}int main() {    int arr[] = {2, 1, 7, 1, 5, 4, 9, 6, 1, 2, 4, 0, 5, 2, 1, 3};    int n = sizeof(arr) / sizeof(arr[0]);    my_qsort(arr, 0, n-1, (fp_operation)compare);    int i = 0;    while(i < n) {        printf("%d ", arr[i]);        i++;    }    printf("\n");    return 0;}


GitHub: https://github.com/wusuopubupt/LearningC/blob/master/K%26R/chp5/quick_sort.c



Fast sorting algorithm implemented by using function pointers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.