Sort--Select sort

Source: Internet
Author: User

Second, choose the sort
? thought: Each trip selects the lowest-key record from the record sequence to be sorted to the first 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

① Simple sort of selection
1, the basic idea: in the group of numbers to be sorted, select the smallest number and the first position of the number of exchanges, and then in the remaining number to find the smallest and second position of the number of exchanges, so loop to the penultimate number and the last number comparison.
  
2. Example


3. Java implementation
Copy Code
Package com.sort;

Not stable
public class Simple selection sort {

public static void Main (string[] args) {
Int[] a={49,38,65,97,76,13,27,49,78,34,12,64,1,8};
System.out.println ("Before sorting:");
for (int i = 0; i < a.length; i++) {
System.out.print (a[i]+ "");
}
Simple sort of selection
for (int i = 0; i < a.length; i++) {
int min = a[i];
int n=i; Index of the minimum number
for (int j=i+1;j<a.length;j++) {
if (a[j]<min) {//Find the smallest number
min = A[j];
n = j;
}
}
A[n] = A[i];
A[i] = min;

}
System.out.println ();
System.out.println ("After sorting:");
for (int i = 0; i < a.length; i++) {
System.out.print (a[i]+ "");
}
}

}
Copy Code
4. Analysis

Simple selection sorting is an unstable sort.

Time complexity: T (n) =o (n2).

② Heap Sorting

1. Basic ideas:

Heap sorting is a sort of tree selection, which is an effective improvement on direct selection sorting.

Heap definition: A sequence with n elements (h1,h2,..., HN), when and only if satisfied (hi>=h2i,hi>=2i+1) or (hi<=h2i,hi<=2i+1) (i=1,2,..., N/2) is called a heap. Only the heap that satisfies the former condition is discussed here. As can be seen from the definition of a heap, the top element of the heap (that is, the first element) must be the largest (large top heap). A fully binary tree can represent the structure of a heap visually. Heap top is the root, the other is Zuozi, right subtree.

Thought: Initially, the sequence of numbers to be sorted is considered to be a two-fork tree of sequential storage, adjusting their storage order to make it a heap, when the heap has the largest number of root nodes. The root node is then exchanged with the last node of the heap. The number of fronts (n-1) is then re-adjusted to make it a heap. And so on, until there are only two nodes of the heap, and exchange them, and finally get an ordered sequence of n nodes. In terms of algorithm description, heap sequencing requires two processes, one is to build the heap, and the other is the last element of the heap to exchange the position. So the heap sort has two functions. One is to build the seepage function of the heap, and the second is to call the function of the infiltration function to realize the sorting.

2. Example

Initial sequence: 46,79,56,38,40,84

Build heap:

Swap, kicking the maximum number out of the heap

And so on: The last two remaining nodes in the heap are exchanged, kicked out, and sorted.

3. Java implementation

Copy Code
Package com.sort;
Not stable
Import Java.util.Arrays;

public class Heapsort {
public static void Main (string[] args) {
Int[] a={49,38,65,97,76,13,27,49,78,34,12,64};
int arraylength=a.length;
Loop Build Heap
for (int i=0;i<arraylength-1;i++) {
Build a heap
Buildmaxheap (a,arraylength-1-i);
Swap heap top and last element
Swap (a,0,arraylength-1-i);
System.out.println (Arrays.tostring (a));
}
}
Build a large top heap from 0 to lastindex for the data array
public static void Buildmaxheap (int[] data, int lastIndex) {
Starting at the parent node of the lastindex node (the last node)
for (int i= (lastIndex-1)/2;i>=0;i--) {
K Save the node being judged
int k=i;
If the child node of the current K-node exists
while (K*2+1<=lastindex) {
Index of the left child node of the K-node
int biggerindex=2*k+1;
If Biggerindex is less than lastindex, that is, the right child node of the K node represented by biggerindex+1 exists
if (Biggerindex<lastindex) {
If the value of the right child node is large
if (Data[biggerindex]<data[biggerindex+1]) {
Biggerindex always records the index of a larger child node
biggerindex++;
}
}
If the value of the K-node is less than the value of its larger child nodes
if (Data[k]<data[biggerindex]) {
Exchange them
Swap (Data,k,biggerindex);
Assign the Biggerindex to K, start the next loop of the while loop, and re-guarantee that the value of the K-node is greater than the value of its left and right child nodes
K=biggerindex;
}else{
Break
}
}
}
}
Exchange
private static void Swap (int[] data, int i, int j) {
int tmp=data[i];
DATA[I]=DATA[J];
data[j]=tmp;
}
}
Copy Code
4. Analysis

Heap ordering is also an unstable sorting algorithm.

Heap sorting is better than simple selection of the reason for sorting:

In order to select the smallest record of a keyword from R[1..N], a n-1 comparison must be made, and then the smallest record of the keyword is selected in R[2..N], and n-2 is required. In fact, there are a number of comparisons that are likely to have been made in the previous n-1 comparison, but because the previous n-2 did not retain these comparisons, the comparison was performed repeatedly when the latter was sorted.

Heap sorting saves some of the comparison results by using a tree structure, which reduces the number of comparisons.

The worst time complexity for heap sorting is O (Nlogn). The average performance of the heap is closer to the worst performance. Heap sorting is not appropriate for files with fewer records due to the number of comparisons required to build the initial heap.

Sort--Select sort

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.