結論:java
代碼:shell
import java.util.Scanner; public class Sort{ public static void swap(int[] arr,int a,int b){ arr[a]=arr[a]+arr[b]; arr[b]=arr[a]-arr[b]; arr[a]=arr[a]-arr[b]; } /*** * 穩定,O(n²) */ public static void bubbleSort(int[] nums){ int N = nums.length; for(int i=0;i<N;i++){ for(int j=0;j<N-1-i;j++){ if(nums[j]>nums[j+1]){ swap(nums,j,j+1); } } } System.out.println("bubble:"); for(int i=0;i<N;i++){ System.out.print(nums[i]+" "); } } /** * 不穩定,O(n²) */ public static void selectSort(int[] nums){ int N = nums.length; for(int i=0;i<N;i++){ int MIN = i; for(int j=i+1;j<N;j++){ if(nums[j]<nums[MIN]){ MIN = j; } } swap(nums, i, MIN); } System.out.println('\n'+"select:"); for(int i=0;i<N;i++){ System.out.print(nums[i]+" "); } } /** * 穩定,O(n²) */ public static void insertSort(int[] nums) { int N = nums.length; for(int i=1;i<N;i++){ for(int j=i;j>0&&nums[j]<nums[j-1];j--){ swap(nums,j,j-1); } } System.out.println("\n"+"inset:"); for(int i=0;i<N;i++){ System.out.print(nums[i]+" "); } } /** * 不穩定,O(n)-O(n²) */ public static void shellSort(int[] nums){ int gap; int N = nums.length; for(gap=N/2;gap>0;gap/=2){ for(int i=gap;i<N;i++){ for(int j=i-gap;j>=0&&nums[j]>nums[j+gap];j-=gap){ swap(nums, j, j+gap); } } } System.out.println("\n"+"shell:"); for(int i=0;i<N;i++){ System.out.print(nums[i]+" "); } } /** * 不穩定,O(nlogn) */ public static void quickSort(int[] nums){ int low = 0,high=nums.length-1; quickSort(nums, low, high); System.out.println("\n"+"quick:"); for(int i=0;i<nums.length;i++){ System.out.print(nums[i]+" "); } } public static int getMiddle(int[] nums,int left,int right){ int i=left,j=right; int key=nums[left]; while(i<j){ while(nums[j]>=key&&i<j) j--; nums[i]=nums[j]; while(nums[i]<=key&&i<j) i++; nums[j]=nums[i]; } nums[i]=key; return i; } public static void quickSort(int[] nums,int low ,int high){ if(low<high){ int middle = getMiddle(nums, low, high); quickSort(nums, low, middle-1); quickSort(nums, middle+1, high); } } /** * 穩定,O(nlogn) */ public static void mergeSort(int[] nums){ int[] temp = new int[nums.length]; mergeSort(nums, 0, nums.length-1, temp); System.out.println("\n"+"merge:"); for(int i=0;i<nums.length;i++){ System.out.print(nums[i]+" "); } } public static void mergeSort(int[] arr,int left,int right,int[] temp) { if(left<right){ int mid=(left+right)/2; mergeSort(arr, left, mid, temp); mergeSort(arr, mid+1, right, temp); merge(arr, left, mid, right, temp); } } public static void merge(int[] arr, int left, int mid, int right, int[] temp){ int i=left; int j=mid+1; int t=0; while(i<=mid&&j<=right){ if(arr[i]<=arr[j]){ temp[t++]=arr[i++]; }else{ temp[t++]=arr[j++]; } } while(i<=mid){ temp[t++] = arr[i++]; } while(j<=right){ temp[t++] = arr[j++]; } t=0; while(left<=right){ arr[left++]=temp[t++]; } } public static int binSearch(int[] nums,int low,int high,int key){ if(low<=high){ int mid = low+(high-low)/2; if(nums[mid]==key){ return mid; }else if(nums[mid]>key){ return binSearch(nums, low, mid-1, key); }else{ return binSearch(nums, mid+1, high, key); } } return -1; } public static int commonBinSerach(int[] nums,int key){ int low = 0; int high = nums.length-1; int middle = 0; while(low<=high){ middle=(low+high)/2; if(nums[middle]>key){ high=middle-1; }else if(nums[middle]<key){ low=middle+1; }else{ return middle; } } return -1; } public static void main(String[] args) { int[] nums = {4,5,9,8,7,3,6,1,2,0}; System.out.println("\n"+"original:"); for(int i=0;i<nums.length;i++){ System.out.print(nums[i]+" "); } System.out.println("\n"); //bubbleSort(nums); //selectSort(nums); //insertSort(nums); //shellSort(nums); //quickSort(nums); //mergeSort(nums); System.out.println("\nplease input a num of array:"); Scanner input = new Scanner(System.in); int x = input.nextInt(); System.out.println("position: "+commonBinSerach(nums,x)); System.out.println("the position of "+x+" is: "+binSearch(nums, 0, nums.length-1, x)); input.close(); } }