------------------------------------------------------------------------------------------------------
The bubble sort (bubble sort) algorithm works as follows: From the last comparison to the next two elements, if the second is smaller than the first element, then swap the two elements, until compared with the final element, so that the largest element is placed in the last This is done by repeating the comparison (n-1) times.
------------------------------------------------------------------------------------------------------
Quick Sort (quicksort) algorithm (an optimization for bubbling sorting):
1) Set two variables I, J, at the beginning of the order: i=0,j=n-1;
2) with the first array element as the key data, assign the value to key, i.e. key=a[0];
3) Forward search from J, that is, after starting the forward search (j--), find the first value less than key A[j], will a[j] and A[i] interchange;
4) Backward search from I, that is, start backward search (i++), find the first a[i] greater than key], interchange a[i] and A[J];
5) Repeat 3rd, 4, until i=j, (3,4 step, did not find the qualifying value, that is, 3 a[j] is not less than the key,4 A[i] is not larger than the time to change the value of J, I, so j=j-1,i=i+1, until 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).
Set the flag bit (sign ) (Another optimization method for bubbling sorting) each time the comparison is complete, see if the element is exchanged, and if so, continue to the next loop, and if not, end the loop.
"Set flag bit" This method does not have a "fast sorting algorithm" high efficiency.
------------------------------------------------------------------------------------------------------
The C language code is as follows:
/*** bubble sort*/ void bubble_sort (int *str, int size) { int i = 0, j = 0; int tmp = 0; /* ** Go size-1 sort; */ for (i = 0; i < size - 1; i++) { /* ** each sort of trip, will be the largest element sink. Next trip less compare I times; */ for (j = 0; j < size - 1 - i; j++) { if (str[j] > str[j + 1]) { tmp = str[j]; str[j] = str[j + 1]; str[j + 1] = tmp; } } }}/*** Optimize one: Set a flag for sign Bubble sort;*/ void bubble_sort (int *str, int size) { int i = 0, j = 0; int tmp = 0, sign = 0; for (i = 0; i < size - 1; i++) { /* ** sign 0 before each trip If the neighboring elements are exchanged then sign>1; ** otherwise, sign==0, without swapping, Sort done, jump out of the loop; */ flag = 0; for (j = 0; j < size - 1 - i; j++) { if (str[j] > str[j + 1]) { tmp = str[j]; str[j] = str[j + 1]; str[j + 1] = tmp; sign++; } } if (0 == sign) break; }}/*** Optimization II: Quick sort;*/void quicksort (int *str, int left, int right) { assert (str); /* ** if the left is greater than or equal to the right, the array is already sorted; */ if (left >= right) { return; } int i = left; int j = right; int key = str[left]; /* ** when the i=j, a trip to sort of completed, Divide all the numbers into one big and two groups; */ while (I&NBSP;<&NBSP;J) { /* ** the first time from the backward forward, encountered the first smaller than key exchange two number of positions; */ while (i < j) && ( KEY&NBSP;<=&NBSP;STR[J]) { j--; } str[i] = str[j]; /* ** the second time in front of the previous traversal, encountered the first larger than key exchange two number position; */ while&nbSP; ((I&NBSP;<&NBSP;J) && (Key >= str[i]) { i++; } str[j] = str[i]; } str[i] = key; /* ** recursive call, complete the ordering of left and right sub-sequences; */ quicksort ( str, left, i - 1); quicksort (str, i + 1, right);}
------------------------------------------------------------------------------------------------------
Little knowledge of dry goods:
When n is larger, it should be sorted by the time complexity O (nlog2n): Quick sort, heap sort, or merge sort order.
Quick sort: is currently considered the best method based on the comparison of the internal sort, when the keyword to be sorted is randomly distributed
, the average time for fast sorting is the shortest;
------------------------------------------------------------------------------------------------------
This article is from the "Nobody" blog, please be sure to keep this source http://814193594.blog.51cto.com/10729329/1715944
Two optimizations for "summary" bubble sort and bubble sort