Java implementation string array and string sorting __java

Source: Internet
Author: User

In the Java environment, it's easy to sort string arrays or strings, so you can sort them by calling the sort method of the arrays class under the Java.util package.

Have you ever thought that we can also use the sort algorithm to write a string array of their own and string sorting, and I did that by using the idea of a quick sort:

* A quick sort of a trip is:
*1, Random generation of a series, select the first number (here also optional number of other) as the cornerstone of comparison, assuming that the number is X, so x=a[0]; set two variables i=0,j=n-1;n is the length of this sequence.
*2, look for it from the front, find the first number larger than a[0, find it and exchange it with X
*3, start looking at the back, find the first number smaller than a[0], find it and exchange it with X
*4, repeat steps 2, 3, until i=j;
* Such a quick sort is done; After the first trip, the number on the left side of X is all less than X, the middle on the right side of X is all greater than X, and then recursively sorts the number on the left of X and the number to the right of X in a recursive way.


The string number is sorted as follows:

Package com.org.tool;   
      T (string[] Pdata,int left,int right) {int i,j;   
      String first,temp;   
      i = left;   
      j = right; A = Pdata[left];  
          It's OK to choose the other numbers here, but I'll take the first one. Quick sort while (true) {//Find the number greater than the hub from the second number, and start looking for more than Pdata[left]   
          while ((++i) <right-1 && Pdata[i].compareto (a) <0);   
          Find the first number of less than the central Pdata[left] from the last number while ((--j) >left && Pdata[j].compareto (primary) >0);   
          if (i>=j) break;   
          The number of exchanges found on both sides temp = pdata[i];   
          Pdata[i] = Pdata[j];   
   
      PDATA[J] = temp;   
      }//Switching center pdata[left] = pdata[j];   
      PDATA[J] = A;   
      Data if (left<j) QuickSort (pdata,left,j) on the left side of the recursive fast-row center; Data on the right side of the recursive fast-row Center if(right>i) QuickSort (pdata,i,right);
		public static void Main (string[] args) {string[] arr = new string[]{"One", "Onad", "Dream", "Beijing", "Olympic"};
		QuickSort (arr, 0, 5);
		for (String S:arr) {System.out.print (s+ "");
 }
	}
}
The string sorting code is as follows:
Package com.org.tool; /** * character sorting, fast row idea * @author Xiaobangsky * time:2014-10-13/public class P4_9 {public static void QuickSort (char[)   
      Pdata,int left,int right) {int first,i,j;   
      char temp;   
      i = left;   
      j = right; A = Pdata[left];  
          It's OK to choose the other numbers here, but I'll take the first one. Quick sort while (true) {//Find the number greater than the hub from the second number, and start looking for more than Pdata[left]   
          while ((++i) <right-1 && pdata[i] < I;   
          Start with the number of the first less than the central Pdata[left] from the last number while ((--j) >left && pdata[j]>first);   
          if (i>=j) break;   
          The number of exchanges found on both sides temp = pdata[i];   
          Pdata[i] = Pdata[j];   
   
      PDATA[J] = temp;   
      }//Switching center pdata[left] = pdata[j];   
      PDATA[J] = (char) A;   
      Data if (left<j) QuickSort (pdata,left,j) on the left side of the recursive fast-row center; Data if (right>i) quicks to the right of the recursive fast-row CenterORT (Pdata,i,right);
		public static void Main (string[] args) {char[] arr = "XARVFWEGFE". ToCharArray ();
		QuickSort (arr, 0, 10);
		for (char S:arr) {System.out.print (s);
 }
	}
}
So it's done.

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.