"Data_structure Note 3" Sorting algorithms common to the insertion of three algorithms __ algorithm

Source: Internet
Author: User
/**************************************************************************************************************
		         File Description: "1" internal sort: refers to in the sort period of the elements are stored in memory of the sort "2" external sort: refers to in the sort of the elements can not be stored in memory at the same time, must be in the process of ordering, as required, constantly moving between the inside and outside the
		        The stability of the sort "3" Sorting algorithm "4" the performance of the internal sorting algorithm depends on the algorithm "time Complexity" and "space complexity", while the time complexity is generally determined by the "comparison" and "move" the number of times to determine the detailed description: "1" Insert sorting algorithm (1) Direct insertion sort (2) Binary insertion sort (3) Hill sort (shell sort) "2" Interchange class sort (1) bubble sort (2) Quick Sort "3" Select Class Sort (1) simple Select Sort (2) heap Sort "4" two-way merge sort "5" cardinal sort Insert Sort: "1" insertion sort is a simple and intuitive sort algorithm, whose basic idea is to insert a record to be sorted each time by its keyword size into a previously sorted sequence.
		column until all of the records are inserted complete. "2" by inserting a sort of algorithm idea can be expanded out of three specific sorting algorithms: (1) Direct insertion sort (2) Binary insertion sort (3) Hill sort *********************************************** /#include <iostream> using

namespace Std; /************************************************************************************************************** Function prototypes: "FunctionsTemplate "Template<typename elemtype>void insertsort (elemtype arrayt[],int ilength)" Applicability "Direct insert sort algorithm" applies to sequential and chained storage of the linear table. and generally applicable to a small number of records and the basic order of the linear table function Description: "1" to record array Arrayt do directly insert Sort "2" ilength the number of records to be sorted in the array directly insert the sort algorithm: "1" from a spatial point of view, the direct insertion sort algorithm simply
		To set up monitoring Sentinel tempt to help implement it.
		"2" from the point of view of time, the main time spent on keyword comparison and elements of mobile operations. The "3" "Insert sort Algorithm" is not arbitrary and is more suitable for cases where the number of records to be sorted is small and basically orderly (1) fewer records (2) Basic ordered "4" Direct insertion sort using "in-place" *********************** /template                               <typename elemtype>void insertsort (elemtype arrayt[],int ilength) {elemtype tempt;
	"1" defines a surveillance sentinel int j = 0;
		for (int i=1;i<ilength;i++) {tempt = Arrayt[i];
		j = i-1;
			while (Tempt<arrayt[j]) {arrayt[j+1] = arrayt[j];
		j = j-1;
	} arrayt[j+1] = tempt; } 
}
/************************************************************************************************************
   Function Prototypes:     function Template: Template<typename elemtype>void binsort (elemtype arrayt[],int ilength) function Description: "1" to record array arrayt do "binary insert row Order "2" ilength number of records to be sorted in array algorithm thought: "1" from the discussion of the search algorithm, we can see that the performance of "ordered order table" is better than sequential lookup for binary lookup. Therefore, the idea of "binary lookup" can be used to determine the insertion position in an orderly record, the corresponding sorting algorithm is called "Binary insert Sort Algorithm" "2" adopts binary insert sort algorithm, "3" adopts binary insert sort algorithm, which can reduce the number of keyword comparisons. Without inserting an element, the maximum number of times to compare is the depth of the binary decision tree ************************************************************************************
/Template<typename elemtype>void binsort (elemtype arrayt[],int ilength)

	{Elemtype tempt;
	int j = 0;
	int ilow = 0;
	int ihigh= 0;

	int imid = 0;

		for (int i=1;i<ilength;i++) {tempt = Arrayt[i];
		Ilow = 0;
		Ihigh = i-1;
			while (Ilow<=ihigh) {IMiD = (Ilow+ihigh)/2;
			if (Tempt<arrayt[imid]) {ihigh = iMid-1;
			else {ilow = imid+1;
		}}//while for (j = i-1;j>=ilow;j--) {arrayt[j+1] = arrayt[j];
	} Arrayt[ilow] = tempt; }//for i}/*****************************Function Prototypes: "Function Templates" template <typename elemtype>void binsort (elemtype arrayt[],int ilength) "Applicability" Hill sort algorithm only applies when the linear table is "sequential storage" condition function Description: "1" on the number of records
		Group Arrayt do "binary insert Sort" "2" ilength number of records to be sorted in the array Hill sort: "1" from the previous explanation, it is not difficult to see that the "Direct insertion sort algorithm" applies to "Basic ordered sort table" and "small amount of data sorting table". "2" is based on these two points.
The 1959 D.l.shell proposed Hill sort, also called "Narrowing the incremental sort".
		Hill sort of thought: "1" first divides the "to sort table" into a special child table with several shapes such as L[i,i+d,i+2d,i+3d,........ I+KD].
		"2" directly to "insert Sort" "3" when the elements in the entire table are "basically ordered", once again, make a "direct insert sort" algorithm for the whole record: "1" "Applicability": Hill sorting algorithm only applies when the linear table is "sequential storage". The time complexity of the "2" Hill sorting algorithm is generally considered O (Nlogn), in the worst case for O (n*n) ********************************************************************* /Template<typename elemtype>void Shellinsertsort ( Elemtype Arrayt[],int ilength) {for (int d=ilength/2;d>=1;d=d/2)//"1" setting (starting increment; cyclic condition; small increments The rule of the quantity {for (int k=0;k<d;k++)//"2"With an increment of N, there is an n subsequence, traversing all the sub sequences {//======================================================================================  ===================== for (int i=k+d;i<ilength;i+=d)//"3" Inserts the "sort" {int itemp for each subsequence
				= Arrayt[i];
				int j = i-d;
					while (J>=k&&itemp<arrayt[j]) {arrayt[j+d]=arrayt[j];
				j = j-d;
			} Arrayt[j+d] = itemp; }//i//========================================================================================================= = = =}//for k} int main (int argc,char* argv[]) {int arrayt[15] = {22,32,44,34,56,21,24,345,213,234,245,127,113,11
	9,933};   
	Insertsort<int> (arrayt,15);
	Binsort<int> (arrayt,15);
	Shellinsertsort<int> (arrayt,15);
	for (int i=0;i<15;i++) {std::cout<<arrayt[i]<<std::endl;
	} std::system ("pause");
return 0; }

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.