|
|
|||||
|
|
Start of topic | Skip to actions
Sorting AlgorithmsThe project is divided into two phases: First, we want to implement the common sorting algorithms on both sized lists and regular lists and compare their speed. Second, we want to use a more expressive data type for the output list reflecting the fact that the list is not only of same size of the input but that it is actually sorted. Here is what we did in the first phase: Insertion Sort (Dan)Insertion sort mainly uses an insert function to insert elements in a sorted list (initially empty) in such a way that the list remains sorted. The following function
let rec u_insert l x cmp =
match l with
| [] -> [x]
| y::ys -> match(cmp x y with
|true -> y :: (u_insert ys x cmp)
|false -> x :: (y::ys)
Using that function, insertion sort was implemented in a straight forward way both for regular and sized lists. No code explosion for sized lists and no important change in speed. Quick Sort (Dan)Quick Sort uses the famous divide and conquer approach. It first splits the list into two smaller lists (one with all elements less than a selected pivot element and one with all elements greater than or equal to that same element), then it sorts the sublists and finally concatenates the resulting lists (without forgetting the pivot) The following
let rec u_qs l cmp =
match l with
|[] -> []
|x::xs -> let partition lt gt l2 =
match l2 with
| [] -> (u_qs lt) @ x::(v_qs gt)
| y::ys -> match cmp x y with
| true -> partition lt (y::gt) l2
| false -> partition (y::lt) gt l2
in partition [] [] xs
Implementation for regular lists was very easily but it was difficult to implement for sized lists (not yet done). The reason is that it is difficult to prove that the (size of the sublists after partitioning) + one (the pivot) is equal to the size of the original list. It is do-able but it requires a lot of work. Bubble Sort (Jun)Very easy and straight forward to implement for both regular and sized lists. (done). Merge Sort (Jun)This is also a divide and conquer algorithm. This is again a little problematic for sized lists (not done yet). The sorting here occurs when you merge back the parts while in Quick sort the sorting occurs when you split. To be able to prove that the resulting list is of the correct size we might need to use the following inductive type definition for natural numbers (to be able to distinguish even from odd numbers). type '(n) xnat = | Z : '(0) xnat | E of '(n) xnat : '(2*n) xnat | O of '(n) xnat : '(2*n+1) xnat Note: That this is exactly like the definition we used to define the typed Braun tree node. The first problem is that we need to be able to build an xnat, we might also need to prove that f = /\'(m>n)....and then f.|'(m2>n2)| ....We can use a split having the following type: forall n. '(2 * n) slist -> '(n) slist * '(n) slistNote that we are getting away from our primary goal which is to verify the list sorting which is the second phase. Things are getting a little bit complicated while we are still in the first phase. Next time we will procede to the second phase. So the type of the sorting function should be: sort: '(n) slist -> (alpha -> alpha -> bool) -> '(n) sortedlistThe second parameter is a comparator like "less than or equal". a0 < a1 < a2 ..... < an The comparison operator to be passed to the sort function must have the following properties, in order that the sort be (totally) correct:
Topic Actions: Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r5 < r4 < r3 < r2 < r1 | More topic actions
Webs: Main | TWiki | Africa | EmbeddedSystems | Gpce | Houston | International | K12 | MetaOCaml | MulticoreOCR | ProgrammingLanguages | RAP | RIDL | Sandbox | SpeechClub | Teaching | Texbot | WG211 Web Actions: |
||||
This work is licensed under a Creative Commons Attribution 2.5 License. Please follow our citation guidelines.