Simple sort, bubble sort, cocktail sort, quick sort

Source: Internet
Author: User

Simple sort: Each time you find the maximum (small) number, sort from the beginning.

1  Public Static int[] Simplysort (inta[]) {2          for(inti = 0; i < a.length-1; i++) {3 //int min = a[i];4             intMinindex =i;5              for(intJ =i + 1; J < A.length; J + +) {6 //My Code7 //if (A[j] < min) {8 //min = a[j];9 //minindex = j;Ten //                } One //Teacher Code A                 if(A[j] <A[minindex]) { -Minindex =J; -                 } the             } - //int m = a[i]; - //A[i] = A[minindex]; - //A[minindex] = m; +             if(I! =Minindex) {
"^" For XOR or -A[i] = A[i] ^A[minindex]; +A[minindex] = A[i] ^A[minindex]; AA[i] = A[i] ^A[minindex]; at } - - } - returnA; -}

Counterfeit sort:

      • Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
      • Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
      • Repeat the above steps for all elements, except for the last one.
      • Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.
1 Public int[] Getsort (int[] Array) { 2Booleanswapped =true;//Loop Control3 for(inti = 0; Swapped && i < array.length-1; i++) { 4 swapped =false; 5 for(intj = 0; J < Array.length-i-1; J + +) { 6if(Array[j] > array[j+1]) { 7inttemp =Array[j];8 Array[j] = array[j+1]; 9 Array[j+1] =temp;Ten swapped =true;//when the exchange is not done, that is, the sequence is already orderly, do not cycle11                 }12             }13         }14returnArray;15}

Cocktail Order:

Different from bubble sort:
The cocktail sort equals a slight distortion of the bubbling sort. The difference is from low to high and from high to low, while bubble sorting is only low to high to compare each element in the sequence. He can get a little bit better performance than bubble sort, because the bubble sort only moves from one Direction to the next (from low to high) and only one item per loop.
In the case of sequences (2,3,4,5,1), cocktail ordering requires only one sequence of visits to complete the sort, but it takes four times to use bubble sorting.
Complexity of:
Cocktail ordering the worst or average number of times spent is O (n^2), but if the sequence has been largely sorted at the beginning, it will approach O (n).

1  Public Static int[] Cocktailsort (int[] a) {2          for(inti = 0; i < A.LENGTH/2; i++) {3              for(intj = 0; J < a.length-i-1; J + +) {4                 if(A[j] > a[j + 1]) {5                     intTEM =A[j];6A[J] = a[j + 1];7A[j + 1] =tem;8                 }9             }Ten              for(intj = a.length-i-1; J > 0; j--) { One                 if(A[j] < a[j-1]) { A                     intTEM =A[j]; -A[J] = a[j-1]; -A[J-1] =tem; the                 } -             } -         } -         returnA; +}

Quick sort: is an improvement to the bubbling sort. by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.

Specific steps:

1) Set two variables I, J, when sorting begins: I=0,j=n-1;2) takes the first array element as the key data, assigns the value to Key, i.e. Key=A[0];3) forward search from J, that is, after the start of a forward search (j--), find the first less than KeyValue a[j], a[j] and A[i] interchange, 4) from I start backward search, that is, to start backward search (i++), to find the first one greater than KeyA[i], will a[i] and A[j] interchange; 5) Repeat 3rd, 4, until i=j; (3,4 step, no value matching the criteria, that is, 3 a[j] is not less than Key, 4 A[i] not greater than KeyChange the value of J, I so that j=j-1,i=i+1 until it is found. Locate the value that matches the condition, and the J pointer position does not change when I exchange it. In addition, I==J this process must be exactly when the i+ or J completes, at which time the loop ends).
1  Public Static int[] QuickSort (int[] A,intStartintend) {2         intLow =start;3         intHigh =end;4         intPivot =A[low];5          while(Low <High ) {6              while(Low < High && A[high] >=pivot) {7high--;8             }9             if(Low < High && A[high] <pivot) {Ten                 intTEM =A[low]; OneA[low] =A[high]; AA[high] =tem; -low++; -             } the              while(Low < High && A[low] <pivot) { -low++; -             } -             if(Low < High && A[low] >=pivot) { +                 intTEM =A[low]; -A[low] =A[high]; +A[high] =tem; Ahigh--; at             } -         } -         if(Low-1 >start) { -QuickSort (A, start, low-1); -         } -         if(High + 1 <end) { inQuickSort (A, low + 1, end); -         } to          +         returnA; -}

Simple sort, bubble sort, cocktail sort, quick 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.