Get the App
SLTechnology News&Howtos  ›  Servers  › 

What is the method of selecting sorting by Java

Shulou Source: shulou.com Published: 2022-05-31 17:57:18 09月21日 Update

The main content of this article is to explain "what is the method of Java selection and sorting". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the method of Java selection and sorting"?

Public class SelectSort {public static void main (String [] args) {int [] nums = {49 System.out.print (num+ "\ t") {System.out.print (int num:nums) {System.out.print (num+ "\ t") }} / * * simple sort processing flow (1) find the element with the lowest keyword from the sequence to be sorted (2) if the smallest element is not the first element of the sequence to be sorted, swap it with the first element; (3) find the element with the lowest keyword from the remaining N-1 elements, and repeat steps (1) and (2) until the sorting is over. * * / static void simpleSelectSort (int [] nums) {/ / 7 for 6 for (int I = 0; I)

< nums.length; i++) { int minIndex = i; for (int j = i+1; j < nums.length; j++) { if (nums[minIndex]>

Nums [j]) {minIndex = j;}} if (minIndexposi) {int temp = nums [I]; nums [I] = nums [minIndex]; nums [minIndex] = temp } static void treeSelectSort (int [] nums) {/ / 19,38,65,97,76,13,27,49 int len = nums.length; int treeSize = 2*len-1; / / the number of nodes of the full binary tree int low = 0; int [] tree = new int [treeSize] / / temporary tree storage space / / populate the tree from back to front, the index starts from 0 for (int I = len-1 description j = 0 * * I > = 0 * * Musashi j = j * * +) {/ / populate the leaf node tree [treeSize-1-j] = nums [I];} for (int x:tree) {System.out.print (x + "\ t") } System.out.println (); for (int I = treeSize-1; I > 0; I-= 2) {/ / populate the non-terminal node / / System.out.println (I + "- -" + ((iMel 1) / 2)); tree [(iMuk 1) / 2] = (tree [iMul 1])

< tree[i] ? tree[i - 1] : tree[i]); } for (int x:tree) { System.out.print(x+"\t"); } System.out.println(); //不断移走最小节点 int minIndex; while (low < len){ int min = tree[0]; //最小值 nums[low++] = min; minIndex = treeSize - 1; while(tree[minIndex] != min){//不断移走最小节点 minIndex--; } tree[minIndex] = Integer.MAX_VALUE;//设置一个最大值标志 //找到其兄弟节点 while (minIndex >

0) {/ / if it has a parent node-- > this step aims to regenerate a tree if (minIndex% 2 = = 0) {/ / if it is the right node tree [(minIndex-1) / 2] = (tree [minIndex-1])

< tree[minIndex] ? tree[minIndex - 1] : tree[minIndex]); minIndex = (minIndex-1)/2; } else {//如果是左节点 tree[minIndex/2] = (tree[minIndex] < tree[minIndex + 1] ? tree[minIndex] : tree[minIndex + 1]); minIndex = minIndex/2; } }// for (int x:tree) {// System.out.print(x+"\t");// }// System.out.println(); } } static void heapSelectSort(int[] nums){ int len = nums.length; //构建堆 for (int i = (len -1)/2; i >

= 0; iMel -) {heapAdjust (nums,i,len);} / / output the heap top element and adjust the process of building a new heap int count = len-1; while (count > 0) {/ / swap tree root and the last value swap (nums,0,count); count--; heapAdjust (nums,0,count) }} static void heapAdjust (int [] nums, int I, int len) {int parent = nums [I]; for (int j = (iTun1) * 2-1; j

< len; j=(j+1)*2 - 1) { if (j < len -1 && nums[j] < nums[j+1]){ ++j; } if (parent >

Nums [j]) {break;} nums [I] = nums [j]; I = j;} nums [I] = parent;} / * the value of two elements in the exchange array * / private static void swap (int [] nums,int iPerint j) {int temp = nums [I] Nums [I] = nums [j]; nums [j] = temp;}} at this point, I believe you have a deeper understanding of "what is the method of Java selection sorting". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Element sort node method minimum selection key keyword content sequence learning practical deeper continuous interest leaf practical practical simple operation array Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MySQL vpn Linux Redmi