安卓快速排序与冒泡排序

【安卓快速排序与冒泡排序】但使书种多,会有岁稔时。这篇文章主要讲述安卓快速排序与冒泡排序相关的知识,希望能为你提供帮助。
  冒泡排序

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; }

 
快速排序波动大,冒泡排序更稳定,数据量越大快速排序速度优势越明显;
在数据大多数顺序正确的情况下冒泡排序可能更快,而数据越混乱快速排序越快;


    推荐阅读