Skip to content
This repository was archived by the owner on May 22, 2026. It is now read-only.

Commit 7156980

Browse files
feat: quick sort for vector
1 parent f4d1268 commit 7156980

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

sort/quick_sort/quickSortVector.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Data.Vector.Unboxed qualified as VU
2+
3+
-- quickSort.hsのVector使用バージョン
4+
quickSort :: (VU.Unbox a, Ord a) => [a] -> [a]
5+
quickSort xs = VU.toList $ go (VU.fromList xs)
6+
where
7+
go vs
8+
| VU.null vs = VU.empty
9+
| otherwise = go left VU.++ VU.singleton pivot VU.++ go right
10+
where
11+
pivot = VU.head vs -- pivotは左端固定とする
12+
rest = VU.tail vs -- pivotを外す
13+
left = VU.filter (< pivot) rest
14+
right = VU.filter (>= pivot) rest
15+
16+
main :: IO ()
17+
main = do
18+
let randomList = [30, 75, 69, 16, 47, 77, 60, 80, 74, 8, 77, 1, 60, 33, 70, 29, 24, 91, 60, 69] :: [Int]
19+
print randomList
20+
print $ quickSort randomList

0 commit comments

Comments
 (0)