第一个Selection Sort 是自己写的,和后面的Selection Sort不同在于,这个版本多了个swap方法来交换两个element.
并且这个版本是外层循环每次都找到一个最小的,放在数组最前面。
点击(此处)折叠或打开
- package Algorithms;
- public class SelectionSort {
- private static void swap(int indexA, int indexB, int[] arr){
- int temp;
- temp=arr[indexA];
- arr[indexA]=arr[indexB];
- arr[indexB]=temp;
- }
- public static void selectionSort(int arr[]){
- int minIndex;
- for(int i=0;i<=arr.length-2;i++){
- minIndex=i;
- for(int j=i+1;j<=arr.length-1;j++){
- if(arr[minIndex] > arr[j]){
- minIndex=j;
- }
- }
- swap( i, minIndex, arr);
- }
- }
- public static void main(String[] args) {
- int arr[]= new int[]{5,3,1,10, 7,6, 12, 0};
- selectionSort(arr);
- for(int i: arr){
- System.out.print(i+" ");
- }
- }
- }
点击(此处)折叠或打开
- package Sort;
- import java.util.Arrays;
- import java.util.Random;
- public class ArraySort {
- public static void SelectionSort(int[] arr){
- int index;
- for(int i=1;i<arr.length;i++){
- index=0;
- for(int j=1;j<=arr.length-i;j++){
- if(arr[j] > arr[index]){
- index=j; //find max assign to index
- }
- }
- //swap arr.length-i and index (max)
- int temp=arr[arr.length-i];
- arr[arr.length-i]=arr[index];
- arr[index]=temp;
- }
- }
-
- public static void insertSort(int[] arr){
- for(int i=1;i<arr.length;i++){
- int key=arr[i];
- int j = i-1;
- while(j>=0 && key < arr[j]){
- arr[j+1]=arr[j];
- j--;
- }
- arr[j+1]=key;
- }
- }
-
- public static void BubbleSort(int[] arr){
- for(int i=1;i<arr.length;i++){
- //比较相邻两个元素,较大的往后冒泡
- for(int j=0;j<arr.length-i;j++){
- if(arr[j] > arr[j+1]){
- int temp=arr[j];
- arr[j]=arr[j+1];
- arr[j+1]=temp;
- }
- }
- }
- }
-
- public static void quickSort(int arr[],int lowIndex,int highIndex){
- int lo=lowIndex;
- int hi=highIndex;
- int mid;
- if(highIndex > lowIndex){
- mid=arr[(lowIndex+highIndex)/2]; //pivot
- while(lo <= hi){
- while((lo<highIndex) && (arr[lo] < mid))
- ++lo;
- while((hi>lowIndex) && (arr[hi]> mid))
- --hi;
- if(lo <= hi){
- swap(arr,lo,hi);
- ++lo;
- --hi;
- }
- }
- if(lowIndex < hi)
- quickSort(arr, lowIndex, hi);
- if(lo < highIndex)
- quickSort(arr, lo, highIndex);
- }
-
- }
-
- private static void swap(int arr[] ,int i, int j){
- int temp= arr[i];
- arr[i]=arr[j];
- arr[j]=temp;
- }
-
-
- public static void randomFill(int[] data,int max){
- Random gen = new Random();
- for(int i=0;i<data.length;i++){
- data[i]=gen.nextInt(max);
- }
- }
- public static void main(String[] args){
- int[] data = new int[10];
- randomFill(data , 1000);
- System.out.println(Arrays.toString(data));
- System.out.println("after sort");
- //insertSort(data);
- //SelectionSort(data);
- //bubbleSort(data);
- quickSort(data,0, data.length-1);
- System.out.println(Arrays.toString(data));
- }
- }
http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#Java
这里有很多的代码实现,我测试下了Java的一些实现.
点击(此处)折叠或打开
- package hezhb_test;
- import java.text.NumberFormat;
- import java.text.ParsePosition;
- import java.util.Scanner;
- public class IsNumeric {
- //Determine a String is a Numeric or not
-
- public static boolean isNumeric(String inputData) {
- return inputData.matches("[-+]?\\d+(\\.\\d+)?");
- }
-
- public static boolean isNumeric2(String inputData) {
- if (inputData.equals("")){
- return false;
- }
- NumberFormat formatter = NumberFormat.getInstance();
- ParsePosition pos = new ParsePosition(0);
- formatter.parse(inputData, pos);
- return inputData.length() == pos.getIndex();
- }
-
- public static boolean isNumeric3(String input){
- Scanner s = new Scanner(input);
- return s.hasNextInt();
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
-
- System.out.println(isNumeric("-1999.456"));
- System.out.println(isNumeric(""));
- System.out.println(isNumeric2("-1999.456"));
- System.out.println(isNumeric2(""));
- System.out.println(isNumeric3("-1999"));
- System.out.println(isNumeric3(""));
- }
- }
综上感觉前两个实现比较好,如果空字符串可以看作数字0,那么isNumeric2 很适用,否则用正则表达式很适用, 正则表达式的问题是太多的反斜线太难看懂了, Python的re.compile(r"") 裸字符串可以少写一个"\",
Java我还不清楚怎么写简单些.
点击(此处)折叠或打开
- true
- false
- true
- false
- true
- false
3. 贴一个linux下cal命令的Java实现.
点击(此处)折叠或打开
- package hezhb_test;
- import java.time.DayOfWeek;
- import java.time.LocalDate;
- //A implementation of Linux command "cal":
- public class Cal {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- LocalDate date = LocalDate.now().withDayOfMonth(1);
- int month;
- if(args.length >= 2){
- month=Integer.parseInt(args[0]);
- int year = Integer.parseInt(args[1]);
- date=LocalDate.of(year,month,1);
- }else{
- month=date.getMonthValue();
- }
-
- System.out.println(" Mon Tue Wed Thu Fri Sat Sun");
- DayOfWeek weekday = date.getDayOfWeek();
- int value = weekday.getValue();
- for(int i=1;i<value;i++){
- System.out.print(" ");
- }
- while(date.getMonthValue() == month){
- System.out.printf("%4d", date.getDayOfMonth());
- date = date.plusDays(1);
- if(date.getDayOfWeek().getValue() ==1)
- System.out.println();
- }
- if(date.getDayOfWeek().getValue()!= 1){
- System.out.println();
- }
- }
- }