一个简单的Fortran递归快速排序

一个简单的Fortran递归快速排序

Fortran指针在使用时,所指向的变量或结构体在声明时必须加target,因此Fortran指针形式快速排序的通用性较差。这里选择用几个整形变量来标记排序的当前位置,函数可以被直接调用而不用重新声明待排序数组或结构体。

执行过程为:
Step1:将数组最左端元素X[l]、最右端元素X[r]、中间元素X[abs((r-l)/2)]的中值作为阈值,并将阈值与数组最左端元素交换位置;
Step2:若阈值右侧相邻元素X[a]小于阈值,则X[a]与阈值交换位置;
Step3:执行Step2,直至阈值小于右侧相邻元素X[a+n];
Step3:若阈值大于数组待排序部分最右端的元素X[b],则X[b]与阈值交换位置;
Step4:若有元素未排序,则执行Step2;
Step5:将数组的l到a+n-1号元素作为原始数据,执行Step1;
Step6:将数组的a+n到r号元素作为原始数据,执行Step1。
这样就将一组数据完整排序。

代码如下:

    module module_FastSort
    implicit none

    integer rec_num
    double precision,allocatable :: source_data(:)
    double precision a,b,c
    
    contains
    
    recursive subroutine half_cut(low,high)
    integer low,high
    integer edge_mid,edge_high,edge_low
    integer my_current,my_low,my_high

    edge_high=high
    edge_low=low
    edge_mid=abs((edge_high-edge_low)/2)
    
    !Check the border of this sort
    if(edge_high<edge_low)then
        write(*,*) "*******boundary error!*********"
        return
    endif
    if(edge_high==edge_low) return
    
    !Select the middle data of source_data array and put it into the first position
    if(edge_high-edge_low>=2)then
        a=source_data(edge_high)-source_data(edge_low)
        b=source_data(edge_mid)-source_data(edge_low)
        c=source_data(edge_high)-source_data(edge_mid)
        if(a>0 .and. b>=0) then
            if(c>0)then 
                call exchange_data(source_data(edge_low),source_data(edge_mid))
            else
                call exchange_data(source_data(edge_low),source_data(edge_high))
            endif
        elseif(a<=0 .and. b<0) then
            if(c>0)then
                call exchange_data(source_data(edge_low),source_data(edge_high))
            else
                call exchange_data(source_data(edge_low),source_data(edge_mid))
            endif
        endif
    endif
    
    !Set boundary of my sort
    my_low=edge_low
    my_high=edge_high
    my_current=edge_low+1
    
    !Do fast sort
    do while(my_current<my_high)
        do while(source_data(my_current)<=source_data(my_low))
            call exchange_data(source_data(my_low),source_data(my_current))
            my_low=my_low+1
            my_current=my_current+1
        enddo
        if(my_low==my_high) return
        do while(source_data(my_current)<=source_data(my_high))
            my_high=my_high-1
            if(my_high==my_current) then
                exit
            endif
        enddo
        if(my_high>my_current) call exchange_data(source_data(my_current),source_data(my_high))
    enddo    
    call half_cut(edge_low,my_low)
    call half_cut(my_low+1,edge_high)
    return
    endsubroutine
    
    subroutine exchange_data(aa,bb)
    !Exchange data of aa and bb
    double precision aa,bb,mid
        mid=aa
        aa=bb
        bb=mid
    endsubroutine
    
    endmodule
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 很高兴能够与你交谈!快速排序是一种基于比较的排序算法,它可以用递归的方式来实现。C语言中,可以通过编写一个函数来实现快速排序,该函数的比较操作可以使用两个参数,一个是待排序的数组,另一个是元素的数量。函数进行递归调用,每次调用时,它会通过比较将数组元素分为两组,然后再分别对这两组元素进行排序,最后将排序结果合并。 ### 回答2: 快速排序是一种基于比较的排序算法,使用分治的策略。它通过选择一个基准值,将数组分割成两个子数组,然后递归地对这两个子数组进行排序。以下是使用C语言编写的递归快速排序算法的实现: ```c #include <stdio.h> // 交换两个元素的值 void swap(int* a, int* b) { int t = *a; *a = *b; *b = t; } // 将基准值放在正确的位置,并将较小元素放在基准值的左侧,较大元素放在基准值的右侧 int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = (low - 1); for (int j = low; j <= high - 1; j++) { if (arr[j] < pivot) { i++; swap(&arr[i], &arr[j]); } } swap(&arr[i + 1], &arr[high]); return (i + 1); } // 递归实现快速排序 void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } // 测试例子 int main() { int arr[] = {64, 25, 12, 22, 11}; int n = sizeof(arr) / sizeof(arr[0]); quickSort(arr, 0, n - 1); printf("排序后的数组:\n"); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } ``` 以上代码实现了一个简单递归快速排序算法。在main函数中,我们定义了一个包含5个元素的整型数组,然后调用quickSort函数对数组进行递归快速排序,并输出排序后的结果。输出结果为:11 12 22 25 64。这证明递归快速排序已成功排序该数组。 ### 回答3: 快速排序是一种常用的排序算法,它基于分治法的思想。使用C语言实现递归快速排序可以按照以下步骤进行: 1. 首先定义一个递归函数`quickSort`,它接受一个整型数组`arr`、待排序元素的起始位置`start`和结束位置`end`作为参数。 2. 在`quickSort`函数中,选择一个基准元素`pivot`,可以选择数组的中间元素。 3. 将数组分为两部分,使得左边的元素都小于等于基准元素,右边的元素都大于基准元素。可以使用两个指针`i`和`j`,分别指向数组的起始和结束位置。 4. 将比基准元素小的元素放到左边,将比基准元素大的元素放到右边,直到`i`和`j`相遇。 5. 将基准元素交换到`i`指针所在的位置。 6. 递归地对左边的子数组和右边的子数组进行快速排序,即调用`quickSort(arr, start, i-1)`和`quickSort(arr, i+1, end)`。 下面是一个示例代码: ``` #include <stdio.h> void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } int partition(int arr[], int start, int end) { int pivot = arr[end]; int i = (start - 1); for (int j = start; j <= end - 1; j++) { if (arr[j] < pivot) { i++; swap(&arr[i], &arr[j]); } } swap(&arr[i + 1], &arr[end]); return (i + 1); } void quickSort(int arr[], int start, int end) { if (start < end) { int pivotIndex = partition(arr, start, end); quickSort(arr, start, pivotIndex - 1); quickSort(arr, pivotIndex + 1, end); } } int main() { int arr[] = {8, 3, 2, 7, 1, 5, 4, 6}; int n = sizeof(arr)/sizeof(arr[0]); quickSort(arr, 0, n - 1); printf("Sorted array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; } ``` 运行上述代码,可以得到已排序的数组`1 2 3 4 5 6 7 8`。这就是使用C语言实现的递归快速排序的示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值