Java Quick Sort

Source: Internet
Author: User

"Turn" http://blog.csdn.net/wangkuifeng0118/article/details/7286332

Ashamed to say, yesterday read someone else's blog above a number of algorithms, in fact, these algorithms have been studied in the university, but almost all forgotten. Although now do Java upper Development basically does not use the algorithm, but still is the feeling algorithm is a kind of thought, is a kind of soul, so also not only opened the Min teacher's data structure, one one to the previously forgotten algorithm realization once.

The basic idea of quick sorting :

Divide the records to be sorted into separate two parts by a single pass, where some of the recorded keywords are smaller than the other, and then the two sections continue to be sorted until the entire sequence is ordered.

First look at this picture:

Think of the whole sequence as an array, the 0th position as the middle axis, and the last one, if it is smaller than it, does not do any processing than it; The end is exchanged again and the smaller one, than it is smaller than that, which does not exchange much larger than it. This cycle, a trip to the completion of the order, the left side is smaller than the middle axis, the right is larger than the middle axis, and then the division of the method, respectively, the two separate array to sort.

[HTML]View Plaincopy
  1. public int getmiddle (integer[] list, int. Low, int.) {
  2. int tmp = List[low]; The first of the arrays as a middle axis
  3. while (Low < high ) {
  4. while (Low < high && List[high] > tmp) {
  5. high--;
  6. }
  7. List[low] = List[high]; Less than mid-axis records moved to the low end
  8. while (Low < high && List[low] < tmp) {
  9. low++;
  10. }
  11. List[high] = List[low]; A record larger than the middle axis moves to the high end
  12. }
  13. List[low] = tmp; Middle axis record to tail
  14. return low; Returns the position of the middle axis
  15. }


Recursive form of sorting algorithm:

[HTML]View Plaincopy
  1. public void _quicksort (integer[] list, int. Low, int.) {
  2. if (Low < high ) {
  3. int middle = getmiddle (list, low, high); Divides a list array into a split
  4. _quicksort (list, low, middle-1); Recursive ordering of low-word tables
  5. _quicksort (list, middle + 1, high); Recursive ordering of high-character tables
  6. }
  7. }


[HTML]View Plaincopy
    1. public void Quick (integer[] str) {
    2. if (Str.length > 0) {//See if the array is empty
    3. _quicksort (str, 0, str.length-1);
    4. }
    5. }


To write a test method:

[HTML]View Plaincopy
  1. public class Testmain {
  2. /**
  3. * @param args
  4. */
  5. public static void Main (string[] args) {
  6. TODO auto-generated Method Stub
  7. Integer[] list={34,3,53,2,23,7,14,10};
  8. Quicsort qs=new Quicsort ();
  9. Qs.quick (list);
  10. for (int i=0;i<list.length;i++) {
  11. System.out.print (list[i]+ "");
  12. }
  13. System.out.println ();
  14. }
  15. }

Look at the print results:

2 3 7 10 14 23 34 53

This sort is good, the quick sort is an improvement on the bubble sort, the average time complexity is O (NLOGN).

Java Quick Sort

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.