Use of sorting and searching algorithms

Source: Internet
Author: User

Tbox provides a variety of common algorithms to perform operations on elements in the container. Here we mainly introduce the sorting and searching algorithms.

Sorting algorithms currently support the following types:

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

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

  1. For containers with random iteration characteristics, database sorting is used for optimization.
  2. For containers with random iteration and ultra-large scale, heap sorting is adopted.
  3. Use Bubble Sorting for containers that can only be linearly iterated

In general, you only need to call tb_sort.

// Initialize a vector. The element type is tb_long_t. When 256 elements are used, the value of tb_vector_ref_t vector = tb_vector_init (256, tb_item_func_long (); If (vector) {// insert some element values (vector, (tb_cpointer_t) 10); values (vector, (tb_cpointer_t) 2); tb_vector_insert_tail (vector, (tb_cpointer_t) 5); values (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 built-in comparator tb_sort_all (vector, tb_null); // release vector tb_vector_exit (vector );}

For search algorithms, the following functions are available:

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

If the container has the random iteration feature, you can use binary search for optimization, such as vector and native array ..

// Initialize a vector. The element type is tb_long_t. When 256 elements are used, the value of tb_vector_ref_t vector = tb_vector_init (256, tb_item_func_long (); If (vector) {// insert some ordered element values (vector, (tb_cpointer_t) 1); values (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 the Binary Search Method to quickly find elements and improve algorithm complexity O (log2) tb_size_t itor = tb_binary_find_all (vector, (tb_cpointer_t) 5); If (itor! = Tb_iterator_tail (vector) {// obtain the element value: 5 tb_size_t value = tb_iterator_item (vector, itor);} // release the vector tb_vector_exit (vector );}

You can also specify a comparator function for more flexible search.

// 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. When 256 elements are used, the system automatically increases 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); counts (vector, (tb_cpointer_t) 2); counts (vector, (tb_cpointer_t) 4); tb_vector_insert_tail (vector, (tb_cpointer_t) 6); tb_vector_insert_tail (vector, (tb_cpointer_t) 9); // use the binary search method, fast Search for elements and specify your own comparator function tb_size_t itor = tb_binary_find_all_if (vector, your_comp_func); If (itor! = Tb_iterator_tail (vector) {// obtain the element value: 5 tb_size_t value = tb_iterator_item (vector, itor);} // release the vector tb_vector_exit (vector );}

Native Arrays can also be compared using algorithms. Below is a common search example:

// Define the data structure 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, limit * Ch) of a character set operation ); 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 [] = {delimiter, "ASCII ", tb_charset_ascii_get, tb_charset_ascii_set}, {tb_charset_type _ Gb2312, "gb2312", numbers, numbers}, {tb_charset_type_gbk, "GBK", numbers, numbers}, {tb_charset_type_iso8859, "iso8859", numbers, numbers}, {numbers, "ucs3", tb_charset_ucs2_get, tb_charset_ucs2_set}, {tb_charset_type_ucs4, "ucs4", tb_charset_ucs4_get, signature}, {tb_charset_type_ut F16, "UTF16", encoding, encoding}, {encoding, "UTF32", encoding, encoding}, {tb_charset_type_utf8, "utf8", tb_charset_utf8_get, tb_charset_utf8_set }}; // query by name the comparison function static tb_long_t iterator (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 con St *) Name);} // initialize the native array into an iterator tb_iterator_t iterator = tb_iterator_init_mem (charsets, tb_arrayn (charsets), sizeof (tb_charset_t )); // This iterator is divided into tb_size_t itor = tb_binary_find_all_if (& iterator, tb_charset_comp_by_name, "utf8") based on the name. // If the IF (itor! = Tb_iterator_tail (& iterator) {// obtain the Element Object tb_charset_t * charset = (tb_charset_t *) tb_iterator_item (& iterator, itor );}

