diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/.idea/.idea.Algorithm-FSharp/.idea/encodings.xml b/.idea/.idea.Algorithm-FSharp/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.Algorithm-FSharp/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.Algorithm-FSharp/.idea/indexLayout.xml b/.idea/.idea.Algorithm-FSharp/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.Algorithm-FSharp/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.Algorithm-FSharp/.idea/projectSettingsUpdater.xml b/.idea/.idea.Algorithm-FSharp/.idea/projectSettingsUpdater.xml new file mode 100644 index 0000000..4bb9f4d --- /dev/null +++ b/.idea/.idea.Algorithm-FSharp/.idea/projectSettingsUpdater.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/.idea.Algorithm-FSharp/.idea/vcs.xml b/.idea/.idea.Algorithm-FSharp/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.Algorithm-FSharp/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/.idea.Algorithm-FSharp/.idea/workspace.xml b/.idea/.idea.Algorithm-FSharp/.idea/workspace.xml new file mode 100644 index 0000000..b3957b2 --- /dev/null +++ b/.idea/.idea.Algorithm-FSharp/.idea/workspace.xml @@ -0,0 +1,116 @@ + + + + Algorithm-FSharp/Algorithm-FSharp.fsproj + + + + + + + + + + + + + + + + { + "keyToString": { + "ASKED_SHARE_PROJECT_CONFIGURATION_FILES": "true", + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "SHARE_PROJECT_CONFIGURATION_FILES": "true", + "WebServerToolWindowFactoryState": "false", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "settings.editor.selected.configurable": "project.propDebugger", + "vue.rearranger.settings.migration": "true" + } +} + + + + + + + + + 1660053659032 + + + 1660059459924 + + + + + + + + + + + + \ No newline at end of file diff --git a/Algorithm-FSharp.sln b/Algorithm-FSharp.sln new file mode 100644 index 0000000..5c6f551 --- /dev/null +++ b/Algorithm-FSharp.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Algorithm-FSharp", "Algorithm-FSharp\Algorithm-FSharp.fsproj", "{BA0BCD71-D94F-4B20-85A9-4CED4FD6DE99}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BA0BCD71-D94F-4B20-85A9-4CED4FD6DE99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BA0BCD71-D94F-4B20-85A9-4CED4FD6DE99}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BA0BCD71-D94F-4B20-85A9-4CED4FD6DE99}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BA0BCD71-D94F-4B20-85A9-4CED4FD6DE99}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Algorithm-FSharp/Algorithm-FSharp.fsproj b/Algorithm-FSharp/Algorithm-FSharp.fsproj new file mode 100644 index 0000000..03764ea --- /dev/null +++ b/Algorithm-FSharp/Algorithm-FSharp.fsproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + Algorithm_FSharp + + + + + + + + diff --git a/Algorithm-FSharp/Program.fs b/Algorithm-FSharp/Program.fs new file mode 100644 index 0000000..df9d4a4 --- /dev/null +++ b/Algorithm-FSharp/Program.fs @@ -0,0 +1,8 @@ +// For more information see https://aka.ms/fsharp-console-apps +module Algorithm_FSharp.Program +open Algorithm_FSharp.QuickSort + +// 测试用例 +let myList = [1; 1; 4; 5; 1; 4; 1; 9; 1; 9; 8; 1; 0] // 你是一个一个一个快速排序算法啊啊啊啊啊啊 +printfn $"%A{myList + |> quicksort}" diff --git a/Algorithm-FSharp/QuickSort.fs b/Algorithm-FSharp/QuickSort.fs new file mode 100644 index 0000000..f651b2d --- /dev/null +++ b/Algorithm-FSharp/QuickSort.fs @@ -0,0 +1,23 @@ +module Algorithm_FSharp.QuickSort + +// 序列切割算法 +// partition : ('a bool) -> 'a list -> 'a list * 'a list +// 按pred切割序列,返回的元组中第一个元素是满足条件的元素序列,第二个元素是不满足条件的元素序列 +let rec partition pred lst = + match lst with // 模式匹配 + | [] -> [],[] // list为空,返回两个空列表 + | head::tail -> // list不为空,切割出第一个元素head和剩余的元素组成的序列tail + let matched, unmatched = partition pred tail // 递归调用partition完成序列切割,此时matched、unmatched分别为匹配谓词和不匹配谓词的元素序列 + match (pred head) with // 对当前元素进行判断 + | true -> head::matched, unmatched // 若匹配谓词,则将其追加至matched的头部并返回 + | false -> matched, head::unmatched // 若不匹配谓词,将其追加至unmatched的头部并返回 + +// quicksort : 'T list -> 'T list +// 快速排序的F#实现 +let rec quicksort i = + match i with // 模式匹配 + | [] -> [] // 空列表和只有一个元素的列表都返回自身即可 + | [single] -> [single] // 这两种情况下序列都完全有序 + | head :: tail -> // 多于一个元素的列表,选取列表的头元素作为参考数 + let leftList, rightList = partition (fun item -> item <= head) tail // 调用partition完成序列切割 + quicksort(leftList) @ [head] @ quicksort(rightList) // 合并序列,此时序列完全有序 \ No newline at end of file