In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail "Java common sorting algorithm how to achieve", the content is detailed, the steps are clear, the details are handled properly, I hope this article "Java common sorting algorithm how to achieve" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.
Summary:
1. Bubbling sort
Determine the maximum value of each cycle
Public void bubbleSort (int [] nums) {int temp; boolean isSort = false; / / optimize and exit for (int I = 0; I) if you find that the order is good.
< nums.length-1; i++) { for (int j = 0; j < nums.length-1-i; j++) { //每次排序后能确定较大值 if(nums[j] >Nums [juni1]) {isSort = true; temp = nums [j]; nums [j] = nums [juni1]; nums [juni1] = temp;}} if (! isSort) {return;} else {isSort = false;}} 2. Select sort
Choose the best value each time, and then exchange it to the edge.
Public void selectSort (int [] nums) {for (int I = 0; I)
< nums.length-1; i++) { int index = i; int minNum = nums[i]; for (int j = i+1; j < nums.length; j++) { if(nums[j] < minNum){ minNum = nums[j]; index = j; } } if(index != i){ nums[index] = nums[i]; nums[i] = minNum; } }}3. 插入排序 对循环的每个数找到属于自己的位置插入; public void insertionSort(int[] nums){ for (int i = 1; i < nums.length; i++) { int j = i; int insertNum = nums[i]; while(j-1 >= 0 & Nums [j-1] > insertNum) {nums [j] = Nums [j-1]; Jmurmuri;} nums [j] = insertNum;}} 4. Quick sort
Choose a basic value, put it on one side that is less than it, and put on the other side if it is larger than it.
Public void quickSortDfs (int [] nums, int left, int right) {if (left > right) {return;} int l = left; int r = right; int baseNum = nums [left]; while (l
< r){ //必须右边先走 while(nums[r] >= baseNum & & l
< r){ r--; } while(nums[l] = r){ return; } int m = (l+r)/2; mergeSortDfs(nums, l, m); mergeSortDfs(nums, m+1, r); merge(nums, l, m, r);}//并private void merge(int[] nums, int left, int mid, int right){ int[] temp = new int[right-left+1]; int l = left; int m = mid+1; int i = 0; while(l 0; step /= 2) { for (int i = step; i < nums.length; i++) { int j = i; int insertNum = nums[i]; while(j-step >= 0 & & nums [j-step] > insertNum) {nums [j] = nums [j-step]; jmurf step;} nums [j] = insertNum;} 7. Heap sort
The big top heap implements ascending order, moving the maximum value to the last position of the heap each time.
Public void heapSort2 (int [] nums) {for (int I = nums.length/2-1; I > = 0; iMel -) {sift (nums, I, nums.length);} for (int I = nums.length-1; I > 0; iMet -) {int temp = nums [0]; nums [0] = nums [I]; nums [I] = temp; sift (nums, 0, I) }} private void sift (int [] nums, int parent, int len) {int value = nums [parent]; for (int child = 2*parent + 1; child)
< len; child = child*2 +1) { if(child+1 < len && nums[child+1] >Nums [child]) {child++;} if (nums [child] > value) {nums [parent] = nums [child]; parent = child;} else {break;}} nums [parent] = value;} 8. Counting and sorting
Count the number of occurrences of each number in order
Public void countSort (int [] nums) {int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for (int num: nums) {max = Math.max (max, num); min = Math.min (min, num);} int [] countMap = new int [max-min+1]; for (int num: nums) {countMap [num-min] +;} int I = 0; int j = 0 While (I)
< nums.length && j < countMap.length){ if(countMap[j] >0) {nums [I] = jimmins; iTunes; countMap [j] -;} else {jacks;}} 9. Bucket sort
Similar to counting sort, the difference is that the number in a certain interval (bucket) is counted.
Public void bucketSort (int [] nums) {int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for (int num: nums) {max = Math.max (max, num); min = Math.min (min, num);} int bucketCount = (max-min) / nums.length+1; List bucketList = new ArrayList (); for (int I = 0; I
< bucketCount; i++) { bucketList.add(new ArrayList()); } for(int num : nums){ int index = (num-min)/nums.length; bucketList.get(index).add(num); } for(List bucket : bucketList){ Collections.sort(bucket); } int j = 0; for(List bucket : bucketList){ for(int num : bucket){ nums[j] = num; j++; } }}10. 基数排序 按个、十、百位依次归类排序; public void radixSort(int[] nums){ int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int num : nums) { min = Math.min(min, num); max = Math.max(max, num); } for (int i = 0; i < nums.length; i++) { nums[i] -= min; } max -= min; int maxLen = (max+"").length(); int[][] bucket = new int[nums.length][10]; int[] bucketCount = new int[10]; for (int i = 0, n = 1; i < maxLen; i++, n*=10) { for (int num : nums) { int digitVal = num / n % 10; bucket[bucketCount[digitVal]][digitVal] = num; bucketCount[digitVal]++; } int index = 0; for (int j = 0; j < bucketCount.length; j++) { if(bucketCount[j] >0) {for (int k = 0; k < bucketCount [j]; knight +) {nums [index] = bucket [k] [j]; index++;}} bucketCount [j] = 0;}} for (int I = 0; I < nums.length; iTunes +) {nums [I] + = min }} 11. Use collection or API11.1 priority queue public void priorityQueueSort (int [] nums) {PriorityQueue queue = new PriorityQueue (); for (int num: nums) {queue.offer (num);} for (int I = 0; I < nums.length; iqueue +) {nums [I] = queue.poll ();}} 11.2 Java APIpublic void arraysApiSort (int [] nums) {Arrays.sort (nums) } after reading this, the article "how to implement common sorting algorithms in Java" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.