Java Algorithm interview questions: What are the methods for sorting? List. Use JAVA to implement a fast sorting. Select bubble quick set to sort by at least four methods, java Algorithm

Source: Internet
Author: User

Java Algorithm interview questions: What are the methods for sorting? List. Use JAVA to implement a fast sorting. Select bubble quick set to sort by at least four methods, java Algorithm

Package com. swift; import java. util. arrayList; import java. util. collections; import java. util. comparator; import java. util. list; public class QuickSort {/** Quick Sort */public static void main (String [] args) {String [] strVoid = new String [] {"11 ", "66", "22", "0", "55", "22", "0", "32"}; QuickSort sort = new QuickSort (); sort. quickSort (strVoid, 0, strVoid. length-1); for (int I = 0; I <strVoid. length; I ++) {System. out. println (strVoid [I] + "") ;}// sort List by comparator <String> list = new ArrayList <String> (); for (String str: strVoid) {list. add (str);} Collections. sort (list, new Comparator <String> () {@ Override public int compare (String arg0, String arg1) {int num = arg1.compareTo (arg0); return num ;}}); for (String str: list) {System. out. print (str + "|") ;}} public void quickSort (String [] strDate, int left, int right) {String middle, tempDate; int I, j; I = left; j = right; middle = strDate [(I + j)/2]; do {while (strDate [I]. compareTo (middle) <0 & I <right) I ++; // find the number on the left greater than the median while (strDate [j]. compareTo (middle)> 0 & j> left) j --; // find a number smaller than the median on the right if (I <= j) {// Replace the large and small numbers on the left with tempDate = strDate [I]; strDate [I] = strDate [j]; strDate [j] = tempDate; I ++; j -- ;}}while (I <= j); // when the two are staggered, stop if (I <right) {quickSort (strDate, I, right ); // slave} if (j> left) {quickSort (strDate, left, j );}}}

 

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.