Java implementation Fast sorting algorithm (Quicktsort)

Source: Internet
Author: User
Tags arrays sort split

This article mainly introduces the Java implementation of the Fast sorting algorithm (Quicktsort), the need for friends can refer to the

The fast sorting algorithm introduces the method of divide-and-conquer to design the algorithm in fast sorting and merging sort. The difference is that the merge sort divides the array into two basic, equal-length sub arrays, which are sorted out and merged (merge) operations, while the quick sort of split-molecule array appears more artistic, taking a datum element, After splitting, the elements on the left side of the datum element are smaller than the datum element, and the elements on the right are not less than the datum elements, so that only the two sub arrays need to be sorted, and the merge operations are no longer required as merge sorts. The selection of datum elements has a great effect on the efficiency of the algorithm, and the best case is that two sub arrays are roughly the same size. For simplicity, we choose the last element, and the more advanced approach is to find a median and swap the median with the last element before doing the same procedure. Splitting is the core of a quick sort. The worst run time for a quick sort is O (N2), but the expected elapsed time is O (NLGN). &nbsp; Fast Sort algorithm Java implementation 1. Split the array into two sub arrays plus a datum element: Select the last element as the datum element, and the index variable records the location of the most recent element that is less than the datum element, initialized to Start-1, and discovers a new element less than the datum element. Index plus 1. From the first element to the penultimate element, in turn, compare to the datum element, which is less than the datum element, the index plus 1, the Exchange position index and the current position element. After the loop ends, Index+1 gets the position where the datum element should be, swapping index+1 and the last element. 2. Sort [Start, index], and [index+2, end] Two sub arrays like the Java implementation of insert sort (insertsort), implement an Array tool class first. The code is as follows: &nbsp; &nbsp; code is as follows: public class Arrayutils {&nbsp; &nbsp; &nbsp; &nbsp;public static void PrintArray (int[] array) {&nbsp; &nbsp; &nbsp; System.out.print ("{"); &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; Array.Length; i++) {&nbsp; &nbsp; &nbsp; &nbsp;system.out.print (array[i)) &N Bsp &nbsp; &nbsp; &nbsp;if (i &lt; array.length-1) {&nbsp; &nbsp; &nbsp; &nbsp; SyStem.out.print (","); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; SYSTEM.OUT.PRINTLN ("}"); &nbsp; &nbsp; &NBSP} &nbsp; &nbsp; &nbsp; &nbsp;public static void Exchangeelements (int[] array, int index1, int index2) {&nbsp; &nbsp; &nbsp; int temp = Array[index1] &nbsp; &nbsp; &nbsp; ARRAY[INDEX1] = Array[index2]; &nbsp; &nbsp; &nbsp; ARRAY[INDEX2] = temp; &nbsp; &nbsp; &NBSP} &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; Quick sort Java implementation and test code as follows: The &nbsp; &nbsp; code is as follows: public class QuickSort {&nbsp; &nbsp; public static void main (string[] args) {&nbsp; &nbsp;int[] array = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0,-1,- 2,-3}; &nbsp; &nbsp; &NBSP;SYSTEM.OUT.PRINTLN ("before sort:"); &nbsp; &nbsp;arrayutils.printarray (array); &nbsp; &nbsp; &nbsp;quicksort (array); &nbsp; &nbsp; &NBSP;SYSTEM.OUT.PRINTLN ("after sort:"); &nbsp; &nbsp;arrayutils.printarray (array); &nbsp;} &nbsp; &nbsp; public static void QuickSort (int[] array) {&nbsp; &nbsp;subquicksort (array, 0, array.length-1); &nbsP } &nbsp; &nbsp; private static void Subquicksort (int[] array, int start, int end) {&nbsp; &nbsp;if (array = = NULL | | (End-start + 1) &lt; 2) {&nbsp; &nbsp; return; &nbsp; &nbsp.} &nbsp; &nbsp; &nbsp;int part = partition (array, start, en D); &nbsp; &nbsp; &nbsp;if (part = = start) {&nbsp; &nbsp; Subquicksort (array, part + 1, end); &nbsp; &NBSP/else if (part = = End) {&nbsp; &nbsp; Subquicksort (array, start, part-1); &nbsp; &NBSP} else {&nbsp; &nbsp; Subquicksort (array, start, PART-1); &nbsp; &nbsp; Subquicksort (array, part + 1, end); &nbsp; &NBSP} &nbsp;} &nbsp; &nbsp; private static int partition (int[] array, int start, int end) {&nbsp; &nbsp;int val UE = Array[end]; &nbsp; &nbsp;int index = start-1; &nbsp; &nbsp; &nbsp;for (int i = start; i &lt; end; i++) {&nbsp; &nbsp; if (Array[i] &lt; value) {&nbsp; &nbsp; &nbsp;in dex++; &nbsp; &nbsp; &NBSP;IF (index!= i) {&nbsp; &nbsp; &nbsp; arrayutils.exchangeelements (array, index, i); &nbsp; &nbsp; &NB SP;} &nbsp; &nbsp;}&nbsp; &NBSP} &nbsp; &nbsp; &nbsp;if ((index + 1)!= end) {&nbsp; &nbsp; arrayutils.exchangeelements (array, index + 1, E nd); &nbsp; &NBSP &nbsp; &nbsp; &nbsp;return index + 1; &nbsp;} &nbsp;} &nbsp; &nbsp;
Related Keywords:
Related Article E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD