Write an algorithm step by step (writing a general algorithm)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

We have written various algorithms, such as sorting, searching, binary tree, queue, and stack. However, when writing these codes, we all have a disadvantage. I don't know if you find it? That is, the data structures used in these algorithms are simple int data. Therefore, if the sorting is int, there is no problem in use. What should we do if it is another data type?

 

In c ++, there is a solution. It is a class function. In the case of Bubble sorting, we can write this statement completely.

 

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;

}

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 a class needs to call the above algorithm, it also needs to define the default type constructor and copy the type to the constructor.

 

So, is there any way in C language? In fact, there is also the void * 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 [inner + 1])

Swap (array [inner], array [inner + 1]);

}

}

 

Return;

}

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 [inner + 1])

Swap (array [inner], array [inner + 1]);

}

}

 

Return;

} Then, in a specific application, you only need to convert void * to the Data Pointer you need. For example, for int sorting, 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;

}

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;

}

 

Function calling is shown below, which makes data conversion a little effort.

 

Void test ()

{

Int array [5] = {1, 2, 4, 3, 5 };

Int * p_array [5] = {& array [0], & array [1], & array [2], & array [3], & array [4]};

Bubble_sort (void **) p_array, 5, compare, swap );

 

Return;

}

Void test ()

{

Int array [5] = {1, 2, 4, 3, 5 };

Int * p_array [5] = {& array [0], & array [1], & array [2], & array [3], & array [4]};

Bubble_sort (void **) p_array, 5, compare, swap );

 

Return;

}

 

 

 

 

 

Summary:

(1) Write specific types of Algorithm functions before writing general functions.

 

(2) The key to general algorithms is how to separate general content from specific data types.

 

(3) C ++ and C languages have different methods in general algorithms. We recommend that you use them more frequently, especially some frequently used functions.

Related Article

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.