Java - 陣列快速排序法 Quick Sort

By sunwc 2023-03-19 Practice

值轉換數字失敗,會拋出NumberFormatException,詳情請看private static Double isNumeric(Object obj) function

題目

取得一個Object Array,包含不同data types,排除不是數字、不是浮點型的值

再將剩下的值使用quick sort進行排序(不可使用Arrays.sort())

/**
 * @Description
 * @author sunwc
 * @Date 2023年3月19日下午10:39:17
 *
 */
public class QuickSort {
	
	public static void main(String[] args) {
		
        Object[] objArr = {"2", "0.3", 38,  null, "hello world", "100", 9.27, "-133", "null"};
		// 1. 排除任何不是numeric的值
		// 2. 使用quick sort 將 numeric array 進行排序

		double[] temp = new double[objArr.length];
		int k = 0;
		for (int i = 0; i < objArr.length; i++) {
			if (isNumeric(objArr[i]) != null) {
				temp[k++] = isNumeric(objArr[i]);
			}
		}
		// 再copy array是為了排除temp[] null值的空間
		double[] result = Arrays.copyOf(temp, k);
		
        // 快速排序
		quickSort(result, 0, result.length-1);
		System.out.println(Arrays.toString(result));
	}

	

    /**
	 * 判斷object array元素是否為數字
	 * 是數字回傳Double 不是數字回傳null
	 * @Description
	 * @author sunwc
	 * @Date 2023年3月19日下午10:37:26
	 * @param obj
	 * @return
	 */
	private static Double isNumeric(Object obj) {
		
		Double result = null;
		if (obj == null) {
			return result;
		}
		
		if (obj instanceof String) {
			String str = (String) obj;
			
			try {
				result = Double.parseDouble(str);
			} catch (NumberFormatException e) {
				result = null;
			}
			
		} else if (obj instanceof Integer) {
			int temp =  (int) obj;
			result = (double) temp;
		} else if (obj instanceof Double) {
			result = (double) obj;
		}
		return result;
	}
	
	/**
	 * 快速排序法
	 * @Description
	 * @author sunwc
	 * @Date 2023年3月19日下午10:41:24
	 * @param array
	 * @param left
	 * @param right
	 */
	public static void quickSort(double[] array, int left, int right) {
		// 快速排序的終止條件就是當left不小於right
		if (left >= right) {
			return;
		}
		
		// 取得partition的標準數
		double pivot = array[(left+right)/2];
		int partitionInx = partition(array, left, right, pivot);
		quickSort(array, left, partitionInx - 1);
		quickSort(array, partitionInx, right);
	}
	
	/**
	 * 使左邊的數比partitionInx小,右邊的數比partitionInx大
	 * @Description
	 * @author sunwc
	 * @Date 2023年3月19日下午9:29:55
	 * @param array
	 * @param left
	 * @param right
	 * @param pivot
	 * @return
	 */
	private static int partition(double[] array, int left, int right, double pivot) {
		
		while (left <= right) {
			
			// 只要左邊的值小於pivot就繼續往右移直到找到為止
			while (array[left] < pivot) {
				left++;
			}
			// 只要右邊的值大於pivot就繼續往左移直到找到為止
			while (array[right] > pivot) {
				right--;
			}
			
			if (left <= right) {
				// 兩值交換
				swap(array, left, right);
				left++;
				right--;
			}
		}
		return left;
	}
	
	
	/**
	 * 兩值交換
	 * @Description
	 * @author sunwc
	 * @Date 2023年3月19日下午9:33:43
	 * @param array
	 * @param left
	 * @param right
	 */
	private static void swap(double[] array, int left, int right) {
		double temp = array[left];
		array[left] = array[right];
		array[right] = temp;
	}
}

輸出結果:

[-133.0, 0.3, 2.0, 9.27, 38.0, 100.0]

參考來源: Algorithms: Quicksort