国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java 實現8種常見排序

2019-11-06 07:11:56
字體:
來源:轉載
供稿:網友
/** * Created by changqing on 2017/3/5.快排 */public class main {    public static void main(String[] args) {        int []nums={1,6,6,8,2,2,5,78,8,21,2,124,4};        QuickSort(nums,0,nums.length-1);        for (int i = 0; i < nums.length; i++) {            System.out.PRintln(nums[i]);        }    }    public static void QuickSort(int []numbers,int left,int right)    {        if(left>=right)            return;        int x=numbers[(left+right)/2];        int low=left,high=right;        while(low<high)        {            while(numbers[high]>x)            {                high--;            }            while(numbers[low]<x)            {                low++;            }            if(low<=high)            {                int temp=numbers[low];                numbers[low]=numbers[high];                numbers[high]=temp;                low++;                high--;            }        }        QuickSort(numbers,left,high);        QuickSort(numbers,low,right);    }}
/** * Created by changqing on 2017/3/5. 選擇排序 */public class selectSort {    public static void main(String[] args) {        int []nums={1,6,6,8,2,2,5,78,8,21,2,124,4};        SelectSort(nums);        for (int i = 0; i < nums.length; i++) {            System.out.println(nums[i]);        }    }    public static void SelectSort(int []nums) {        int size=nums.length;        for(int i=0;i<size;i++)        {            int k=i;            for(int j=size-1;j>i;j--)            {                if(nums[j]<nums[k])                    k=j;            }            int temp=nums[i];            nums[i]=nums[k];            nums[k]=temp;        }    }}
import java.util.Arrays;/** * Created by changqing on 2017/3/5.歸并排序 */public class MergeSort {    public static void merge(int[] a, int low, int mid, int high) {        int[] temp = new int[high - low + 1];        int i = low;// 左指針        int j = mid + 1;// 右指針        int k = 0;        // 把較小的數先移到新數組中        while (i <= mid && j <= high) {            if (a[i] < a[j]) {                temp[k++] = a[i++];            } else {                temp[k++] = a[j++];            }        }        // 把左邊剩余的數移入數組        while (i <= mid) {            temp[k++] = a[i++];        }        // 把右邊邊剩余的數移入數組        while (j <= high) {            temp[k++] = a[j++];        }        // 把新數組中的數覆蓋nums數組        for (int k2 = 0; k2 < temp.length; k2++) {            a[k2 + low] = temp[k2];        }    }    public static void mergeSort(int[] a, int low, int high) {        int mid = (low + high) / 2;        if (low < high) {            // 左邊            mergeSort(a, low, mid);            // 右邊            mergeSort(a, mid + 1, high);            // 左右歸并            merge(a, low, mid, high);            System.out.println(Arrays.toString(a));        }    }    public static void main(String[] args) {        int a[] = { 51, 46, 20, 18, 65, 97, 82, 30, 77, 50 };        mergeSort(a, 0, a.length - 1);        System.out.println("排序結果:" + Arrays.toString(a));    }}
public class main {    public static int sort(int da[], int a) {        int len = da.length;        int left = 0, right = len - 1;        int mid = 0;        while (left <= right) {            mid = (left + right) / 2;            if (a < da[mid]) {                right = mid - 1;            } else if (a > da[mid]) {                left = mid + 1;            } else {                return mid;            }        }        return -1;    }    public static void main(String[] args) {        int a[] = {1, 2, 6, 87, 456, 899};        int result = sort(a, 899);        System.out.println(result);    }}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兴山县| 堆龙德庆县| 临猗县| 监利县| 资源县| 奉贤区| 鲁甸县| 鹤壁市| 昌邑市| 彰化县| 昔阳县| 锡林郭勒盟| 新宾| 扬州市| 安阳市| 桐乡市| 巴林左旗| 卫辉市| 子长县| 马山县| 鄱阳县| 澄迈县| 泰顺县| 金昌市| 汪清县| 乐昌市| 汉寿县| 洛川县| 察隅县| 阳原县| 宝山区| 福安市| 湖州市| 外汇| 保德县| 安丘市| 宁乡县| 九龙城区| 宝清县| 鲁山县| 商河县|