安卓快速排序與冒泡排序

來源:互聯網
上載者:User

標籤:uil   pos   pid   stat   for   lease   elements   tar   exp   

 冒泡排序
 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 JNICALLJava_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 JNICALLJava_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;}

 

快速排序波動大,冒泡排序更穩定,資料量越大快速排序速度優勢越明顯;

在資料大多數順序正確的情況下冒泡排序可能更快,而資料越混亂快速排序越快;

安卓快速排序與冒泡排序

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.