author | bulwahn |
Mon, 03 Oct 2011 14:43:13 +0200 | |
changeset 45116 | f947eeef6b6f |
parent 44604 | 1ad3159323dc |
child 58889 | 5b7a9633cfa8 |
permissions | -rw-r--r-- |
30738 | 1 |
(* Author: Tobias Nipkow |
24615 | 2 |
Copyright 1994 TU Muenchen |
3 |
*) |
|
4 |
||
40349 | 5 |
header {* Quicksort with function package *} |
24615 | 6 |
|
7 |
theory Quicksort |
|
44604 | 8 |
imports "~~/src/HOL/Library/Multiset" |
24615 | 9 |
begin |
10 |
||
11 |
context linorder |
|
12 |
begin |
|
13 |
||
28041 | 14 |
fun quicksort :: "'a list \<Rightarrow> 'a list" where |
37075
a680ce27aa56
modernized sorting algorithms; quicksort implements sort
haftmann
parents:
30738
diff
changeset
|
15 |
"quicksort [] = []" |
a680ce27aa56
modernized sorting algorithms; quicksort implements sort
haftmann
parents:
30738
diff
changeset
|
16 |
| "quicksort (x#xs) = quicksort [y\<leftarrow>xs. \<not> x\<le>y] @ [x] @ quicksort [y\<leftarrow>xs. x\<le>y]" |
a680ce27aa56
modernized sorting algorithms; quicksort implements sort
haftmann
parents:
30738
diff
changeset
|
17 |
|
a680ce27aa56
modernized sorting algorithms; quicksort implements sort
haftmann
parents:
30738
diff
changeset
|
18 |
lemma [code]: |
a680ce27aa56
modernized sorting algorithms; quicksort implements sort
haftmann
parents:
30738
diff
changeset
|
19 |
"quicksort [] = []" |
a680ce27aa56
modernized sorting algorithms; quicksort implements sort
haftmann
parents:
30738
diff
changeset
|
20 |
"quicksort (x#xs) = quicksort [y\<leftarrow>xs. y<x] @ [x] @ quicksort [y\<leftarrow>xs. x\<le>y]" |
a680ce27aa56
modernized sorting algorithms; quicksort implements sort
haftmann
parents:
30738
diff
changeset
|
21 |
by (simp_all add: not_le) |
24615 | 22 |
|
23 |
lemma quicksort_permutes [simp]: |
|
24 |
"multiset_of (quicksort xs) = multiset_of xs" |
|
37075
a680ce27aa56
modernized sorting algorithms; quicksort implements sort
haftmann
parents:
30738
diff
changeset
|
25 |
by (induct xs rule: quicksort.induct) (simp_all add: ac_simps) |
24615 | 26 |
|
27 |
lemma set_quicksort [simp]: "set (quicksort xs) = set xs" |
|
37075
a680ce27aa56
modernized sorting algorithms; quicksort implements sort
haftmann
parents:
30738
diff
changeset
|
28 |
by (simp add: set_count_greater_0) |
24615 | 29 |
|
37075
a680ce27aa56
modernized sorting algorithms; quicksort implements sort
haftmann
parents:
30738
diff
changeset
|
30 |
lemma sorted_quicksort: "sorted (quicksort xs)" |
a680ce27aa56
modernized sorting algorithms; quicksort implements sort
haftmann
parents:
30738
diff
changeset
|
31 |
by (induct xs rule: quicksort.induct) (auto simp add: sorted_Cons sorted_append not_le less_imp_le) |
a680ce27aa56
modernized sorting algorithms; quicksort implements sort
haftmann
parents:
30738
diff
changeset
|
32 |
|
40349 | 33 |
theorem sort_quicksort: |
37075
a680ce27aa56
modernized sorting algorithms; quicksort implements sort
haftmann
parents:
30738
diff
changeset
|
34 |
"sort = quicksort" |
a680ce27aa56
modernized sorting algorithms; quicksort implements sort
haftmann
parents:
30738
diff
changeset
|
35 |
by (rule ext, rule properties_for_sort) (fact quicksort_permutes sorted_quicksort)+ |
24615 | 36 |
|
37 |
end |
|
38 |
||
39 |
end |