Sort
Time Limit: 6000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)Total submission (s): 27771 accepted submission (s): 8400
Problem description gives you n integers. Output The first m in the ascending order. There are two rows of test data in each input group. The first row has two numbers n, m (0 5 33 -35 92 213 -644Sample output
213 92 3 knowledge points: heap, STL#include
This is a created
article in which the information may have evolved or changed. "' Gopackage mainimport (" FMT ") func main () {array2: = []int{3, 2, 1}heapsort (array2) fmt. Println ("Heapmy", Array2)}func heapsort (array []int) {EP: = (len (array)-1) >> 1fmt. Println (EP) for I: = EP; I >= 0; i--{heapt (array, I, Len (array)-1)}for I: = Len (array)-1; i > 0; i--{array[0], array[i] = Array[i], array[0]heapt (array, 0, i-1)}}func heapt (array []int, start int, end int) {le: = start * * + 1re:
than base from the front - $ if(I >= J) Break;//Note that the equals sign $ - swap (Arry[i], arry[j]); - } the -Swap (Arry[lo], arry[j]);//determine the location of JWuyi the returnJ; - } Wu - voidQsort_3way (vectorint> arry,intLointhi) About { $ if(Lo >= hi)return; - - intLt=lo, i=lo+1, gt=Hi; - intPivot =Arry[lo]; A + while(I GT) the { - if(arry[i]pivot) $Swap (arry[lt++], arry[i++]);//i++, should be Lt->pivot, after Exchange lt++, pivot, i+
The process of bubbling sorting is to first compare the keywords of the first record with the keywords of the second record, and, in reverse order, Exchange two records and then compare the keywords for the second record and the third record. And so on, until the n-1 record and the nth record's keywords have been compared. The above process is called the first bubbling sort, then the second trip to the previous n-1 the same operation, ...The quick
] forIinchRange (len (data_set)):#Big Circle determines the number of rounds that we chooseMinindex =I forJinchRange (I+1,len (data_set)):#a small circle is the number of times we compare each time we choose #//If the subsequent element is smaller than the element I chose, swap the position ifDATA_SET[J] Data_set[minindex]: Minindex=J Temp=Data_set[i] Data_set[i]=Data_set[minindex] Data_set[minindex]=TempPrint(Data_set)" "The basic idea of inserting
The example in this paper describes how Python implements heap ordering. Share to everyone for your reference, as follows:
Heap sorting is one of the basic sorting methods, similar to a merge sort rather than an insert sort, which runs at O (Nlogn), like an insert
interchange Total-= 1 if total = = 2 and Array[total] >= Array[total-1]: Break Heap_adjust (Total,1,a Rray) return Arrayprint_tree (sort (total,origin))Five, algorithm analysis1, the use of a heap nature of a selection of sorting, at the top of the heap to select the maximum or minimum value2. Complexity of TimeThe time complexity of
Heap: One of the priority queues that use the priority queue to increase the object in any order, and to find (or possibly remove) the smallest element at any time (possibly at the same time as the object is incremented), which is more efficient than the method used for min in the list.Python does not have a separate heap type, only a module that includes some heap
decremented to ensure that the maximum value is not reordered returnHeapif __name__=='__main__': #A = eval (raw_input (' Please enter a list to be sorted \ n '))A = [Random.randint (1, 2000) forIinchRange (1000)] B=Copy.deepcopy (a) B_begin=Datetime.datetime.now () sort (b) B_end=Datetime.datetime.now ()Print 'My method Use%s'% (B_end-b_begin). Total_seconds () C=Copy.deepcopy (a) C_begin=Datetime.datetime.now () heapq.heapify (c) C_end=Datetim
Sort Algorithm Analysis [5]: Merge and sort (with Python C ++ code), and algorithm analysis with python
Merge and sort: combines two sorted sequences into one.Algorithm principle
First look at the dynamic diagram:
The algorithm is described as follows:
Algorithm Implementa
. The number of child nodes of the left and right subtree of the balanced two-fork tree node is the same. In the implementation of the heap, we use the structure of the "complete binary tree" to achieve the approximate "balance". A complete binary tree, which means that each internal node tree reaches its maximum value, except that the last layer can only be missing several nodes on the right. Figure 1 shows a complete binary tree.
Figure 1: Complete
= 2 [0, 9, 5, 6, 2, 3]i = 1 [0, 9, 2, 6, 5, 3]i = 0 [0, 2, 3, 6, 5, 9]
The following code is a fully binary heap implementation.
def insert(self,k): self.heapList.append(k) self.currentSize = self.currentSize + 1 self.percUp(self.currentSize) def percDown(self,i): while (i * 2)
You canO(n)It seems incredible that the binary heap can be generated under the overhead, which proves that it is beyond
One, counting sortProfile: Time complexity O (n), spatial complexity O (k), K is the range of values of the input sequence (maximum-minimum), is stable. The count sort is generally used for a relatively small range of known input values, such as the sorting of height and weight information for company employees.Ideas: The input array A is {3,5,1,2,4,3}, and the range of values is 5 buckets, and the sequence number is 1,2,3,4,5. When you load a buck
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.