Note: The above example extracts the code from the tbox library for reference only and cannot be copied directly.
Tbox provides a variety of common algorithms to perform operations on elements in the container. Here we mainly introduce the sorting and searching algorithms.

Sorting algorithms currently support the following types:

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

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

  1. For containers with random iteration characteristics, database sorting is used for optimization.
  2. For containers with random iteration and ultra-large scale, heap sorting is adopted.
  3. Use Bubble Sorting for containers that can only be linearly iterated

In general, you only need to call tb_sort.

// Initialize a vector. The element type is tb_long_t. When 256 elements are used, the value of tb_vector_ref_t vector = tb_vector_init (256, tb_item_func_long (); If (vector) {// insert some element values (vector, (tb_cpointer_t) 10); values (vector, (tb_cpointer_t) 2); tb_vector_insert_tail (vector, (tb_cpointer_t) 5); values (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 built-in comparator tb_sort_all (vector, tb_null); // release vector tb_vector_exit (vector );}

For search algorithms, the following functions are available:

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

If the container has the random iteration feature, you can use binary search for optimization, such as vector and native array ..

// Initialize a vector. The element type is tb_long_t. When 256 elements are used, the value of tb_vector_ref_t vector = tb_vector_init (256, tb_item_func_long (); If (vector) {// insert some ordered element values (vector, (tb_cpointer_t) 1); values (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 the Binary Search Method to quickly find elements and improve algorithm complexity O (log2) tb_size_t itor = tb_binary_find_all (vector, (tb_cpointer_t) 5); If (itor! = Tb_iterator_tail (vector) {// obtain the element value: 5 tb_size_t value = tb_iterator_item (vector, itor);} // release the vector tb_vector_exit (vector );}

You can also specify a comparator function for more flexible search.

// 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. When 256 elements are used, the system automatically increases 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); counts (vector, (tb_cpointer_t) 2); counts (vector, (tb_cpointer_t) 4); tb_vector_insert_tail (vector, (tb_cpointer_t) 6); tb_vector_insert_tail (vector, (tb_cpointer_t) 9); // use the binary search method, fast Search for elements and specify your own comparator function tb_size_t itor = tb_binary_find_all_if (vector, your_comp_func); If (itor! = Tb_iterator_tail (vector) {// obtain the element value: 5 tb_size_t value = tb_iterator_item (vector, itor);} // release the vector tb_vector_exit (vector );}

Native Arrays can also be compared using algorithms. Below is a common search example:

// Define the data structure 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, limit * Ch) of a character set operation ); 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 [] = {delimiter, "ASCII ", tb_charset_ascii_get, tb_charset_ascii_set}, {tb_charset_type _ Gb2312, "gb2312", numbers, numbers}, {tb_charset_type_gbk, "GBK", numbers, numbers}, {tb_charset_type_iso8859, "iso8859", numbers, numbers}, {numbers, "ucs3", tb_charset_ucs2_get, tb_charset_ucs2_set}, {tb_charset_type_ucs4, "ucs4", tb_charset_ucs4_get, signature}, {tb_charset_type_ut F16, "UTF16", encoding, encoding}, {encoding, "UTF32", encoding, encoding}, {tb_charset_type_utf8, "utf8", tb_charset_utf8_get, tb_charset_utf8_set }}; // query by name the comparison function static tb_long_t iterator (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 con St *) Name);} // initialize the native array into an iterator tb_iterator_t iterator = tb_iterator_init_mem (charsets, tb_arrayn (charsets), sizeof (tb_charset_t )); // This iterator is divided into tb_size_t itor = tb_binary_find_all_if (& iterator, tb_charset_comp_by_name, "utf8") based on the name. // If the IF (itor! = Tb_iterator_tail (& iterator) {// obtain the Element Object tb_charset_t * charset = (tb_charset_t *) tb_iterator_item (& iterator, itor );}

Note: The above example extracts the code from the tbox library for reference only and cannot be copied directly.
Tbox provides a variety of common algorithms to perform operations on elements in the container. Here we mainly introduce the sorting and searching algorithms.

