Step-by-step write algorithm (the preparation of the general algorithm)

Source: Internet
Author: User

Original: Step by step Write algorithm (the general algorithm of writing)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


We've written all sorts of algorithms, what sort, find, binary tree, queue, stack, and so on. But we have a flaw in writing this code, do not know that you have found it? That is, the data structures used in these algorithms are simple int. So, if the sort is int, then there's nothing wrong with it. The point is, what should we do if there are other data types?

In C + +, there is a workaround. That's the class function. Take the bubble sort out, we can write it this way.

Template <typename type>void bubble_sort (type array[], int length) {int outer;int inner;type median;if (NULL = = Array || 0 = = length) return;for (outer = length-1; Outer >0; outer-) {for (inner = 0; inner < outer; inner + +) {if (Array[inner ] > Array[inner +1]) {median = Array[inner];array[inner] = Array[inner +1];array[inner +1] = median;}}} return;}
Of course, if it is a class that needs to call the algorithm above, it also needs to define the type default constructor, type copy enough to construct two functions.

So, is there any way in C language? In fact, there is, that is void* this method.

void Bubble_sort (void* array[], int length, int (*compare) (void*, void*), Void (*swap) (void*, void*)) {int outer;int inner; for (outer = length-1; Outer >0; outer-) {for (inner = 0; inner < outer; inner + +) {if (compare (Array[inner), array[in NER + 1]) swap (Array[inner], Array[inner + 1]);}} return;}
Then, in a specific application, it is only necessary to convert the void* to the data pointer you need. For example, if you sort int, we need to add these two functions.

int compare (void* var1, void* var2) {int* p_var1 = (int*) var1;int* p_var2 = (int*) var2;return (*p_var1 > *p_var2)? 1: 0;} void swap (void* var1, void* var2) {int* p_var1 = (int*) var1;int* p_var2 = (int*) var2;int Median;median = *p_var1;*p_var1 = *P_VAR2;*P_VAR2 = median;}

The function call looks like this, and the data conversion is a little cumbersome.

void Test () {int array[5] = {1, 2, 4,3,5};int* p_array[5] = {&array[0], &array[1], &array[2], &array[3], &A Mp;array[4]};bubble_sort ((void**) P_array, 5, compare, swap); return;}


Summarize:

(1) Write a specific type of algorithmic function before writing a general function.

(2) The key to the general algorithm is how to separate the common content from the specific data type.

(3) C + + and C language in the general algorithm each has its own methods, it is recommended to use a lot, especially some commonly used functions.


Step-by-step write algorithm (the preparation of the general algorithm)

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.