The first is the Eightalgorithms.java file, the code is as follows:Import java.util.arrays;/* * Implements eight commonly used sorting algorithms: Insert sort, bubble sort, select sort, Hill sort * and quick sort, merge sort,
The relationship between 8 sorts:
1, direct insertion sort
(1) Basic idea: In the set of numbers to be sorted, suppose that the front (n-1) [n>=2] number is already a row
In a good order, now you have to insert the nth number into the preceding ordered number so that the number of n
It's a good order, too. Repeat the loop until all the order is sorted.
(2) Example
(3) Implement with Java[Plain] View plain copy packagecom.njue; publicclassinserts
size of the left and right subtree.So, to design an ascending sort, we need to build a maximum heap.1) Build a maximum heap.2) for the 1th time, the element of the root node is exchanged with the last node element of the heap, so that the maximum element is guaranteed to be at the last node of the
Reference: seven-heap and heap sequencing of vernacular classical algorithm series1#include 2#include 3#include 4 using namespacestd;5 6 namespaceHeap {7 8 //Insert an element to a heap9 voidInserttoheap (vectorint> H,intx) {Ten h.push_back (x); One intn =h.size (); A - inti = n-1; - intp = (I-1) /2;//Parent the inttemp =x; - while(P >=0 I!
Sort the arbitrarily populated array in piles, then delete the heap, because the heap is always the largest in the current heap, and according to this feature, you can get an ordered array(in piles) starts from the last parent node and adjusts downward to the topmost parent node.(delete
Read, think, write code!/********************************************* * [emailprotected] * Blog:http://blog.csdn.net/hustyangju * Title: Heap sequencing Implementation, in addition to implement interface: Take heap maximum and delete, heap insert * idea: The heap is implemented on the original site of the sequential a
An algorithm is a set of well-defined rules that are used to solve a problem within a finite step. Popular point, that is, the process of computer problem solving. In this process, whether the formation of a problem-solving ideas or writing programs, are in the implementation of some kind of algorithm. The former is the algorithm of inference implementation, the
Brief introduction
Heap for sorting algorithm is a more common data structure, I use the Java language to implement this algorithm
First, we need to know the structure of the heap of the form, in fact, is a special two-fork tree. But this binary tree has certain characteristics, in addition to the complete binary tree,
The problem is to give two sequences, the first one is the pre-order, the second is in the sort, and the next is the sequence in which it is sorted, sorted, and output.The feature of the insertion sort is that the front is arranged from small to large, followed by the same as the original sequence.The feature of heap sorting is that the following is the largest n
node I does not satisfy the nature of Dagen, but its left and right subtree are legal large root heap, then call Maxheadfly //// adjust to I as root node can constitute a valid large heap, and a node of the left and right son node number is greater than its /// So we can call the heap's adjustment function sequentially from the numbered N/2--1 node, then we can get a big
Select Sortthought: Each trip selects the lowest-keyword record from the sequence of records to be sorted and places it in the top position of the sorted table until all rows are completed. key issue: Find the minimum key code record in the remaining sequence of records to be sorted. Method:– Direct selection of sorting– Heap sorting(1) Simple selection of sorting1, the basic idea: in the group of numbers to be sorted, select the smallest number and t
Heap sorting is a sort of tree selection, which is an effective improvement on direct selection Sorting.Basic Idea:The heap is defined as follows: a sequence with n elements (k1,k2,..., kn), when and only if it satisfiesis called a heap. As can be seen from the definition of a heap
Heap Sort
Inserts all the elements into a priority queue that inserts a lookup for the smallest element, repeating the call to delete the smallest element, so that all elements are sorted in ascending order.
Two stages of heap sequencing
Heap Order
Data order
All elements are arranged in ascendi
Select sortHow it works: each time the largest or smallest element is selected from the data element to be sorted, it is stored at the beginning of the sequence until all the data elements to be sorted are exhausted.Voidselectsort (int*a,size_tsize) {assert (a); for (inti=0;iHeap Sortonly needAscending Voidadjustdown (int*a,size_tsize,introot) { ASSERT (a);intchild=root*2+1; while (childThis article from "Write Quality or Low" blog, declined reprint!Sort
Heap Sorting algorithm
First, the basic characteristics of heap sorting algorithmComplexity of Time: O (N*LGN)Worst: O (N*LGN)Complexity of Space: O (1)Not stable.
Heap sorting is a sort of selection algorithm, which is independe
001
#include
002
#include
003
int main ()
004
{
005
int l[11]={0,70,38,65,97,76,93,1,15,68,64}; 0 do not sort just as sentinels, or temporarily swap space for use.
006
int i=0;
007
printf ("Unordered pre-sequence: \ n");
008
for (i=1;i
009
{
dealing with static data.For example: Select the top 100 in 100 million elements?Abstract: Select the first m element in n elements?The easy-to-think solution is to sort the n elements and then pick the first m elements. The time complexity is O (NLOGN).But if we use the priority queue, then our time complexity will be up to O (NLOGM).If the data to be processed is large, then the use of the priority queue will make the whole process more than 10 tim
analysisThe time of the heap ordering is mainly composed of the time overhead of the two parts: the initial heap and the duplicate rebuild heap, all of which are implemented by calling Heapsort.
The worst time complexity for heap sorting is O (NLGN). Average time performance of
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.