插入排序的改進(jìn)(The improvement of insertion sort)_第1頁
插入排序的改進(jìn)(The improvement of insertion sort)_第2頁
插入排序的改進(jìn)(The improvement of insertion sort)_第3頁
插入排序的改進(jìn)(The improvement of insertion sort)_第4頁
插入排序的改進(jìn)(The improvement of insertion sort)_第5頁
已閱讀5頁,還剩29頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、插入排序的改進(jìn)(The improvement of insertion sort)Insertion sortInsertion sort is by several (usually N-1) round into the process to get all the data in the composition series.Each round is inserted to the (incomplete) insert a new data element in the sequence, the original sequence has been expanded. The f

2、irst round of the insert only contains a sequence of data elements.The cost of insertion sort operation is mainly used to find new insertion position (check) and insert a new element to the location (plug). Therefore, to reduce the time complexity of the efforts are also around to check and plug.1 d

3、irect exchange insertion sort#includeUsing namespace std;Ifstream fin (count4.in);OFSTREAM fout (count.out);(main)int n, a200001, I, J, t=clock ();Finna1;For (i=2; iai;For (j=i; j1; j-)If (ajaj-1) swap (aj, aj-1);Else / / break;Fout (clock) -t (/1000.0endl);For (i=1; i=n; i+)Foutai;Foutendl (clock)

4、-t (/1000.0);Just look at the swap (aj, aj-1) of a sentence and ignore the comment line, this code is like bubble method, remove the comment line / /, more easily understand the for cycle J is inserted into ai from a1 to ai-1 ordered the original column, but also significantly speed.2 by lookout sea

5、rch, using loop interchange insertionWe know that the exchange of two subscript variable operation is time consuming, large range of continuous change to reduce the data amount of the mobile exchange cycle will exchange. Put itself as lookout can also limit range and to determine the cycle to determ

6、ine the size of the keyword as a judge, this saves some judgment operation.#includeUsing namespace std;Ifstream fin (count4.in);OFSTREAM fout (count.out);(main)int n, m, a200001, I, J, K, t=clock ();Finna1;For (i=2; iai;For (j=1; ajj; k-) ak=ak-1;Aj=m;Fout (clock) -t (/1000.0endl);For (i=1; i=n; i+)

7、Foutai;Foutendl (clock) -t (/1000.0);With these two kinds of improved speed will be twice as likely to improve.3 of the combined cycle, reduce judgment, circulation and exchangeExamples: NOIP improve the combination and fruitTwo, with fruit (fruit.pas / DPR / C / CPP)descriptionIn an orchard, a lot

8、has all the fruit down, and according to the different kinds of fruit into different pile. A lot of decided to put all the fruit of a pile of synthesis.Every time the merger, a lot can be merged together two piles of fruit, and the weight of the physical consumption is equal to two pile of fruit. We

9、 can see that all the fruit after n-1 times after the merger, on the left a pile of. When a lot of fruit in the combined strength equal to the total consumption of each of the merger and the physical consumption.Because we have to spend great efforts to bring these fruit home, so a lot of fruit in t

10、he combined time as much as possible to save energy. Assume that each fruit weight is 1, and the number of species known to fruit and the number of each type of fruit, your task is to design a combined order of programs, so a lot of physical least cost and physical output of the minimum cost value.F

11、or example, there are 3 kinds of fruit, followed by the number 1, 2, 9. The first 1, 2 heap merge, a new pile number is 3, the physical cost is 3. Then, the new heap merged with the original third, and get a new pile, the number was 12, the physical cost is 12. So a lot of total expenditure of physi

12、cal =3+12=15. It can prove the value 15 as the minimum physical.The input file The input file contains two lines of fruit.in,The first line is an integer n (1 = n=10000), said the number of species of fruit. The second line contains n integers, separated by spaces, the I integer AI (1 = ai=20000) is

13、 the number of the I fruit.The output file The output file fruit.out includes a line, this line contains an integer, which is the least power consumption value. The input data to ensure that the value is less than 231.Sample input Three129Sample output Fifteensize dataFor 30% of the data, to ensure

14、that there is n=1000:For 50% of the data, to ensure that there is n=5000;For all the data, ensure that there are n=10000.The problem with the Huffman algorithm, each for at least two piles of fruit with. Can first sort, which is equivalent to remove two piles of fruit and the least is a bunch of new

