diff --git a/Uebung/SoSe24/ListSort.f95 b/Uebung/SoSe24/ListSort.f95 new file mode 100644 index 0000000000000000000000000000000000000000..bc3ba4d78d46a81fce8e758a8410a7b351cda163 --- /dev/null +++ b/Uebung/SoSe24/ListSort.f95 @@ -0,0 +1,187 @@ +program ListSortProgram + + implicit none + integer, dimension(10) ::myList + + myList = (/4,2,5,1,8,9,7,6,3,0/) + + write(*,*)myList + call heapSort(myList) + write(*,*)myList + + contains + + subroutine swap(a,b) + integer, intent(inout) :: a,b + integer :: sw + sw = a + a = b + b = sw + end subroutine swap + + subroutine bubbleSort(list) + integer, dimension(:), intent(inout) :: list + integer :: i, n + + do n=size(list), 1, -1 + do i=1, n-1 + if(list(i) > list(i+1)) then + call swap(list(i), list(i+1)) + end if + end do + end do + end subroutine bubbleSort + + subroutine selectionSort(list) + integer, dimension(:), intent(inout) :: list + integer :: i, n, max + + do n=size(list), 1, -1 + max = 1 + do i=2, n + if(list(i) > list(max)) then + max = i + end if + end do + + call swap(list(max), list(n)) + end do + end subroutine selectionSort + + subroutine insertionSort(list) + integer, dimension(:), intent(inout) :: list + integer :: i, n + + do n=size(list),1,-1 + do i=n, size(list)-1 + if(list(i) > list(i+1)) then + call swap(list(i), list(i+1)) + end if + end do + end do + end subroutine insertionSort + + recursive subroutine quicksort(list) + integer, dimension(:), intent(inout) :: list + integer :: last, i, j, pivot + + last = size(list) + + pivot = list((last+1) / 2 ) + i = 1 + j = last + do + do while (list(i) < pivot) !größer/gleich als pivot von links + i=i+1 + end do + + do while (pivot < list(j)) !kleiner/gleich als pivot von rechts + j=j-1 + end do + + if(i<j) call swap(list(i), list(j)) !falls verschieden tauschen + + if( i <= j) then !weitersetzen + i = i+1 + j = j-1 + end if + + if (i > j) exit !alles verglichen + end do + + if (1 < j) call quicksort(list(1:j)) !kleinere Teilliste + if (i < last) call quicksort(list(i:last)) !größere Teilliste + end subroutine quicksort + + subroutine merge(A, B, mergedAB) + integer, dimension(:), intent(in) :: A, B + integer, dimension(:), intent(out) :: mergedAB + integer, dimension(1:size(mergedAB)) :: C + integer :: i, j, k + i = 1 + j = 1 + + do k = 1, size(C) + + if (i <= size(A) .and. j <= size(B)) then + + if (A(i) <= B(j)) then + C(k) = A(i) + i = i + 1 + else + C(k) = B(j) + j = j + 1 + end if + + else if (i <= size(A)) then + + C(k) = A(i) + i = i + 1 + + else if (j <= size(B)) then + + C(k) = B(j) + j = j + 1 + end if + end do + mergedAB = C + + end subroutine merge + + recursive subroutine mergeSort(list) + integer, dimension(:), intent(inout) :: list + integer :: half + + if(size(list) > 1) then + + half = size(list) / 2 + + call MergeSort(list( : half)) + call MergeSort(list(half + 1 :)) + + if (list(half) > list(half + 1)) then + call merge(list(:half), list(half + 1:), list) + endif + end if + end subroutine mergeSort + + subroutine heapSort(list) + integer, dimension(:), intent(inout) :: list + integer :: root, n, last + + n = size(list) + do root = n / 2, 1, -1 + call heapify(list(root:)); + end do + + do last = n, 2, -1 + call swap(list(1), list(last)) + call heapify(list(:last-1)) + end do + + end subroutine heapSort + + subroutine heapify(list) + integer, dimension(:), intent(inout) :: list + integer :: child, root, bottom + + bottom = size(list) + root = 1 + + do while(root*2 < bottom) + child = root * 2 + + if (child + 1 < bottom) then + if (list(child) < list(child+1)) child = child + 1 + end if + + if (list(root) < list(child)) then + call swap(list(child), list(root)) + root = child + else + exit + end if + end do + + end subroutine heapify +end program ListSortProgram