Quick sorting (1)

xiaoxiao2021-04-06  241

/ ***

Title: Quick Sorting *

Description: * Quick Sort is one of the fastest rankings (depending on the object of the subject), * Although the rapid sorting method can reach O (N2) under the worst state, in most cases, the efficiency performance of the rapid sorting method is quite good. * The basic spirit of the rapid sorting method is to find the appropriate axis in the plurality of columns, and then divide the number one to two. Sort the left and the right number, which affects the efficiency of the rapid ordering method. Selection. * *

Copyright: Copyright (c) 2006 *

Company: * @Author NOT Attributable * @version 1.0 * / public class quicksort {

Public static void print (int [] number) {// Print input array content for (int i = 0; i

Public static void swap (int [] number, int i, int j) {// exchange Number array index i, value INT TMP; TMP = Number [i]; Number [i] = number [J]; Number [J] = TMP;} / ** * Fast Sort by the left element * Solution: * 1. Set the leftmost number to the axis, and record its value as S * 迴 process: * 2. Order index i from the number column left to the right, until the number greater than S is found * 3. Let the index j look forward from the number of numbers to the left, until the number of less than s * 4. If i> = j, leave the loop * 5. If i

* @Param Number Sort Array * @Param LEFT Start Index * @Param Right End Index * / Public Static Void Sort (int [] Number, INT LEFT, INT RIGHT) {IF (Left -1 && Number [- j]> s) // Looking left; if (i> = j) Break; swap (number, i, j); print (Number);} Number Left] = Number [J]; Number [J] = S; Sort (Number, Left, J - 1); // End back to Sort (Number, J 1, Right); // In the right }} / ** * Rapid Sort by the intermediate element * Take the intermediate element S, the same first-to-right find the index i, then find the index J, * as long as both sides The index has not failed, exchange the element value of I and J, this time no need to exchange the shaft, because the elements of the *, the elements of the position in the process of searching are also involved in the exchange, for example: * 41 24 76 11 45 64 21 69 19 36 * First Left is 0, Right is 9, (Left Right) / 2 = 4 (Take the intensity of the number), the axis is the location of the index 4, * Compare the element is 45, you go to the right Looking for 45 big, looking for left to the left to exchange: * 41 24 76 * 11 [45] 64 21 69 19 * 36 * 41 24 36 11 45 * 64 21 69 19 * 76 * 41 24 3 6 11 19 64 * 21 * 69 45 76 * [41 24 36 11 19 21] After completing the above, the left brackets are handed back to the part of the parentheses, so you can complete Sort the purpose.

* @Param Number * @Param Left * @Param Right * / Public Static Void Sort1 (int [] Number, int LEFT, INT Right) {if (Left >> 1; INT S = Number [MID]; int Len = Number.Length; While (True) {While (i 1 -1 && number [- j]> s) // left shift left; if (i> j) Break; SWAP (Number, I, J); Print (Number); Sort1 (Number, Left, J); Sort1 (Number, I, Right);}} public static void sort2 (int [] number, int} in (} public static void main (string) ] args) {int [] Number = {-6, -1}; sort1 (number, 0, number.length - 1);}}

转载请注明原文地址:https://www.9cbs.com/read-132255.html

New Post(0)