15、 fruit. Then put this pile of fruit is inserted into the sequence of. This operation is repeated until all the fruit into a heap.Reference code#include Using namespace std;Int a10001=2147483647;Void insert (int x, int y)While (axn;For (int i=1; iai;Insert (i-1, ai);For (int i=n-1; i0; i-)S+=ai+=ai+1

16、;Insert (i-1, ai);Couts;System (pause);Return 0;Insert the 4 road twoThe above 3 are square calculation description. As the two road selection sort, if we find the order of ranking two road, will reduce the amount of calculation.Exercise: programming complete two insertion sort. I input the amount o

17、f data N and M boundary value, the second row is N to be sorted data. For less than the boundary value data of the original insertion sort, but not less than the cut-off value of inserting data from an to a1 reverse.Here the boundary value if the N data is split, two road will get the best treatment

18、 effect, but the distribution is not uniform enough, the effect will be discounted. Please evaluate the 100 data distribution for the following three kinds of calculation pattern of quantity:505030701090Can distribution according to the average two to describe the insertion order? The following COUN

19、T contest as an example,NOIP group of 2007The first problem: count statisticsEnter a number of n (n=200000) and n natural numbers (each number are not more than 1.5*109), the statistics of the number of these natural number please each appear, according to the order from small to large output. The i

20、nput data to ensure the same number of not more than 10000.Sample input:EightTwoFourTwoFourFiveOne hundredTwoOne hundredSample output:2342511002The two-way Chain to describe two way insertion#include #include Using namespace std;Ifstream fin (count.in);OFSTREAM fout (count.out);Struct nodeInt Data,

21、Count;Node *previous, *next;Void ins (node *q, int x)Node *p=new node;P-Data=x;P-Count=1;P-previous=q;Q-next-previous=p;P-next=q-next;Q-next=p;Return;Int (main)Int (clockt=clock);Node L, m, R, *z, *t;Z=&m;Int x, N, I, b=0;Finnm.Data;M.Count=1;L.Data=-1;R.Data=1500000001;L.next=r.previous=z;M.previou

22、s=&l;M.next=&r;For (i=1; ix;if (x = = z - data)z - count + +;else if (x data)t = z;do t = t - previous;while (x data);if (x = = t - data)t - count + +;elseins (t, x);b -;elset = z;do t = t - next;while (x t data);if (x = = t - data)t - count + +;elseins (t - previous, x);b + +;if (b previous;b = 0;e

23、lse if (b 1)z = z - next;b = 0;t = l.next;while (t! = & r)agricultural development data count next;cout (clock () - clockt) / 1000.0;system (pause);return 0;5 用鏈?zhǔn)讲迦胼o助哈希解決count如果把count中的數(shù)據(jù)范圍由0 x n;for (i = 0; i n; i + +) a, b a + +;for (i = 1; in 0) agricultural development in b in final;agricultural

24、 development n;node * p, * q;for (i = 0; i = 150000; i + +)h i = new node;for (i = 1; in temp;p = h temp / 10000;while (p - next! = 0 & & temp p - next data)p = p - next;if (p - next! = 0 & & p - next data = = temp)p - next count + +;elseq = new node;q - = = p - next;p - next = q;q - data = temp;q -

25、 count = 1;for (i = 0; i next! = 0)p = h in - next;while (p! = 0)agricultural development data count next;system (pause);return 0;在4和5中, 插入都避免了成片搬, 把 插 的代價降低到了極點, 但 查 的代價仍然很大.為了降低 查 的代價, 可以考慮折半查找.6 折半查找的插入排序仍然以count為例, 下面的折半插入描述時間復(fù)雜度較低.# include cstdlib 1) m =(L + R)/ 2;如果(xM。數(shù)據(jù))R = M;其他的L = M;回我;int main(int argc、argv char * ) 0 。數(shù)據(jù)= 1;一個 1 。數(shù)據(jù)= 2147483647;int n,i,x,t,d = 1;T = sizeof(節(jié)點);鰭 n;y;為(i = 1;i X;Y = SE(0,d,x);如果(一個年。數(shù)據(jù)= = X)一個年數(shù)+ +;其他的 memcpy(+ y + 2,+ y + 1,T *(D-Y);D + +;一個 y + 1 數(shù)據(jù)= x;一個 y + 1 。數(shù)= 1;為(i = 1;i D;i+)四一個我。數(shù)據(jù)”一個我數(shù) endl

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論