zoukankan      html  css  js  c++  java
  • 排序算法

    public static void kuaipai(int[] arr, int lift, int right) {//快速排序
    int l = lift;
    int r = right;
    int p = arr[(lift + right) / 2];
    int temp = 0;
    while (l < r) {
    while (arr[l] < p) {
    l++;
    }
    while (arr[r] > p) {
    r--;
    }
    if (l >= r) {
    break;
    }
    temp = arr[r];
    arr[r] = arr[l];
    arr[l] = temp;

    if (arr[r] == p) {
    l++;
    }
    if (arr[l] == p) {
    r--;
    }
    }
    if (l == r) {
    l += 1;
    r -= 1;
    }
    if (lift < r) {
    kuaipai(arr, lift, r);
    }
    if (l < right) {
    kuaipai(arr, l, right);
    }
    }
    ----------------------------------------------------------------------
    //给定三个颜色的数字,只有0,1,2,然后排序
    public static int[] sort1(int[] arr){

    int zero=-1;
    int two=arr.length;
    for(int i=0;i<two;){
    if(arr[i]==1){
    i++;
    }else if(arr[i]==0){
    int a=arr[i];
    arr[i]=arr[++zero];
    arr[zero]=a;
    }else{
    int a=arr[i];
    arr[i]=arr[--two];
    arr[two]=a;


    }
    }
    return arr;
    }

    ///插入排序
    public static int[] inster(int[] arr){

    for(int i=1;i<arr.length;i++){
    int a=arr[i];
    int j=i-1;
    while (j>=0&&arr[j]>a){
    arr[j+1]=arr[j];
    j--;

    }
    arr[j+1]=a;
    }

    return arr;
    }

    //冒泡排序
    public static int[] maopao(int[] arr){

    for (int i=0;i<arr.length;i++){
    for(int j=0;j<arr.length-i-1;j++){
    if(arr[j]>arr[j+1]){
    int temp=arr[j+1];
    arr[j+1]=arr[j];
    arr[j]=temp;
    }
    }
    }
    return arr;
    }
    //选择排序
    public static int[] xuanze(int[] arr){

    for (int i=0;i<arr.length-1;i++){
    for (int j=i+1;j<arr.length;j++){
    if(arr[i]>arr[j]){
    int temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    }
    }
    }
    return arr;
    }

    public static void sort2(int[] arr,int start,int end){
    if(start<end){
    int l=start;
    int r=end;
    int middle=(l+r)/2;
    sort2(arr,l,middle);
    sort2(arr,middle+1,l);
    merger(arr, start,middle, end);
    }
    }
    private static void merger(int[] arr,int start,int middle,int end){

    int[] temp=new int[end-start+1];
    int l=start;
    int r=middle;
    int k=0;
    while(l<middle&&r<end){
    if (arr[l] <arr[r]) {
    temp[k++]=arr[l++];
    }else {
    temp[k++]=arr[r++];
    }
    if(l<middle){
    while (l<middle){
    temp[k++]=arr[l++];
    }
    }
    if(r<end){
    while (r<end){
    temp[k++]=arr[r++];
    }
    }
    }
    for (int i=0;i<k;i++){
    arr[start++]=temp[i];
    }
    }


  • 相关阅读:
    hdu 5534 Partial Tree 背包DP
    Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索
    Educational Codeforces Round 1 D. Igor In the Museum bfs 并查集
    Educational Codeforces Round 1 B. Queries on a String 暴力
    Educational Codeforces Round 1 A. Tricky Sum 暴力
    Codeforces Round #282 (Div. 1) A. Treasure 水题
    hdu 5565 Clarke and baton 二分
    hdu 5563 Clarke and five-pointed star 水题
    Codeforces Testing Round #12 C. Subsequences 树状数组维护DP
    Codeforces Testing Round #12 B. Restaurant 贪心
  • 原文地址:https://www.cnblogs.com/shanbaoxin/p/11622605.html
Copyright © 2011-2022 走看看