从字符串取出数值,并进行排序

来源:互联网 发布:粤语教程软件 编辑:程序博客网 时间:2024/05/17 05:14
  题目:从一个字符串取出数值,并进行从小到大的排序
  思路:利用正则表达式取数,利用希尔排序进行重排列

import java.util.Arrays;
import java.util.regex.Matcher;

import java.util.regex.Pattern;


public class GetNeedStringTest {
/**
* @param args by gongxuesong
*/
private static int a,b;
public static int[] stringToInts(String s) {
int result[] = new int[s.length()];
for(int i=0;i<s.length();i++) {
result[i] =Integer.parseInt(s.substring(i, i+1));
}
return result;
}
public static int[] shellsort(int a[], int n)  
{  
   int i, j, gap;  
 //希尔排序
   for (gap = n / 2; gap > 0; gap /= 2)  
       for (i = gap; i < n; i++)  
           for (j = i - gap; j >= 0 && a[j] > a[j + gap]; j -= gap)  
               {
               a[j]^=a[j+gap];
               a[j+gap]^=a[j];
               a[j]^=a[j+gap];
               }
              
   return a;

static String str="Gongxuesong is a pretty boy! 另外,国庆节是1991年10月1日哦";
public static void main(String[] args) {
// TODO Auto-generated method stub
Matcher m = Pattern
.compile("\\d", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE)
.matcher(str);
String s="";
while(m.find()) {
  s = s +m.group();
}
System.out.println();
for(int i=0;i<s.length();i++) {
System.out.print(shellsort(stringToInts(s), s.length())[i]+"");
}


}


}
0 0
原创粉丝点击