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).   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:     code is as follows: public class Arrayutils {       public static void PrintArray (int[] array) {      System.out.print ("{");       for (int i = 0; i < Array.Length; i++) {       system.out.print (array[i)) &N Bsp      if (i < array.length-1) {        SyStem.out.print (",");              }       SYSTEM.OUT.PRINTLN ("}");     &NBSP}        public static void Exchangeelements (int[] array, int index1, int index2) {      int temp = Array[index1]       ARRAY[INDEX1] = Array[index2];       ARRAY[INDEX2] = temp;     &NBSP}    }       Quick sort Java implementation and test code as follows: The     code is as follows: public class QuickSort {    public static void main (string[] args) {   int[] array = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0,-1,- 2,-3};      SYSTEM.OUT.PRINTLN ("before sort:");    arrayutils.printarray (array);      quicksort (array);      SYSTEM.OUT.PRINTLN ("after sort:");    arrayutils.printarray (array);  }     public static void QuickSort (int[] array) {   subquicksort (array, 0, array.length-1); &nbsP }     private static void Subquicksort (int[] array, int start, int end) {   if (array = = NULL | | (End-start + 1) < 2) {    return;   &nbsp.}      int part = partition (array, start, en D);      if (part = = start) {    Subquicksort (array, part + 1, end);   &NBSP/else if (part = = End) {    Subquicksort (array, start, part-1);   &NBSP} else {    Subquicksort (array, start, PART-1);     Subquicksort (array, part + 1, end);   &NBSP}  }     private static int partition (int[] array, int start, int end) {   int val UE = Array[end];    int index = start-1;      for (int i = start; i < end; i++) {    if (Array[i] < value) {     in dex++;      IF (index!= i) {      arrayutils.exchangeelements (array, index, i);     &NB SP;}    }  &NBSP}      if ((index + 1)!= end) {    arrayutils.exchangeelements (array, index + 1, E nd);   &NBSP      return index + 1;  }  }    
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

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

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.