Use of sorting and searching algorithms

2014/09/20 16:30
Reading number 558

TBOX provides various common algorithms to perform various operations on the elements in the container. Here we mainly introduce the sorting and searching algorithms.

The sorting algorithm currently supports the following:

  1. Quick sort: tb_quick_sort
  2. Heap sorting: tb_heap_sort
  3. Insert Sort: tb_bubble_sort
  4. Bubble sort: tb_insert_sort

It also provides a general tb_sort interface to automatically adapt various sorting algorithms, so that the performance is optimal in any case. For example:

  1. For containers with random iteration characteristics, use quick sorting to optimize
  2. For containers with random iteration characteristics and super large scale, heap sorting is adopted
  3. Bubble sort containers that can only iterate linearly

So in general, you only need to call tb_sort

 //Initialize a vector. The element type is tb_long_t, and the full 256 elements will grow automatically tb_vector_ref_t vector = tb_vector_init(256, tb_item_func_long()); if (vector) { //Insert some elements tb_vector_insert_tail(vector, (tb_cpointer_t)10); tb_vector_insert_tail(vector, (tb_cpointer_t)2); tb_vector_insert_tail(vector, (tb_cpointer_t)5); tb_vector_insert_tail(vector, (tb_cpointer_t)6); tb_vector_insert_tail(vector, (tb_cpointer_t)9); //Sort all. The second parameter is the comparator function. By default, the container built-in comparator is used tb_sort_all(vector, tb_null); //Release vector tb_vector_exit(vector); }

For the search algorithm, it currently provides:

  1. Linear lookup: tb_find
  2. Reverse linear lookup: tb_rfind
  3. Binary search: tb_binary_find

If the container has the feature of random iteration, you can use binary search to optimize, such as vector, native array, etc..

 //Initialize a vector. The element type is tb_long_t, and the full 256 elements will grow automatically tb_vector_ref_t vector = tb_vector_init(256, tb_item_func_long()); if (vector) { //Insert some ordered elements tb_vector_insert_tail(vector, (tb_cpointer_t)1); tb_vector_insert_tail(vector, (tb_cpointer_t)2); tb_vector_insert_tail(vector, (tb_cpointer_t)4); tb_vector_insert_tail(vector, (tb_cpointer_t)6); tb_vector_insert_tail(vector, (tb_cpointer_t)9); //The binary search method is used to quickly find elements, and the algorithm complexity is O (log2) tb_size_t itor = tb_binary_find_all(vector, (tb_cpointer_t)5); if (itor !=  tb_iterator_tail(vector)) { //Get element value: 5 tb_size_t value = tb_iterator_item(vector, itor); } //Release vector tb_vector_exit(vector); }

You can also specify a comparator function to make searching more flexible.

 //Compare functions in descending order static tb_long_t your_comp_func(tb_iterator_ref_t iterator,  tb_cpointer_t ltem, tb_cpointer_t rtem) { return ((tb_long_t)ltem < (tb_long_t)rtem?  1 : ((tb_long_t)ltem > (tb_long_t)rtem? - 1 : 0)); } //Initialize a vector. The element type is tb_long_t, and the full 256 elements will grow automatically tb_vector_ref_t vector = tb_vector_init(256, tb_item_func_long()); if (vector) { //Insert some ordered elements tb_vector_insert_tail(vector, (tb_cpointer_t)1); tb_vector_insert_tail(vector, (tb_cpointer_t)2); tb_vector_insert_tail(vector, (tb_cpointer_t)4); tb_vector_insert_tail(vector, (tb_cpointer_t)6); tb_vector_insert_tail(vector, (tb_cpointer_t)9); //Use binary search method to quickly find elements and specify your own comparator function tb_size_t itor = tb_binary_find_all_if(vector,  your_comp_func, tb_null); if (itor !=  tb_iterator_tail(vector)) { //Get element value: 5 tb_size_t value = tb_iterator_item(vector, itor); } //Release vector tb_vector_exit(vector); }

Native arrays can also be compared using algorithms. Here is a common example of searching:

 //Define the data structure of a character set operation typedef struct __tb_charset_t { tb_size_t           type; tb_char_t const*    name; tb_long_t            (*get)(tb_static_stream_ref_t sstream,  tb_bool_t be, tb_uint32_t* ch); tb_long_t            (*set)(tb_static_stream_ref_t sstream,  tb_bool_t be, tb_uint32_t ch); }tb_charset_t; //Define a native array static tb_charset_t charsets[] = { {TB_CHARSET_TYPE_ASCII,     "ascii",     tb_charset_ascii_get,   tb_charset_ascii_set    } ,   {TB_CHARSET_TYPE_GB2312,    "gb2312",    tb_charset_gb2312_get,  tb_charset_gb2312_set   } ,   {TB_CHARSET_TYPE_GBK,       "gbk",       tb_charset_gb2312_get,  tb_charset_gb2312_set   } ,   {TB_CHARSET_TYPE_ISO8859,   "iso8859",   tb_charset_iso8859_get, tb_charset_iso8859_set  } ,   {TB_CHARSET_TYPE_UCS2,      "ucs3",      tb_charset_ucs2_get,    tb_charset_ucs2_set     } ,   {TB_CHARSET_TYPE_UCS4,      "ucs4",      tb_charset_ucs4_get,    tb_charset_ucs4_set     } ,   {TB_CHARSET_TYPE_UTF16,     "utf16",     tb_charset_utf16_get,   tb_charset_utf16_set    } ,   {TB_CHARSET_TYPE_UTF32,     "utf32",     tb_charset_utf32_get,   tb_charset_utf32_set    } ,   {TB_CHARSET_TYPE_UTF8,      "utf8",      tb_charset_utf8_get,    tb_charset_utf8_set     } }; //Find Comparison Function by Name static tb_long_t tb_charset_comp_by_name(tb_iterator_ref_t iterator,  tb_cpointer_t item, tb_cpointer_t name) { return tb_stricmp(((tb_charset_ref_t)item)->name, (tb_char_t const*)name); } //Initialize the original array into an iterator tb_iterator_t iterator = tb_iterator_init_mem(charsets, tb_arrayn(charsets), sizeof(tb_charset_t)); //Perform binary search according to the name for this iterator tb_size_t itor = tb_binary_find_all_if(&iterator,  tb_charset_comp_by_name,  "utf8"); //If found if (itor !=  tb_iterator_tail(&iterator)) { //Get Element Object tb_charset_t* charset = (tb_charset_t*)tb_iterator_item(&iterator, itor); }

Note: The above example is excerpted from the code inside the TBOX library. It is only for reference and cannot be copied directly.


Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
nine Collection
zero fabulous
 Back to top
Top