contributed by < RZHunagJeff
>
linux2021
With a singly linked list defined as follows:
Given following program:
And the corresponding test bench:
Complete the program, which makes the program work properly.
In order to answer the question, the program should be analyzed. Following is my analysis flow.
First, I started with the test bench, in the main
function of test bench, it shows that the list is generated by 20 random number. After display the original list, the list will be sorted, by calling function quicksort
, and display it again after sorting. After all, the order of the list will be checked whether it is in acensing order by function list_is_ordered
. The flow of test bench is shown below:
Then, I start to analyze function quicksort
. To demonstrate, the list shown below is used:
Following are the steps in quicksort
:
Check the list: check if given list is empty. Function will continue only when the list is not empty.
Pick a pivot: the first element in the list will be choosen as pivot of this round.
After these lines are evaluated:
Spilt the list: go through remaining elements of the list, reattach them to other two lists named left
and right
, depends on the value of the element. An element will be attached to right
if it holds a greater value than pivot does, to left
, otherwise.
As we can see later(step 5), left
will be concatenated to result
first, which means that left
should contain elements less than pivot and others will be attached to right
. And with the definition of list_add_node_t
, the type of first argument passed to list_add_node_t
is node_t**
, so we can conclude that AAA
is &right
and BBB
is &left
.
After spliting:
Recursive operation: call quicksort
recursively with left
and right
as arguments. After this step, elements in left
and right
will be ranked in acensing order.
After return from recursive calls:
Rebuild the list: define an empty list named result
, concatenate left
, pivot
and right
with it one after another. At this moment, we can find that CCC
would be list_concat(&result, pivot); list_concat(&result, right);
, which attaches pivot
to result
before right
. Finally, result
holds the result of sorting and will be set back to list
.
After all:
With steps mentioned above, the list will be sorted in acensing order.
There is another function that aims to concatenate lists, that is list_concat
.
In order to attach right
to the tail of left
, the tail of left
should be found first, that is what the while loop around line 11 - 12 does. We can find that LLL
should be left = &((*left)->next)
, which moves left
to the tail of first list.
The generator that being choosen here is Mersenne Twister[1].
There are several reasons of why choosing this algorithm:
The MT number generator is based on following algorithm:
Notation:
: the size in bits of generated number
: an integer, which is the degree of recurrence
: an integer with in range
: the cut point of combination of two numbers (mention below) where
: the number generated by MT algorithm
: the upper bits of
: the lower bits of
: a matrix with entries in , this matrix is in form of , which makes mutiplication by is very fast.
To improve accuracy, the generated number should be transformed by following transformation:
where is the final result of generation.
This is a version of Mersenne Twister, with following parameters are given:
where is the bottom of matrix .
The program shown above is my implementation of MT19937. For this implementation, there are several things require detail explanation.
First one is the if statement at line 8, the design of this part of program is to generate N
random numbers at a time, that is, replace the content of buffer with a bunch of new numbers at a time.
Second is the body of if statement around line 9 - 22, as we can see in the recursive algorithm shown above, the number requires , and number, in order to simplify the program, the loop to fill buffer with new numbers was divided into three parts, with in each part, the loop does no need to concern about possibility of out of range.
Last one is codes around line 24 - 29, which does the final transformation that mentioned about.
quicksort
The basic concept of my implementation of non-recursive quicksort is base on the content of this website. And the following is my program to perform non-recursive quicksort.
To perform quicksort without recursive function calls, it is important to record the head and tail of each partition manually, which will be maintained in array begin
and end
, and the variable i
acts as stack pointer that shows the partition should be done with for each loop iteration.
There is a huge different between sorting elements in array and elements maintained with linked-list. The order between each element will be maintained automatically while reordering some elements in array, while the order between each node in a linked-list is represented by "link" from one node points to another, which means that "link" should be re-linked while the order is changed, that is why begin
and end
are in type of node_t **
. Since that the content of begin
are addresses of pointer that points to the head of coresponding partition, the partition is able to be linked back into the list by just modifying the pointer that points to it, which is record in content of begin
.
The main difference between these two kinds of lists is that the list implemented in linux-list has only pointers to other nodes in list_head
structure, which means that for any program requires feature of linked list, they do not need to re-implement their own version of linked list, all they have to do is containing list_head
in their own structure, and hand off the work of maintaining list to the functions that are already written. Another advantage of abstracting a linked list in this way is that many different structures can be contained in a single list, as long as list_head
is contained in these structures.
This function looks similar as non-recursive version that performs on singly-linked list. In this function, a partition is marked by recording the previous node of its first node in beg
, and the next one of its last node in end
, base on these records, we can perform in place sorting. With two built in functions list_move_tail
and list_move
, which will attach item
as previous node or next node of pivot
respectively, the rank of nodes in given partition can be easily reordered with nodes less than pivot
in front of it and greater ones at its back.