Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Quick sort in java

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to achieve quick sorting in java, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.

I. principle

Select a key value as the base value. Those smaller than the benchmark are in the left sequence (usually unordered), and those larger than the benchmark are on the right (usually unordered). Generally select the first element of the sequence.

2. Description

One cycle: compare from back to front, compare the base value with the last value, if the exchange position is smaller than the base value, if you do not continue to compare the next one, do not exchange until the first value less than the base value is found. After finding this value, start the comparison from the back. If there is anything larger than the base value, swap the position. If you do not continue to compare the next value, you will not exchange until you find the first value that is larger than the base value. Until the index is compared from back to back > from back to front, the first loop ends, and the left and right sides are ordered for the benchmark value. Repeat the above cycle.

3. Example public class maxtest {public static void main (String [] args) {/ / # Quick sorting method start#//int [] state = {2myr13, 7, 6, 15, 10, 12, 16, 16, 14, 11, 14, 5, 1}; int start = 0; int end = state.length-1 System.out.print ("initial value:"); for (int I = 0; I

< state.length; i++) { System.out.print(state[i]+","); } System.out.println(""); sort(state,start,end); //###################快速排序法end###################// } public static int partition(int []array,int lo,int hi){//固定的切分方式int key=array[lo];while(lo=key&&hi>

Lo) {/ / scan forward hi--;} array [lo] = array [hi] from the back part; while (Array [lo] lo) {lo++;} array [hi] = Array [lo];} System.out.println ("); for (int I = 0; I)

< array.length; i++) { System.out.print(array[i]+","); } array[hi]=key;return hi; } public static void sort(int[] array,int lo ,int hi){if(lo>

= hi) {return;} int index=partition (array,lo,hi); sort (array,lo,index-1); sort (array,index+1,hi);}}

The results are as follows:

初始值:2,13,7,6,15,9,10,12,3,16,8,11,14,5,1,1,13,7,6,15,9,10,12,3,16,8,11,14,5,13,1,2,5,6,3,9,10,12,9,16,8,11,14,15,13,1,2,3,6,6,7,10,12,9,16,8,11,14,15,13,1,2,3,5,6,7,8,9,9,16,12,11,14,15,13,1,2,3,5,6,7,8,9,10,16,12,11,14,15,13,1,2,3,5,6,7,8,9,10,13,12,11,14,15,13,1,2,3,5,6,7,8,9,10,11,12,11,14,15,16,1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,

The figure is as follows:

On how to achieve quick sorting in java to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report