Data structure Sorting series detailed six tree-shape selection sort

Source: Internet
Author: User
Tags comparison

This blog goes on to say sort of select one of the class sort: Tree Select sort

In the simple selection of the sort, each comparison does not use the results of the last comparison, so the time complexity of the comparison operation is O (n^2), want to reduce the number of comparisons, you need to compare the size of the relationship in the process to save. Tree selection Sorting is an improvement to the simple selection sort.

Tree selection Sort: Also known as tournament sequencing (tournament sort), is a way to sort by the idea of a tournament. The first 22 comparisons are made to the keywords of n records, and then a 22 comparison is made between the smaller N/2, so repeat until the smallest record is selected.

The algorithm implementation code is as follows:

Package exp_sort;
		public class Treeselectsort {public static int[] Treeselectionsort (int[] mdata) {int treelong = mdata.length * 4;
		int MinValue =-10000; Int[] tree = new Int[treelong];
		The size of the tree int basesize;
		int i;
		int n = mdata.length;
		int Max;
		int maxindex;
		int treeSize;
		
		Basesize = 1;
		while (Basesize < n) {basesize *= 2;
		
		} treeSize = Basesize * 2-1;
		for (i = 0; i < n; i++) {tree[treesize-i] = Mdata[i];
		} for (; i < basesize; i++) {tree[treesize-i] = MinValue; 
		}//Construct a tree for (i = treeSize; i > 1; I-= 2) {TREE[I/2] = (Tree[i] > tree[i-1]? tree[i]: tree[i-1]);
		} N-= 1;
			while (n! =-1) {max = tree[1];
			mdata[n--] = max;
			Maxindex = treeSize;
			while (Tree[maxindex]! = max) {maxindex--;
			} Tree[maxindex] = MinValue;  while (Maxindex > 1) {if (maxindex% 2 = = 0) {TREE[MAXINDEX/2] = (Tree[maxindex] > tree[maxindex + 1]? Tree[maxindex]: tree[Maxindex + 1]);
				} else {TREE[MAXINDEX/2] = (Tree[maxindex] > tree[maxindex-1]? tree[maxindex]: tree[maxindex-1]);
			} maxindex/= 2;
	}} return mdata;  } public static void Main (string[] args) {//TODO auto-generated method stub int array[] = {38, 62, 35, 77, 55,
		14, 35, 98};
		
		Treeselectionsort (array);
		for (int i = 0; i < Array.Length; i++) {System.out.print (Array[i] + "");
	} System.out.println ("\ n");
 }

}

Algorithm Analysis:

In the tree selection sort, in addition to the smallest keyword, the smallest keyword is chosen to go through a leaf node to the comparison with the node, because of the full binary tree containing n leaf nodes depth log2n+1, so in the tree selection sort, each selected a smaller keyword needs to be log2n times compared, So its time complexity is O (nlog2n), the number of mobile records does not exceed the number of comparisons, so the total algorithm time complexity of O (nlog2n), compared with the simple choice of sorting algorithm, reduce the number of comparisons of the order of magnitude, increased n-1 additional storage space to store the intermediate comparison results.


In the next section, we talk about an alternative sorting method for improving tree selection and sorting--heap ordering.



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.