zoukankan      html  css  js  c++  java
  • 安卓快速排序与冒泡排序

     

    冒泡排序

     private void swap(int[] arrays, int i, int j) {
            int temp;
            temp = arrays[i];
            arrays[i] = arrays[j];
            arrays[j] = temp;
        }
    
        public int[] arrayIntSort(int[] array) {
            for (int i = 1; i < array.length; i++) {
                for (int j = 0; j < array.length - i; j++) {
                    if (array[j] > array[j + 1]) {
                        swap(array, j, j + 1);
                    }
                }
            }
            return array;
        }

    快速排序

    private void quickSort(int[] array, int start, int end) {
            int left = start;
            int right = end;
            if (left < right) {
                int temp = array[left];
                while (left != right) {
                    while (left < right && temp <= array[right])
                        right--;
                    array[left] = array[right];
                    while (left < right && temp >= array[left])
                        left++;
                    array[right] = array[left];
                }
                array[right] = temp;
                quickSort(array, start, left - 1);
                quickSort(array, right + 1, end);
            }
        }

    c++冒泡排序写法

    
    void swap(jint *arrays, int i, int j) {
        int temp;
        temp = arrays[i];
        arrays[i] = arrays[j];
        arrays[j] = temp;
    }
    
    JNIEXPORT jintArray JNICALL
    Java_com_cpf_ndkdemo_MainActivity_arrayIntSortByNative(JNIEnv *env, jobject instance,
                                                              jintArray array_) {
        jint *array = env->GetIntArrayElements(array_, NULL);
        int length = env->GetArrayLength(array_);
        for (int i = 1; i < length; i++) {
            for (int j = 0; j < length - i; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                }
            }
        }
        jintArray jintArray = env->NewIntArray(length);
        env->SetIntArrayRegion(jintArray, 0, length, array);
        env->ReleaseIntArrayElements(array_, array, 0);
        return jintArray;
    }
    

    c++快速排序写法

    
    void fastSort(jint *array, jint start, jint end) {
        int left = start;
        int right = end;
        if (left < right) {
            int temp = array[left];
            while (left != right) {
                while (left < right && temp <= array[right])
                    right--;
                array[left] = array[right];
                while (left < right && temp >= array[left])
                    left++;
                array[right] = array[left];
            }
            array[right] = temp;
            fastSort(array, start, left - 1);
            fastSort(array, right + 1, end);
        }
    }
    
    JNIEXPORT jintArray JNICALL
    Java_com_cpf_ndkdemo_MainActivity_arrayFastIntSortByNative(JNIEnv *env, jobject instance,
                                                                  jintArray array_, jint start,
                                                                  jint end) {
        jint *array = env->GetIntArrayElements(array_, NULL);
        fastSort(array, start, end);
        jintArray jintArray = env->NewIntArray(end + 1);
        env->SetIntArrayRegion(jintArray, 0, end + 1, array);
        env->ReleaseIntArrayElements(array_, array, 0);
        return jintArray;
    }
    

    快速排序波动大,冒泡排序更稳定,数据量越大快速排序速度优势越明显;

    在数据大多数顺序正确的情况下冒泡排序可能更快,而数据越混乱快速排序越快;

  • 相关阅读:
    自定义View的ToolBar布局报错Error:(2) No resource identifier found for attribute 'context' in package 'c
    在学git之主分支 branch
    获取发布版SHA1
    关于开启线程与UI的操作
    播放音频和视频(VideoView控件)
    通知栏Notification的应用
    Android 真机调式 Installation failed with message 远程主机强迫关闭了一个现有的连接。. It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing. WA
    运行程序申请危险权限
    mysql乐观锁总结和实践
    Nginx配置文件nginx.conf中文详解
  • 原文地址:https://www.cnblogs.com/xgjblog/p/9413063.html
Copyright © 2011-2022 走看看