C + + implementation of direct insertion sorting and binary insertion sorting

Source: Internet
Author: User

1. Direct Insert Sort

The process of directly inserting a sort can be understood as a fixed-length array that is divided into two sets, that is, sorted and unordered.

Initially, the sorted collection is empty, and the unordered collection is the entire array. When a sort begins to insert an object, the sorted collection element number is 1, the number of unordered collection elements is reduced by 1, and the insertion process is repeated until the unordered collection is emptied, and the sorted collection is the final result. Such as:

C + + implementation is as follows, in order to make the program can be a variety of basic data types can be sorted, using the template class, Notice that the template class declaration and member function implementation must be in the same CPP file, cannot separate!!

1 #ifndef Insertsort_h2 #defineInsertsort_h3#include <vector>4#include <iostream>5 usingstd::cout;6 usingStd::endl;7 usingstd::vector;8 9Template <typename t>Ten classInsertsort One { A Private: - unsigned len; -Vector<t>list; the  Public: -Insertsort (vector<t>_list, unsigned _len) -     { -          for(Unsigned i =0; i < _len; ++i) list.push_back (_list[i]); +          This->len =_len; -     } +     voidInsertsort () A     { at T Insertnum; -          for(Unsigned i =0; i < Len; ++i)//Insert number for Len Times -         { -Insertnum = List[i];//The number is to be inserted -unsigned j =i; -              while(J && Insertnum < list[j-1])//Find the position to insert the target number in in             { -LIST[J] = list[j-1]; to--J; +             } -LIST[J] =Insertnum; the         } *     } $     void  out()Panax Notoginseng     { -          for(Unsigned i =0; i < Len; ++i) the         { +cout << List[i] <<" "; A             if((i+1)% -==0) cout <<Endl; the         } +cout <<Endl; -     } $ }; $ #endif

2. Two-point insertion sort

Because the direct insertion sort is very inefficient when searching for the insertion position, especially for large arrays, the binary insertion sort is called binary insert Sort, and the binary insertion sort is using binary lookup to find the location to insert .

The following shows the position of the binary lookup method to determine the number 35 in the target array:

    • First, determine the median position of the target array as 45 digits.
    • Because of the > 35, the descending array binary and takes the left half molecule array as the search target, the middle element of the left half is 23
    • Since the < 35, so binary, select half of the sub-array as the search target
    • And the < 36, so that 35 of the position between 23 and 36.

The C + + implementation is given below:

1 #ifndef Binaryinsertsort_h2 #defineBinaryinsertsort_h3#include <vector>4 usingstd::vector;5Template <typename t>6 classBinaryinsertsort7 {8 Private:9     intLen;TenVector<t>list; One  Public: ABinaryinsertsort (vector<t> _list,int_len) -     { -          for(inti =0; i < _len; ++i) list.push_back (_list[i]); the          This->len =_len; -     } -      voidBinaryinsertsort () -      { +         intMiddle; -          for(inti =0; i < Len; ++i) +         { AT Insertnum =List[i]; at             intleft =0; -             intright = i-1; -              while(left <= right)//Find the insertation position with binary search -             { -Middle = (left + right)/2; -                 if(Insertnum >List[middle]) inleft = middle +1; -                 Else toright = Middle-1; +             } -              for(intj = i; J > left; --J) List[j] = list[j-1]; theList[left] =Insertnum; *         } $      }Panax Notoginseng     void  out() -     { the          for(Unsigned i =0; i < Len; ++i) +         { Acout << List[i] <<" "; the             if((i+1)% -==0) cout <<Endl; +         } -cout <<Endl; $     } $ }; - #endif

3. Test run

1#include"InsertSort.h"2#include"BinaryInsertSort.h"3#include <vector>4 using namespacestd;5 6 Constunsigned numele =8;7 intData[numele] = {1,5,7,3,8,2,6,4};8 9 Ten intMain () One { Avector<int>TestData; -      for(Unsigned i =0; i < Numele; ++i) testdata.push_back (Data[i]); -     //insertsort<int> Test (TestData, numele); the     //Test.insertsort (); -binaryinsertsort<int>Test (TestData, numele); - Test.binaryinsertsort (); -Test. out(); +     return 0; -  +}

4. References

murmured: The principle of C + + data structure and classical problem solving

C + + implementation of direct insertion sorting and binary insertion sorting

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.