Skip to content

Create quicksort.f95 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions sorting/quicksort.f95
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
program testquicksort
integer lst(10)
lst = (/ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 /)
call quicksort(lst, 1, 10)
call show(lst)
end program testquicksort

recursive subroutine quicksort(lst, start, end_)
integer lst(10)
integer start, end_
integer pivot, i, tmp
if (start .lt. end_) then
pivot = start
do i = start+1, end_
if (lst(i) .le. lst(start)) then
pivot = pivot + 1
tmp = lst(pivot)
lst(pivot) = lst(i)
lst(i) = tmp
end if
end do
tmp = lst(pivot)
lst(pivot) = lst(start)
lst(start) = tmp

call quicksort(lst, start, pivot)
call quicksort(lst, pivot+1, end_)
end if
end

subroutine show(lst)
integer lst(10)
integer x
do x = 1, 10
print 100, lst(x)
end do

100 format (i0)
end