Java implementation of fast Ordering (Quicktsort) __java

Source: Internet
Author: User

Directory (?) [+] Introduction to the Fast sorting algorithm

The fast sorting and merging sort all use divide the method to design the algorithm, the difference lies in the merging sort to divide the array into two basic equal length sub arrays, after the sequence is arranged separately also to carry on the merge (merge) operation , but the quick sort splits the molecule array the time appears more artistic, takes a A datum element, after which 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 splits 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 that is 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. Sort [Start, index], and [index+2, end] Two sub arrays separately

Like the Java implementation of insert sort (insertsort), implement an Array tool class first. The code is as follows:[Java] View Plain copy 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]);                    if  (i <  array.length - 1)  {                        system.out.print (", ");                   }                }                system.out.println ("}");            }              public static void  exchangeelements (INT[]&NBSP;ARRAY,&NBSP;INT&NBSP;INDEX1,&NBSP;INT&NBSP;INDEX2)  {                int temp = array[index1];                array[index1] =  array[index2];               array[index2 ] = temp;           }        }  

The fast-sorted Java implementation and test code are as follows:[Java] View plain copy 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);       }           private static void subquicksort (int[] array, int start,  Int end)  {           if  (array ==  null | |   (end - start + 1)  < 2)  {                return;           }               int part = partition ( Array, start, end);              if  ( Part == start)  {                subquicksort (array, part + 1, end);            } else if  (part == end)  {                subquicksort (array, start, part - 1);            } else {                subquicksort (array, start, part - 1) ;               subquicksort (array,  Part + 1, end);           }        }          private static int partition (int[] array, int start, int end)  {            int value = array[end];           int  index = start - 1;               for  (int i = start; i < end; i++)  {                if  (Array[i] < value)  {                    index++;                    if  (index != i)  {                        arrayutils.exchangeelements (Array,  index, i);                    }               }            }               if  ((index + 1)  != end)  {                arrayutils.exchangeelements (array, index + 1, end);            }               return index + 1;       }  } 

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.