快速排序程序

#include <stdio.h>
/*
功能:快速排序
start表示起始位置指针,len表示要排序的长度
无返回值
*/
void qiuck_sort(int *start,int len)
{
    int k;//用作记录枢轴记录关键字
    int *p1,*p2,*pkey;//p1,p2分别表示高位和低位的指针,pkey枢轴记录关键字指针
    if(len<=1)//1位不必排序
    {
        return;
    }
    pkey=start;//把第一为
    k=*start;//记录枢轴记录关键字
    p1=start;
    p2=start+len-1;
    while(p1<p2)
    {
        while(*p2>=k&&p1<p2)p2--;
        *pkey=*p2;
        pkey=p2;
        while(*p1<=k&&p1<p2)p1++;
        *pkey=*p1;
        pkey=p1;
    }
    *p1=k;//填入枢轴记录
    //分治为两部分递归
    qiuck_sort(start,p1-start);
    qiuck_sort(p1+1,start+len-p1-1);
}
### 回答1: 我可以给你一些参考代码: public static void quickSort(int[] arr, int low, int high) { if (arr == null || arr.length == 0) return; if (low >= high) return; // 挑选基准元素 int middle = low + (high - low) / 2; int pivot = arr[middle]; // 数组分区 int i = low, j = high; while (i <= j) { while (arr[i] < pivot) { i++; } while (arr[j] > pivot) { j--; } if (i <= j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } // 递归调用 if (low < j) quickSort(arr, low, j); if (high > i) quickSort(arr, i, high); } ### 回答2: JAVA快速排序是一种常用的排序算法,它基于分治的思想,通过将一个数组划分为较小的子数组,然后递归地对这些子数组进行排序,最终得到一个有序数组。 以下是一个示例的JAVA快速排序程序: ```java public class QuickSort { public static void main(String[] args) { int[] arr = {5, 9, 3, 1, 6, 2, 8, 7, 4}; quickSort(arr, 0, arr.length - 1); System.out.println("排序后的数组:"); for (int num : arr) { System.out.print(num + " "); } } public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pivot = partition(arr, low, high); quickSort(arr, low, pivot - 1); quickSort(arr, pivot + 1, high); } } public static int partition(int[] arr, int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } } ``` 通过以上示例程序,我们可以对一个包含整数的数组进行快速排序快速排序的主要思想是通过选择一个pivot(一般选择数组中的最后一个元素),然后将数组中小于pivot的元素放在pivot的左边,大于pivot的元素放在pivot的右边。然后对pivot左边和右边的子数组进行递归排序。最终,所有子数组都被排序,整个数组也就完成了排序。 ### 回答3: 快速排序是一种常见的排序算法,它的核心思想是通过分治的方法将一个序列按照某个元素分成两个子序列,再对两个子序列分别进行排序。具体步骤如下: 1. 选择一个基准元素(通常选择第一个元素),将序列分成两个部分。 2. 将小于基准元素的元素放在左边,大于基准元素的元素放在右边。 3. 递归地对左边和右边的两个子序列进行排序。 4. 合并排序后的左右子序列。 下面是一个使用Java实现快速排序的例子: ```java public class QuickSort { public void quickSort(int[] arr, int low, int high) { if (low < high) { int pivotIndex = partition(arr, low, high); // 分区操作,将数组分成两部分 quickSort(arr, low, pivotIndex - 1); // 递归排序左子数组 quickSort(arr, pivotIndex + 1, high); // 递归排序右子数组 } } public int partition(int[] arr, int low, int high) { int pivot = arr[low]; // 将第一个元素作为基准元素 while (low < high) { while (low < high && arr[high] >= pivot) { high--; } arr[low] = arr[high]; // 比基准元素小的移到左边 while (low < high && arr[low] <= pivot) { low++; } arr[high] = arr[low]; // 比基准元素大的移到右边 } arr[low] = pivot; // 基准元素放到最终位置 return low; // 返回基准元素的索引 } public static void main(String[] args) { QuickSort quickSort = new QuickSort(); int[] arr = {6, 1, 8, 4, 3, 9, 7}; quickSort.quickSort(arr, 0, arr.length - 1); for (int num : arr) { System.out.print(num + " "); } } } ``` 这个例子中,我们使用第一个元素作为基准元素,并使用low和high两个指针指向序列的起始和结束位置。在partition方法中,我们通过循环交换元素的位置,将小于基准元素的元素移到左边,大于基准元素的元素移到右边,最后将基准元素放到最终位置。在quickSort方法中,我们通过递归调用quickSort方法对左右子序列进行排序。最后,我们打印排序后的数组,结果为1 3 4 6 7 8 9。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值