排序---交换排序---快速排序Quick Sort

                                   快速排序---Quick Sort

      基本思路:

      分治思想,将一个大的数据序列分割成两个子序列,然后再依次将两个子序列划分成更小的两个,这里就能很明显的看出快速排序的时间复杂度是与2 的对数有关。

      划分的标准是找到序列中数据居中的值做为key值,怎么取得key值,就可以判断出算法的快慢来。通常比较简单的情况是将序列的第一个值做为key值。然后通过交换的思路,将序列划分为左、右两个子序列来,划分后的结果是左边的子序列的值都比key值小,右边的子序列的值都比key值大,key值居于中间。

      算法实现:

    int A[] = {49,38,65,97,76,13,27,49}
void qsort(int left, int right)
{
    int pos = A[left];
    int empty = left; /*每次都是先把最左边的取出来*/
    int p = left, q = right;
    int mid = 0;
    /*将pos放在中间,左边的比它小,右边的比它大*/
    while(p<q)
    {
/*先做右边,把右边比pos小的放在pos的位置上,因为开始是把最左边的空出来了*/
while(p<q)
{
if (pos>A[q])
{
A[empty]=A[q]
empty = q;/*更改空位置*/
break;
}
else
{
q--;
}
}
       
    while(p<q)
{
if(pos<A[p])
{
A[empty]=A[p]
empty=p;
break;
}
else
{
p++;
}
}
    }
    A[empty]=pos;
    mid=empty;


    if (left<q)
    {
qsort(left,q-1);
    }
    if(p<right)
    {
qsort(p+1,right);
    }
     
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是快速排序代码: public class QuickSort { public static void sort(int[] arr, int start, int end){ if (start < end){ int partitionIndex = partition(arr, start, end); sort(arr, start, partitionIndex-1); sort(arr, partitionIndex+1, end); } } private static int partition(int[] arr, int start, int end){ int pivot = arr[end]; int i = start-1; for (int j=start; j<end; 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[end]; arr[end] = temp; return i+1; } } ### 回答2: 快速排序Quick Sort)是一种常用的排序算法,通过将待排序的序列分割成两部分,其中一部分所有元素都小于等于另一部分所有元素,然后对这两部分递归地进行排序,最终得到一个有序序列。 下面是使用Java编写的快速排序代码示例: ```java public class QuickSort { public static void main(String[] args) { int[] array = {5, 2, 9, 4, 7, 1, 6, 8, 3}; quickSort(array, 0, array.length - 1); for (int num : array) { System.out.print(num + " "); } } public static void quickSort(int[] array, int left, int right) { if (left < right) { int pivot = partition(array, left, right); quickSort(array, left, pivot - 1); quickSort(array, pivot + 1, right); } } public static int partition(int[] array, int left, int right) { int pivot = array[right]; int i = left - 1; for (int j = left; j < right; j++) { if (array[j] <= pivot) { i++; swap(array, i, j); } } swap(array, i + 1, right); return i + 1; } public static void swap(int[] array, int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } } ``` 以上代码定义了一个`QuickSort`类,其中包含了`main`方法作为程序入口,以及三个静态方法`quickSort`、`partition`和`swap`来实现快速排序算法。 在`main`方法中,首先定义了一个待排序的数组`array`,然后调用`quickSort`方法对该数组进行排序,最后将排序结果输出。 `quickSort`方法用于递归地进行快速排序,它接收一个数组、一个左边界索引`left`和一个右边界索引`right`作为参数。在`quickSort`方法中,首先通过`partition`方法将数组分割为两部分,然后对这两部分分别调用`quickSort`方法进行递归排序。 `partition`方法用于分割数组,它接收一个数组、一个左边界索引`left`和一个右边界索引`right`作为参数。在`partition`方法中,首先选择数组的最后一个元素作为基准值`pivot`,然后通过一个循环将小于等于基准值的元素放置在左侧,大于基准值的元素放置在右侧,最后将基准值放置在中间位置,并返回该位置索引。 `swap`方法用于交换数组中的两个元素,它接收一个数组和两个索引作为参数,然后将对应索引位置的元素进行交换。 以上就是使用Java生成快速排序代码的简单示例。 ### 回答3: 快速排序是一种常用的排序算法,它基于分区的概念,通过找到一个基准元素,将数组分成两个子数组,其中一个子数组的所有元素小于等于基准元素,另一个子数组的所有元素大于等于基准元素,然后递归地对子数组进行排序。 在Java中,可以使用以下代码来实现快速排序: ```java public class QuickSort { public static 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 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++; swap(arr, i, j); } } swap(arr, i + 1, high); return i + 1; } public static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static void main(String[] args) { int[] arr = {9, 2, 5, 1, 0, 8, 3}; quickSort(arr, 0, arr.length - 1); for (int num : arr) { System.out.print(num + " "); } } } ``` 运行这段代码会将数组 `{9, 2, 5, 1, 0, 8, 3}` 进行快速排序,并输出排序后的结果:`0 1 2 3 5 8 9`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值