1. Indexing and pointer ordering
-
Because of the number of elements or the magnitude of the data, we do not want to move the elements to be sorted frequently. Therefore, the sort method of not moving an element is to maintain an indexed array or index pointer, and the goal of sorting is to rearrange the index array or pointer.
such as: Initialize for(int i = 0; i < N; i++) a[i] = &data[i];
Using indirect comparisons#define less(A, B) (*A < *B)
- The main reason for
-
Sorting using subscript or pointer is to avoid disrupting the data that you want to sort. We can sort read-only files, or sort on multiple keywords for a single file. The
Another reason is to avoid moving the entire record, and for large records (relatively small keyword values) The movement overhead is huge.
For example: struct Record {char [30]name; int num;}
int less (Item a , Item b) {return a ->num < B->num ; The
Any sort implementation will sort the pointer on the integer field keyword , based on this type;
int Less (item A, item b) {return strcmp (A->name, B->name) < 0; The
Arbitrary sort implementation will sort the pointer on the string field keyword based on this type;
-
Indirect ordering requires additional space to store subscript or pointer arrays, as well as additional space for indirect comparisons, but these costs are not significant compared to the flexibility of not needing to move data. for large records of files, we generally choose to sort indirectly .
2. List sequencing Application scope and rationale
- For the data stored in the linked list, we need to use the list sort method. In some cases, the algorithm is useful only if the data is able to support the sequential way in which the list operations are efficiently supported.
- Usually the pointers to the table nodes we manipulate are maintained by other parts of the application system (the Multilink list), which means that the sequencer can only change the links in the node and cannot change the keyword values and other information. What we need is to change the link itself so that when the link is traversed in the linked list, the nodes appear sequentially, When accessed through other links, they are not affected by their original order .
Algorithm implementation
We maintain an input list (H->next points to this list, with a table header ), and an output list (out points to this list). When the list is non-empty, traverse the list to find the largest element (because the linked list is easy to insert before the element, insert the big one, and then plug in the smaller before the large), then remove the largest element from the input table and insert it in front of the output table. Where the maximum element function Findmax () is found, the address of the previous node of the largest element node is returned. (because you want to delete a node, you must know the address of its previous node Prev->next = prev->next->next;)
C Program implementation
LinkLinklist_sort (Linkh) {LinkOut, T,Max; Out= NULLT= NULL,Max = NULL; while(h -Next!= NULL) {Max =Findmax (h); T= Max -Next//MAX element node Max -Next= Max -Next -Next//Delete max nodeT -Next=Out//Insert T before the outOut=T//Return out} h -Next=OutreturnH;}
In the processing of some tables, we do not need to implement the sorting process in detail, simply inserting a new element into the table like an insert sort to make the table orderly.
3. Keyword Index sorting principle
The so-called sort, is to organize the records in the file, so that the keyword increment (or decrement) order.
Sorted objects – A file consists of a set of records. Records are made up of several data items (or domains). One of these can be used to identify a record, called a keyword item. The value of the data item is called the keyword (key).
In the file to be sorted, if there are more than one record of the same keyword, we want to sort by keyword index, where the keyword is an integer in a small range.
For example:
1) We count the number of keywords for different values: 6 0, 4 1, 2 2, 3 3.
2) The number of keywords with local statistics smaller than other keywords: 0 keywords are smaller than 0, 6 keywords are smaller than 1, 10 keywords are smaller than 2, 12 keywords are smaller than 3.
3) According to the above statistics as an index to put the keyword in the right place: at the beginning of 0 to put the 0 keyword element 0, according to 0, increase the pointer value, put down a 0 keyword element 3 ... ); Place elements with 3 keywords starting at position 12 (12 is smaller than 3);
C Program implementation
voidKeyword_sort (Item a[],intLintR) {Item b[r-l+1];intCNT[M];//m is the number of keywords for(intj =0; J < M; J + +)//Number of initialization total 0CNT[J] =0; for(inti = l; I <= R; i++)//Count the number of keywords for different values,cnt[a[i]+1]++; for(intj =1; J < M; J + +)//Statistics The number of keywords smaller than the keyword J, cnt[j] stores the number of elements smaller than the keyword JCNT[J] = Cnt[j] + cnt[j-1]; for(inti = l; I <= R; i++)//By keyword number index into secondary array bb[cnt[A[i]]++] = a[i]; for(inti = l; I <= R; i++) A[i] = b[i-l]; }
If the file to be sorted is large, using secondary array b causes memory allocation problems, which can be avoided by using an in-place ordering to avoid the use of extra arrays.
Resources
"Algorithm: C language Implementation" p180~190
Basic Sort (iv): Index pointer sort, list sort, keyword sort