Android Quick sort and bubble sort

Source: Internet
Author: User

Bubble sort
 Privatevoid 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;}  
                                  

Quick Sort
private void QuickSort (Int[]Arrayint start,IntEnd) {Intleft = start;Intright =Endif (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++; Span class= "hljs-built_in" >array[right] = array[left]; } array[right] = temp; QuickSort ( array, start, left-1); QuickSort (array, right + 1, Span class= "Hljs-keyword" >end); } } 

C + + bubble sort notation
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++) { Span class= "Hljs-keyword" >for (int j = 0; j < Length-i; J + +) { Span class= "Hljs-keyword" >if (array[j] > array[j + 1]) {swap (array, J, j + 1);}} Jintarray Ji Ntarray = Env->newintarray (length); Env->setintarrayregion (Jintarray, 0, length, array); env- >releaseintarrayelements (Array_, array, 0); return Jintarray;} 
/span>
C + + Quick sort notation
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;}            

Fast sort fluctuation, bubble sort more stable, the larger the data volume, the more obvious the faster the sorting speed advantage;

Bubble sorting can be faster when most of the data is in the correct order, and the more chaotic the data, the faster the sorting;

Android Quick sort and bubble sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.