Java algorithm interview question: Design a quick sort. Two-way fast sequencing, simple and easy to understand.

Source: Internet
Author: User

 PackageCom.swift;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;Importjava.util.List; Public classQuickSort {/** Quick Sort*/     Public Static voidMain (string[] args) {int[] strvoid =New int[] {11, 66, 22, 0, 55, 2, 0, 11 }; QuickSort Sort=NewQuickSort (); Sort.quicksort (Strvoid,0, Strvoid.length-1);  for(inti = 0; i < strvoid.length; i++) {System.out.println (Strvoid[i]+ " "); }        //Sort by Comparatorlist<integer> list =NewArraylist<integer>();  for(Integer i:strvoid) {list.add (i); } collections.sort (List,NewComparator<integer>() {@Override Public intCompare (integer arg0, integer arg1) {intnum = arg1-arg0; returnnum;        }        });  for(Integer i:list) {System.out.print (i+ " | "); }    }    voidQuickSort (int[] strdate,intLeftintRight ) {        intI, J, T, Key; if(Left >Right )return; Key= Strdate[left];//the base number is stored in temp.i =Left ; J=Right ;  while(I! =j) {//from the back of the key to find the smaller or wait for the key to the left             while(Strdate[j] > key && i <j) J--; //I used to find bigger than key on the right side of key             while(Strdate[i] <= key && i <j) I++; //less than the swap position, equal to stop            if(I <j) {T=Strdate[i]; Strdate[i]=Strdate[j]; STRDATE[J]=BT; }        }        //Two when the pointer is equal, the value of the first position is swapped with the value at the same position, completing the first round of sortingStrdate[left] =Strdate[i]; Strdate[i]=key; QuickSort (Strdate, left, I-1);//proceed to the left, here is a recursive processQuickSort (strdate, i + 1, right);//proceed to the right, here is a recursive process    }}

This is my train of thought, should belong to a two-way fast sort, a quick sort of solution too many ideas, there is a single road, two, three road, each kind of writing also has different, everyone's ideas are strange.

Java algorithm interview question: Design a quick sort. Two-way fast sequencing, simple and easy to understand.

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.