我给你来个快速排序import java.util.*;public class QuickSortUtil { public static final void quickSortString(int[] s) { quickSortString(s, 0, s.length-1); }
//用递归的快速排序法对s从low到high排序 private static final void quickSortString(int[] s, int low, int high) { //定义lo和hi两个标记 int lo = low; int hi = high; if (lo >= hi) return ; int mid = s[(lo + hi) / 2];//取中间位置的值 while (lo < hi) { while (lo<hi && s[lo]<mid) lo++;//lo向后移动 while (lo<hi && s[hi]>mid) hi--;//hi向前移动 if (lo < hi) { int temp = s[lo];//交换s[lo]和s[hi] s[lo] = s[hi]; s[hi] = temp; lo++;//lo向后移动 hi--;//hi向前移动 } } if (hi < lo) { int temp = hi;//交换lo和hi hi = lo; lo = temp; } quickSortString(s, low, lo);//用快速排序法对s从low到lo排序 quickSortString(s, lo == low ? lo+1 : lo, high);//用快速排序法对s从lo到high排序 } }
ustbzhangwei(wei)请问 private static final void quickSortString(int[] s, int low, int high)中的low和high代表什么?
代码不是很规范,只是个示例 public static void main(String[] args) { String source = "121501020607"; ArrayList al = new ArrayList(); for( int i = 0, n = source.length(); i < n; ){ al.add( source.substring( i, i += 2 ) ); } for( int i = 0, n = al.size(); i < n ;i++ ){ System.out.println( al.get( i ) ); } Collections.sort( al ); for( int i = 0, n = al.size(); i < n ;i++ ){ System.out.println( al.get( i ) ); } }
Geniusth(I'm just a Fool) ( ) 信誉:100 这位哥哥是挺厉害的,呵呵。 ustbzhangwei(wei) ( ) 信誉:100 使用的是一种典型的排序方法而且,任何数据结构的书上都有的(我们当然最好看java语言描述了,拿过来就可以用,呵呵)。
{
public static final void quickSortString(int[] s)
{
quickSortString(s, 0, s.length-1);
}
//用递归的快速排序法对s从low到high排序
private static final void quickSortString(int[] s, int low, int high)
{
//定义lo和hi两个标记
int lo = low;
int hi = high;
if (lo >= hi)
return ; int mid = s[(lo + hi) / 2];//取中间位置的值
while (lo < hi)
{
while (lo<hi && s[lo]<mid)
lo++;//lo向后移动
while (lo<hi && s[hi]>mid)
hi--;//hi向前移动
if (lo < hi)
{
int temp = s[lo];//交换s[lo]和s[hi]
s[lo] = s[hi];
s[hi] = temp;
lo++;//lo向后移动
hi--;//hi向前移动
}
}
if (hi < lo)
{
int temp = hi;//交换lo和hi
hi = lo;
lo = temp;
}
quickSortString(s, low, lo);//用快速排序法对s从low到lo排序
quickSortString(s, lo == low ? lo+1 : lo, high);//用快速排序法对s从lo到high排序
}
}
private static final void quickSortString(int[] s, int low, int high)中的low和high代表什么?
import java.util.*;public class TestSort {
public static String formatString(String str, int size, char c) { //格式化输出
if(str.length()>=size)
return str;
String result;
char[] imp =new char[size-str.length()];
for(int i=0;i<imp.length;i++)
imp[i]=c;
result=new String(imp);
result += str;
return result;
} public static String sort(String str) {
String result="";
String allNum="0123456789";
for(int i=0;i<str.length();i++) { //判断是否是数字串
if(allNum.indexOf(String.valueOf(str.charAt(i)))<0) {
return str;
}
}
int[] s=new int[str.length()/2];
for(int i=0;i<s.length;i++) {
s[i]=Integer.parseInt(str.substring(i*2,i*2+2));
}
Arrays.sort(s);
for(int i=0;i<s.length;i++) {
result += formatString(Integer.toString(s[i]),2,'0');
}
return result;
}
public static void main(String[] args) {
String sortStr ="121501020607";
System.out.println(sortStr + " after sorted: " + sort(sortStr));
}
}输出:
121501020607after sorted: 010206071215
代码不是很规范,只是个示例
public static void main(String[] args) {
String source = "121501020607";
ArrayList al = new ArrayList();
for( int i = 0, n = source.length(); i < n; ){
al.add( source.substring( i, i += 2 ) );
}
for( int i = 0, n = al.size(); i < n ;i++ ){
System.out.println( al.get( i ) );
}
Collections.sort( al );
for( int i = 0, n = al.size(); i < n ;i++ ){
System.out.println( al.get( i ) );
}
}
使用的是一种典型的排序方法而且,任何数据结构的书上都有的(我们当然最好看java语言描述了,拿过来就可以用,呵呵)。