题目:从一个字符串取出数值,并进行从小到大的排序
思路:利用正则表达式取数,利用希尔排序进行重排列
import java.util.Arrays;
import java.util.regex.Matcher;
/**
* @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]+"");
}
}
}
思路:利用正则表达式取数,利用希尔排序进行重排列
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @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]+"");
}
}
}