The sorting algorithm is the content of the data structure discipline, in which the internal sorting existing algorithms have many kinds, what is the characteristics of each? This brings graph design realizes the commonly used internal sorting algorithm and compares. Sort by bubbling, direct insertion, simply sequencing, rapid sorting, stack sorting, test comparison for the number of comparisons of keywords, and number of movements.
Problem analysis and overall design
ADT ORDERABLIST {
Data objects: d = {ai | ai∈integerseet, i = 1, 2, ..., n, n≥0}
Data relationship: r1 = {
Basic operation:
Initlist (n)
Operation results: Construct a length of N, element value is 1, 2, ..., n.
Randomizel (D, isinverseorser)
Operation results: random disruption
Bubblesort ()
Operation result: Sort by bubble
INSERSORT ()
Operation result: Insert Sort
SELECTSORT ()
Operation result: Select Sort
Quicksort ()
Operation results: quick sort
HEAPSORT ()
Operation result: Stack sort
Listtraverse (Visit ())
Operation results: Call the function Visit () in turn on each element of the L
} ADT ORDERABLIST
The keywords for the elements of the designer table are integers. Test comparisons with different data in positive order, inverse ordered and different chart.
Test comparisons for the number of comparisons and movements of keywords (keyword exchange gauges 3 times).
Require display information, the user inputs the number of the table length (100-1000) and different test data by the keyboard (8-18). Each test is completed, requiring the column performance is a comparison result.
The results are required to analyze the results.
detailed design
1, bubble sorting
Algorithm: Core thinking is to scan data list and find two adjacent items that have a downturn. When these two projects are found, the location of the project is swapped and then continues to scan. Repeat the above operation until all items are placed in order
Bubblesort
Struct REC R [],
INT N)
{
INT I, J;
Struct REC W;
Unsigned long
INT COMPARE = 0, MOVE = 0;
For (i = 1; i <= n-1; i )
For (j = n; j> = i 1; j -)
{
IF (R [J] .Key { w = r [j]; R [j] = r [j-1]; R [j-1] = W; Move = Move 3; } COMPARE ; } Printf ("/ nbubblesort compare =% ld, move =% ld / n", compare, move); } 2, direct insertion sort Algorithm: After I-1 is treated, L [1..i-1] has a sequence. The second handling only the L [I] inserts the appropriate position of L [1..i-1] such that L [1..i] is a sequence of sequence. To achieve this, we can use the method of comparing. First compare L [I] and L [I-1], if L [I-1] ≤ L [i], then L [1..i] has been ranked, the clutch is over; otherwise L [I] and L [I-1] position, continue to compare L [I-1] and L [I-2] until a certain position j (1 ≤ J ≤ i-1) makes L [J ] ≤ L [J 1] INSERTSORT Struct REC R [], INT N) { Int i, j; unsigned long INT COMPARE = 0, MOVE = 0; For (i = 2; i <= n; i ) {Compare ; R [0] = r [I]; Move ; J = I-1; While (r [0] .key {R [J 1] = R [J]; J -; Move ; Compare; R [J 1] = R [0]; Move ; } Printf ("/ Ninsertsort Compare =% LD, Move =% LD / N", Compare, Move } 3. Simple selection order Algorithm: First find the smallest data in the data list, then exchange this data to the first data switching; next to the second small data, then exchange the same second data exchange location, and so on. SELECTSORT (Struct Rec R [], INT N) { Unsigned long INT COMPARE = 0, MOVE = 0; INT I, J, K; Struct REC W; For (i = 1; i <= n-1; i ) {K = i; For (j = i 1; j <= n; j ) { IF (r [j] .key> r [k] .key) {k = j; compare ;} W = r [i]; R [i] = r [k]; R [k] = W; Move = Move 3; } } Printf ("/ NSelectSort Compare =% LD, Move =% LD / N", Compare, Move } 4, quick sort Algorithm: First check the number of data in the data list, if less than two, exit the program directly. If there are more than two data, select a split point to divide the data into two parts, less than the split point of the data is placed in one group, and the rest is placed in another group, and then sorted two sets of data. Usually the data of the split point is randomly selected. This is similar to whether your data has been arranged, the size of the two word lists you divide is almost almost. And as long as the size of the two sub-lists is almost Q (Struct REC R [], Int S, INT T) { INT i = S, J = T; IF (S { R [0] = R [S]; A; C ; Do { While (j> i && r [j] .key> = r [0] .key) {J-; a; IF (i {R [i] = r [j]; i ; C ;} While (I {i ; a; IF (i {r [j] = r [i]; J -; C ;} } While (i R [i] = r [0]; C ; Q (R, S, J-1); Q (R, J 1, T); } } 5. Stack sort (1) Basic ideas: Pile sorting is a tree selection sort, in the ordering process, the R [1..n] is regarded as a complete binary tree, using the intrinsic relationship between the dual-proof point and the child's node To choose the smallest element. (2). The definition of the heap: Sequences K1, K2, K3, ..., Kn of n elements are called a heap, when and only when the sequence is satisfied: Ki ≤ K2i Ki ≤ K2i 1 (1 ≤ i ≤ [N / 2]) SIFT Struct REC R [], Int L, Int m) { INT I, J; Struct REC W; i = L; j = 2 * i; W = r [i]; While (j <= m) { IF (j } IF (W.Key { R [i] = r [j]; i = j; J = 2 * i; } ELSE J = M 1; } R [i] = W; } HEAPSORT Struct REC R [], INT N) { Unsigned long INT COMPARE = -1, Move = -1; Struct REC W; INT I; Int a; For (i = N / 2; I> = 1; I -) A = SIFT (R, I, N); COMPARE ; Move ; For (i = n; i> = 2; i -) { W = r [i]; R [i] = r [1]; R [1] = W; A = SIFT (R, 1, I-1); Compare = a; Move = a; } } summary: 1. Learn to use a random function randomize () to set the initial value to add #include in the header file. 2. Basically, it is basically completed after a variety of sorting processes before doing this procedure. 3. Summary of sorting algorithms: (1) If N is smaller (eg n≤50), direct insertion or direct selection can be used. When the record size is small, the direct insertion sort is better; otherwise, because the number of records directly selects the mobile recorder is less than the direct plug, the selection is selected to be selected. (2) If the file initial state is basically ordered (refer to the positive order), the quick sorting of direct interceptors, bubble or random should be selected; (3) If N is large, the order of time complexity is O (NLGN) should be used: rapid sorting, stack sorting or sorting. Rapid sorting is currently considered to be the best method based on internal sorting of comparison. When the keyword to be sorted is randomly distributed, the average time of rapid sorting is shorter; The auxiliary space required for stacking is less rapidly sorted and there is no worst case that may occur quickly. These two sorts are unstable.