Sorting algorithms currently support the following types:

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

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

  1. For containers with random iteration characteristics, database sorting is used for optimization.
  2. For containers with random iteration and ultra-large scale, heap sorting is adopted.
  3. Use Bubble Sorting for containers that can only be linearly iterated

In general, you only need to call tb_sort.

// Initialize a vector. The element type is tb_long_t. When 256 elements are used, the value of tb_vector_ref_t vector = tb_vector_init (256, tb_item_func_long (); If (vector) {// insert some element values (vector, (tb_cpointer_t) 10); values (vector, (tb_cpointer_t) 2); tb_vector_insert_tail (vector, (tb_cpointer_t) 5); values (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 built-in comparator tb_sort_all (vector, tb_null); // release vector tb_vector_exit (vector );}

For search algorithms, the following functions are available:

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

If the container has the random iteration feature, you can use binary search for optimization, such as vector and native array ..

// Initialize a vector. The element type is tb_long_t. When 256 elements are used, the value of tb_vector_ref_t vector = tb_vector_init (256, tb_item_func_long (); If (vector) {// insert some ordered element values (vector, (tb_cpointer_t) 1); values (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 the Binary Search Method to quickly find elements and improve algorithm complexity O (log2) tb_size_t itor = tb_binary_find_all (vector, (tb_cpointer_t) 5); If (itor! = Tb_iterator_tail (vector) {// obtain the element value: 5 tb_size_t value = tb_iterator_item (vector, itor);} // release the vector tb_vector_exit (vector );}

You can also specify a comparator function for more flexible search.

// 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. When 256 elements are used, the system automatically increases 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); counts (vector, (tb_cpointer_t) 2); counts (vector, (tb_cpointer_t) 4); tb_vector_insert_tail (vector, (tb_cpointer_t) 6); tb_vector_insert_tail (vector, (tb_cpointer_t) 9); // use the binary search method, fast Search for elements and specify your own comparator function tb_size_t itor = tb_binary_find_all_if (vector, your_comp_func); If (itor! = Tb_iterator_tail (vector) {// obtain the element value: 5 tb_size_t value = tb_iterator_item (vector, itor);} // release the vector tb_vector_exit (vector );}

Native Arrays can also be compared using algorithms. Below is a common search example:

// Define the data structure 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, limit * Ch) of a character set operation ); 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 [] = {delimiter, "ASCII ", tb_charset_ascii_get, tb_charset_ascii_set}, {tb_charset_type _ Gb2312, "gb2312", numbers, numbers}, {tb_charset_type_gbk, "GBK", numbers, numbers}, {tb_charset_type_iso8859, "iso8859", numbers, numbers}, {numbers, "ucs3", tb_charset_ucs2_get, tb_charset_ucs2_set}, {tb_charset_type_ucs4, "ucs4", tb_charset_ucs4_get, signature}, {tb_charset_type_ut F16, "UTF16", encoding, encoding}, {encoding, "UTF32", encoding, encoding}, {tb_charset_type_utf8, "utf8", tb_charset_utf8_get, tb_charset_utf8_set }}; // query by name the comparison function static tb_long_t iterator (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 con St *) Name);} // initialize the native array into an iterator tb_iterator_t iterator = tb_iterator_init_mem (charsets, tb_arrayn (charsets), sizeof (tb_charset_t )); // This iterator is divided into tb_size_t itor = tb_binary_find_all_if (& iterator, tb_charset_comp_by_name, "utf8") based on the name. // If the IF (itor! = Tb_iterator_tail (& iterator) {// obtain the Element Object tb_charset_t * charset = (tb_charset_t *) tb_iterator_item (& iterator, itor );}
Note: The above example extracts the code from the tbox library for reference only and cannot be copied directly.
  • Tbox project details
  • Tbox project source code
  • Tbox Project Document

Use of sorting and searching algorithms

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.