Sorting implementation

xiaoxiao2021-03-06  69

Sorting (Sorting) sorted into the interior (internal sorting) and external sorting (external sorting) the structure is defined as follows: #define maxsize 20typedef int Keytype; typedef struct {Keytype key; Infotype other;} Redtype; typedef struct {Redtype r [maxsize 1]; // R [0] made a sentinel unit Int length;} SQList;

Time complexity and spatial complexity

The following algorithm is the internal sorting method of the sentinel unit, bubble sorting void bubblesort (int Data [], int N) {INT I, J; int Temp; for (i = 0; i Data [J 1]) {temp = data [j]; data [j] = data [j 1]; DATA [J 1] = Temp;}}}} Sampling results Stable 2, selection Sorting Void SelectSort (int Data [], int N) {INT I, J; Int min, Temp; For (i = 0; i

}} Sampling results Unstable 3, insertion (1) STRIGHT INSERTION SORTING VOID INSERTSORT (int Data [], int N) {Int i, j; int Temp;

For (i = 1; i = 0) {data [j 1] = data [J ]; J -;} DATA [J 1] = Temp;}} Sampling results Stable (2) Hill Sort (Int Data [], INT N) {Int Gap, I, J, Temp ;

For (GAP = N / 2; GAP> 0; GAP / = 2) {for (i = gap; i = 0) {data [j gap] = data [j]; j = j-gap;} DATA [J GAP] = TEMP;}}} Sampling results unstable

4, Quick Sorting - Improved INT QS (Int Data [], INT LEFT, INT Right) {Int Temp; Temp = Data [Left]; WHILE (Left Temp) {Right - Data [Left ] = DATA [Right]; WHIL (Left

Void Quicksort (int data) {int base; if (Left

Sorting results are unstable 5, Merge Sorting

Sorting results Stable 6, Heap Sorting Sort Sediment Unit

7. The base sorting result is stable

Hill sort, select sort, rapid sorting, stacking is unstable sort. Other sorts are stable sorting. 1-3 is simple sorting, 4-8 is advanced.

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 substantially orderly (referring to the positive order), it should be used to use direct intercept, bubbling, random sorting is appropriate; (3) If N is large, the time complexity should be used as (NLGN) Sorting method: rapid sorting, stack sorting or sorting.

Rapid sorting is currently considered to be the best method based on the internal sort of comparison. When the keyword to be sorted is randomly distributed, the average time of rapid sorting is shortest; the auxiliary space required for the stacking routine is less sorted, and not There will be worst cases that may appear rapidly. These two sorts are unstable.

Void MS (int DATA [], INT LEFT, INT BASE, INT Right) {INT I, J; For (i = Base 1, J = Left; Left

转载请注明原文地址:https://www.9cbs.com/read-87690.html

New Post(0)