In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Java sorting algorithm how to achieve fast sorting of the three-number middle method", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Java sorting algorithm how to achieve fast sorting of the three-number middle method" it!
The basic step is to take the number three.
In the process of fast arrangement, each time we take an element as the pivot value, with this number to divide the sequence into two parts. Here we use the three-number center method, that is, take the left end, the middle and the right end of the three numbers, and then sort them, and take the middle number as the pivot value.
Segmentation according to pivot value
The code implements package sortdemo;import java.util.Arrays;/** * Created by chengxiao on 2016-12-14. * Quick sort * / public class QuickSort {public static void main (String [] args) {int [] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; quickSort (arr, 0, arr.length-1); System.out.println ("sort result:" + Arrays.toString (arr)) } / * * @ param arr * @ param left left pointer * @ param right right pointer * / public static void quickSort (int [] arr, int left, int right) {if (left)
< right) { //获取枢纽值,并将其放在当前待处理序列末尾 dealPivot(arr, left, right); //枢纽值被放在序列末尾 int pivot = right - 1; //左指针 int i = left; //右指针 int j = right - 1; while (true) { while (arr[++i] < arr[pivot]) { } while (j >Left & & arr [--j] > arr [pivot]) {} if (I
< j) { swap(arr, i, j); } else { break; } } if (i < right) { swap(arr, i, right - 1); } quickSort(arr, left, i - 1); quickSort(arr, i + 1, right); } } /** * 处理枢纽值 * * @param arr * @param left * @param right */ public static void dealPivot(int[] arr, int left, int right) { int mid = (left + right) / 2; if (arr[left] >Arr [mid]) {swap (arr, left, mid);} if (arr [left] > arr [right]) {swap (arr, left, right);} if (arr [right] < arr [mid]) {swap (arr, right, mid);} swap (arr, right-1, mid) * * @ param arr * @ param a * @ param b * / private static void swap (int [] arr, int a, int b) {int temp = arr [a]; arr [a] = arr [b]; arr [b] = temp;}}
Sort result
[1, 2, 3, 4, 5, 6, 7, 8]
Thank you for your reading, the above is the "Java sorting algorithm how to achieve fast sorting of the three-number middle method" of the content, after the study of this article, I believe you on the Java sorting algorithm how to achieve fast sorting of the three-number middle method of this problem has a more profound understanding, the specific use of the need for everyone to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.