Title Description Enter n integers to find the smallest number of K. For example, enter 4,5,1,6,2,7,3,8 these 8 numbers, then the smallest 4 number is 1,2,3,4,.//Heap Sort#include #include#includeusing namespacestd;classsolution{ Public: voidHeapadjust (vectorint> a,intSintN) {intRc=A[s]; for(intj=2*s+1; j1; j=j*2+1) { if(j1a[j]>a[j+1]) J++; if(rcA[j]) Break; Else{A[s]=A[j]; S=J; }} A[
2016_2_25_heap.cpp:defines the entry point for the console application.//#include "stdafx.h" //adjust the tree heap so that the maximum value is placed at the root node //i is the location of the array element and the tree //length void heapadjust (int Array[],int i,int nlength)//This is done only once { int nchild;//This is the child node of I int ntemp; for (; 2*i+1 { //left Dial Hand node location =2* (parent node location) +1 //
exceed 1G. First step borrowingHashPre-processing of this batch of massive data: first(Maintain aKeyto beQueryString,Valuefor theQuerythe number of occurrences, i.e.Hashmap (Query,Value), each time you read aQueryif the string is notTable, the string is added, and theValuevalue is set to1, if the string is in theTable, add a count of the string. In the end weO (N)(Nto be1Be sure to iterate through the entire array once and for each of the statistics officesQueryThe number of occurrences) is use
HeapSort.cpp: Defines the entry point of the console application. #include "stdafx.h" #include Reference URL: http://www.cnblogs.com/ahalei/p/3783543.htmlHttp://www.cnblogs.com/ahalei/p/3792155.htmlSpecific code explanation look at the above two URLs on the line, and then to "Ah Halle" praise one.Look at the data structure write code (63) Heap Sort
; - for(intI=size;i>1; i--) the { -TMP = arr[0];Wuyiarr[0] = arr[i-1]; thearr[i-1] =tmp; -Max_heapify (arr,--size,1); Wu } - } About $ //return max key of Arr - intTopintArr[],Const intsize) - { - returnarr[0]; A } + the //return max key of arr and delete it - intPopintArr[],intsize) $ { the if(Size 1)returnERROR; the intmax = arr[0]; thearr[0] = arr[--size]; theMax_heapify (Arr,size,1); - returnMax; in } the the //increase the key of I About voidHeap_increase_key
In the future, I want to put my usual writing here. If there is any improvement, please point out.#include"stdafx.h"#includeusing namespacestd;int*p;intLength//Save the number of elements that need to be sortedvoidInit () {//initialized. cout "Enter the number of elements to sort"Endl; CIN>>length; P=New int[length]; cout"Enter the elements you want to sort"Endl; for(inti =0; i ) {cin>>P[i]; }}voidShow ()
less (after the bounds of demarcation need to be judged beyond the bounds) if(n[q].now>n[q+1].now) {q++; } Else if(n[q].now==n[q+1].nown[q].no>n[q+1].no) {q++; } } //if (n[q].now>n[p].now) {//who the hell is the comparison object! if(n[q].now>A.now) { Break; } Else if(n[q].now==a.nowa.no//is a in the comparison Break; } N[p]=N[q]; P=q,q=2*p; } N[p]=A;}voidMakeminheap (intLen) { for(intJ= (len-1)/2; j>=1; j--)
/*recently in the C language version of the data structure, C usage is really difficult, so according to the meaning, in the C language to write a JavaScript version of the three sorting Method! For everyone to learn together and refer to the following difficult merge sort, and quick sort, later, say nonsense, direct edge code side explain! */Heap
#include using namespacestd;voidHeapadjust (intArr[],intSintM//large Top Heap, m represents the last digit position, s represents the first position, is subscript{ intCur=s; intindex; inttmp=Arr[cur]; while(cur*2+1m) {index=cur*2+1;// Left Child if(index1]) {index++;// Right Child } if(arr[cur]Arr[index]) {Arr[cur]=Arr[index]; Arr[index]=tmp; Cur=index; } Else { Break; } }}voidHeapsort (intArr[],in
The problem is not to ask for the smallest dictionary order ... Tore it up for half an hour to find something wrong t tThis question is able to make small as far as possible before, regardless of dictionary order ... For example, 1 can be in front of 2 must be in front of 2 ...It is clear that the topology is sorted first, so that the small one is converted to make the big one as far back as possible, so it is actually irrelevant to the dictionary order. Then build a reverse diagram, with the
Use heap sorting to find the small number of k in list, with a small top heapdef min_heap_k(nums, topk): def siftdown(nums, e, begin, end): i = begin j = 2*i + 1 while j Linked list reversal Today's headline intern interview common topics Heap sort topk, reverse list
array=[0,30,20,80,40,50,10,60,70,90]# to sort sequences #i=4-1 or 1#n =len (array) total=len (array) -1# resize to a large top heap, I refers to the node from which to start the adjustment, n represents the total number of elements to be sorted def adjust_heap (N,i,array): #length =len (array) # Print_tree (Array) whilei*2This article is from the "12064120" blog, please be sure to keep this source http://12
ArticleDirectory
1. Select sort
2. Heap sorting
3. Efficiency
1. Select sort
The sorting principle is to first select the smallest number, exchange with the first number, and then switch from the second number to the second number ,......
Def
Selection_sort
(
Data
):
For
I
In
Range
(
Len
(
Data
)
-
1
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.