Java Quick Sort)

Source: Internet
Author: User

Java Quick Sort)

This article mainly introduces Java's quick sorting algorithm (Quicktsort). If you need it, please refer to it.

Quick Sort Algorithm Introduction

Both quick sorting and Merge Sorting use the divide and conquer method to design algorithms. The difference is that Merge Sorting divides arrays into two basic sub-arrays with an equal length. After sorting separately, Merge (Merge) operations are also performed, the quick sorting and splitting of molecular arrays is more artistic. Take a benchmark element. After the split, the elements on the left of the benchmark element are smaller than those on the benchmark element, and the elements on the right are no less than the benchmark element, in this way, you only need to sort the two sub-arrays separately, and do not need to merge operations like merging and sorting. The selection of benchmark elements has a great impact on the efficiency of the algorithm. The best case is that the size of the two subarrays is basically the same. For simplicity, we select the last element. In a more advanced approach, we can first find a median and exchange the median with the last element, and then perform the same operation steps. Splitting is the core of fast sorting. The worst running time of quick sorting is O (n2), but the expected running time is O (nlgn ).

 

Java Implementation of the Quick Sort Algorithm

1. split the array into two sub-arrays and add a benchmark element: select the last element as the benchmark element. The index variable records the location of the last element smaller than the benchmark element and initializes it to start-1, A new element smaller than the base element is found, and the index is added with 1. From the first element to the second last element, compare with the base element in sequence. If the base element is smaller than the base element, add 1 to the index, and change the position index and the current position element. After the loop ends, index + 1 obtains the position where the baseline element should be located, exchanging index + 1 and the last element.

2. Sort the [start, index] and [index + 2, end] sub-arrays respectively.

Like Java Implementation of Insertsort, an array tool class is implemented first. The Code is as follows:

 

 

The 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]);

If (I <array. length-1 ){

System. out. print (",");

}

}

System. out. println ("}");

}

 

Public static void exchangeElements (int [] array, int index1, int index2 ){

Int temp = array [index1];

Array [index1] = array [index2];

Array [index2] = temp;

}

}

 

 

 

Java Implementation and test code of quick sorting are 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 );

}

 

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;

}

}

 

 

Related Article

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.