Improvements in bubbling sorting:
In order to mark whether the data exchange in the comparison, set a Boolean flag, before each comparison, the flag is set to true, if there is a data exchange in the comparison, the flag is false, after the end of a trip to Judge Flag, If it is still true (indicating that the sequencing does not have data exchange), the sort ends, or the next comparison is made.
1 Constn=Ten;2Var A:Array[1.. N ofinteger;3 I,j,t:integer;flag:boolean;4 Begin5 fori:=1 toN DoReadln (a[i]);6 forj:=1 toN-1 Do begin7 flag:= true;8 fori:=1 toN-j Do9 ifa[I] < a[i+1] Then beginTent:=a[i];a[i]:=a[i+1];a[i+1]:=T; One flag:= false; A End; - If flag thenBreak ; - End; the fori:=1 toN DoWrite (a[i]:6); - End.
to select an improvement for sorting:
Because each exchange of two data elements to execute 3 statements, excessive switching must take a lot of time. The improvement scheme is to find out the index of the largest element in the comparison of the inner loop, and consider whether to exchange it at the end of the inner loop.
1Const n=Ten;2 varA:Array[1.. N ofinteger;3 I,j,t,k:integer;4 Begin5 fori:=1 toN Doreadln (A[i]);6 fori:=1 toN-1 Do7 begin8 k:= i;9 forj:=i+1 toN DoTen ifA[K]<A[J] Then k:= J; One if K<>i Then begin At:=a[k];a[k]:=a[i];a[i]:=T; - End; - End; the fori:=1 toN DoWrite (A[i]:5); - End.
Several sorts of algorithms commonly used in competitions:
- Bubble sort
- Select sort
- Bucket sort
- Insert Sort
- Quick Sort
- Merge sort
- Heap Sort
- Binary sort Tree
Bucket sort
- The idea of barrel sequencing is that if the data to be sorted is within a finite range of obvious or predictable limits, a finite number of ordered buckets can be designed, each with a value (which can also be loaded into multiple values), sequentially outputting the values of each bucket, and the ordered sequence is obtained.
- Algorithm advantages: Time efficiency is high, time complexity is only O (n), of course, this n is not the number of data to be sorted, but the range of data to be sorted;
- Algorithm disadvantage: Obviously, when the data is relatively small, but the scope span is relatively large, the algorithm creates a huge waste of space and time.
1Enter n x 0~ -between the integers, sorted by small to large output. 2 Main code:3 fori:=0 to - Dob[i]:=0;4 fori:=1 toN Do5 begin6 read (k);7 Inc (B[k]);8 End;9 fori:=0 to - DoTen whileB[i]>0 Do begin OneWrite (i:6); A Dec (b[i]); - End;
Example One1 Enter n lowercase letters and output in dictionary order. 2 Main code:3 forch:='a' to 'Z' Dob[ch]:=0;4 fori:=1 toN Do5 begin6 read (c);7 Inc (B[c]);8 End;9 forch:='a' to 'Z' DoTen whileB[ch]>0 Do begin OneWrite (Ch:6); A Dec (b[ch]); - End;
Example Two
Insert Sort
Algorithmic thinking: There is an ordered data sequence that requires inserting a number in the already-sorted data sequence, but requires that the data sequence is still ordered after insertion, and this time a new sort method is used-insert sort, the basic operation of inserting a sort is inserting a data into the ordered data that is already sorted, In order to obtain a new, number plus one ordered data, the algorithm is suitable for the ordering of a small amount of data, the time complexity of O (n^2). is a stable sorting method.
Divides the sequence of n elements into two parts that are ordered and unordered, as follows:
{{A1},{a2,a3,a4,...,an}}
{{a1 (1), A2 (1)},{a3 (1), A4 (1) ..., an (1)}}
...
{{a1 (n-1), A2 (n-1), ...}, {an (n-1)}}
Each process is to compare the first element of an unordered sequence with the elements of an ordered sequence of numbers, to find the insertion position, and to insert the element into the appropriate position in the ordered sequence.
1To r[1.. N] Insert Sort by ascending order, r[0] It's a surveillance whistle .2 procedureInssort (varr:arraytype);3 begin4r[0]:=-maxint;5 fori:=2 toN DoInsert r[in turn2],..., R[n]6 begin7j:=i-1; x:=R[i];8 whileX<R[J] Do//find the insertion position of r[i]9 beginTenr[j+1]:=R[j]; Onej:=j-1; A End; -r[j+1]:=x; //Insert R[i] - End; the End;
Insert Sort Efficiency Analysis:
- If the goal is to arrange the sequence of n elements in ascending order, then the insertion sort is the best and worst case scenario. The best case is that the sequence is already in ascending order, in which case the comparison operation needs to be done (n-1). The worst case scenario is that the sequence is sorted in descending order, then there is a total of n (n-1)/2 times to be performed at this point. The assignment operation to insert a sort is the number of times the comparison operation is added (n-1). On average, the time complexity of inserting the sorting algorithm is O (n^2). Thus, the insertion sort is not suitable for applications with a large amount of data for sorting. However, if the amount of data that needs to be sorted is small, for example, if the magnitude is less than thousand, then inserting the sort is a good choice.
- An advantage of inserting a sort is that the data to be sorted can not be produced in advance, allowing for the sequencing of dynamically generated data, similar to the two-fork sort tree and the balanced binary tree, but the latter two are more efficient and the sorting algorithm is more complex, and we will introduce it later in the study.
- Insert sort can also be properly optimized, using dichotomy to insert the sort, in the later study we introduced the two-point search technology, students can improve their own insertion of sorting the comparison process.
Quick Sort
Basic idea: The basic idea of fast-line is essentially a kind of divide-and-conquer strategy. Any data element in the current unordered area R[1..h] as the "datum" of the comparison (as may be remembered as X), using this datum to divide the current unordered division into the left and right two smaller unordered areas: r[1..i-1] and r[i+1..h], and the left unordered sub-region data elements are less than or equal to the datum elements. The data elements in the unordered sub-region on the right are greater than or equal to the datum elements, while the Datum X is in the final sort position, that is, R[1..i-1]≤x.key≤r[i+1..h] (1≤i≤h), when R[1..i-1] and r[i+1..h] are not empty, Each of them is divided into the above process until all the data elements in the unordered sub-regions are sorted.
In order to prevent the fast row in the extreme data (for example, the data is ordered or basic order) degenerate into a bubble sort, generally take the number of intermediate positions in the unordered interval as a baseline, the sequence is as follows:
Initial keyword [38 65 97 49 76 13 27 48]
After the first Exchange [38 48 97 49 76 13 27 65]
After the second exchange [38 48 27 49 76 13 97 65]
After the third Exchange [38 48 27 13 76 49 97 65] After Exchange i=5,j=5,i=j
Continue scanning, i=5,j=4, at this time i>j, first trip sort end
"Example":
Initial keyword [38 65 97 49 76 13 27 49]
After a trip sort [38 48 27 13] [76 49 97 65]
Two times sorted [38 13 27] [48] [49] [76 97 65]
Three trips sorted [13] [38 27] [48] [49] [76 65] [97]
Final results [13] [27] [38] [48] [49] [65] [76] [97]
1 proceduresort (l,r:longint);2 var3Tmp,i,j,mid:longint; //in practical applications, variables here can be defined as global variables to save stack space4 begin5i:=l;6j:=R;7mid:=a[(I+J)Div 2];{defines the number of the current sequence in the middle position as an intermediate number datum}8 Repeat9 whileA[i]<mid DoInc (I);{look for a number larger than the middle number in the left half}Ten whileA[j]>mid DoDec (j);{in the right half, look for a number smaller than the middle.} One ifI<=j Then {If you find a set of pairs that are inconsistent with the sort target, swap them} A begin -tmp:=a[i];a[i]:=a[j];a[j]:=tmp; -Inc (I);{Keep looking .} the Dec (j); - End; - untili>j;{notice there's no equal sign here .} - ifL<j ThenSort (l,j);{If the sequence has more than 1 non-vegetarian, then recursively search the left and right intervals} + ifI<r Thensort (i,r); - End;
- The time complexity of fast sorting is O (nlog2n), fast, but it is an unstable sort method. Therefore, in terms of average time, fast ordering is currently considered to be the best internal sorting method. If the initial record sequence is ordered or basically ordered by the keyword, the quick sort will degenerate into a bubbling sort with a time complexity of O (N2).
- If we adopt a certain strategy when we choose the standard keyword, we can avoid the degradation of the fast sorting, for example, we randomly produce a value x, the keyword of the record in the X position of the sequence as the standard keyword, so that the randomization fast sorting can prevent degradation well. It is common practice to use keywords that are recorded in the middle of the sequence as standard keywords, which can simplify the code and prevent degradation.
- In terms of time, the average performance of the quick sort is better than the bubbling sort discussed previously, the sorting is selected, and the sequence method is inserted, but the fast sort requires a stack space for recursion.
Data sorting (i)