8-1 Quick Sort

题目描述

According to the basic idea of quick sort algorithm descirbed on class, we will modify a little bit of this algorithm.
Only the first pivot is given by the input operation. The other pivots will still use Median-of-Three patitioning method.The inithial sequence is {49,38,65,97,76,13,27,50,2,8}. We will sort this sequence in ascending order.
The parameter Cutoff is 3.

输入格式:

The index of first pivot in the sequence. The index begins from 0.

输出格式:

Several lines. Each line shows the temporary result of sort.
In each interation, if the quicksort method is used, then add "Qsort:" before the result. The first number in bracket is the left index of sequence processed by quick sort method, and the second number is the right index of this sequence.
If the insertion sort method is used, then add "Insert:" before the result. The first number in brackets is the left index of sequence processed by insertion sort method, and the second number means the amount of the following elements.
Add "\n" to the end of each line.

输入样例:

在这里给出一组输入。例如:

1

输出样例:

在这里给出相应的输出。例如:

Qsort(0,9):2,8,27,13,38,97,65,50,49,76,
Qsort(0,3):2,8,27,13,38,97,65,50,49,76,
insert(0,1):2,8,27,13,38,97,65,50,49,76,
insert(2,2):2,8,13,27,38,97,65,50,49,76,
Qsort(5,9):2,8,13,27,38,50,65,49,76,97,
insert(5,3):2,8,13,27,38,49,50,65,76,97,
insert(9,1):2,8,13,27,38,49,50,65,76,97,

C语言代码

#include<stdio.h>
#include<stdlib.h>
#define CUTOFF 3

void Swap(int * a,int * b){
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}

void printstring(int *A){
    int i;
    for(i=0;i<10;i++){
        printf("%d,",A[i]);
    }
    printf("\n");
}

void Insertionsort(int * A, int N){
    int P,i;
    for(P=1;P<N;P++){
        int temp=A[P];
        for(i=P;i>0;i--){
            if(temp<A[i-1]) A[i]=A[i-1];
            else break;
        }
        A[i]=temp;
    }
}

int Median3(int * A,int Left, int Right){
    int Center=(Left+Right)/2;
    if(A[Left]>A[Center]) Swap(&A[Left],&A[Center]);
    if(A[Left]>A[Right]) Swap(&A[Left],&A[Right]);
    if(A[Center]>A[Right]) Swap(&A[Center],&A[Right]);
    Swap(&A[Center],&A[Right-1]);
    return A[Right-1];
}

void Qsort(int * A, int Left,int Right){
    int i,j;
    int Pivot;
    if(Left+CUTOFF<=Right){
    	Pivot=Median3(A,Left,Right);
        for(i=Left,j=Right-1;;){
            while(A[++i]<Pivot){}
            while(A[--j]>Pivot){}
            if(i<j) Swap(&A[i],&A[j]);
            else break;
        }
        Swap(&A[i],&A[Right-1]);
        printf("Qsort(%d,%d):",Left,Right);
        printstring(A);
        Qsort(A,Left,i-1);
        Qsort(A,i+1,Right);
    }
    else {
        printf("insert(%d,%d):",Left,Right-Left+1);
        Insertionsort(A+Left,Right-Left+1);
        printstring(A);
    }
}

void quicksort(int * A,int i_pivot){
    Swap(&A[i_pivot],&A[9]);
    int i,j,Pivot;
    Pivot=A[9];
    for(i=-1,j=9;;){
        while(A[++i]<Pivot){}
        while(A[--j]>Pivot){}
        if(i<j) Swap(&A[i],&A[j]);
        else break;
    }
    Swap(&A[i],&A[9]);
    printf("Qsort(0,9):");
    printstring(A);
    Qsort(A,0,i-1);
    Qsort(A,i+1,9);
}


main(){
    int A[10]={49,38,65,97,76,13,27,50,2,8};
    int pivot;
    scanf("%d",&pivot);
    quicksort(A,pivot);
    return 0;
}

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Quick sort is a popular sorting algorithm that works by partitioning an array into two sub-arrays, and then recursively sorting each sub-array. It is a divide-and-conquer algorithm that has an average time complexity of O(n log n), making it one of the fastest sorting algorithms. The basic idea behind quick sort is to select a pivot element, partition the array around the pivot element, and then recursively apply the same process to each of the sub-arrays. The partitioning process involves selecting a pivot element, rearranging the array so that all elements less than the pivot are on one side and all elements greater than the pivot are on the other side, and then returning the index of the pivot element. This pivot index is then used to divide the array into two sub-arrays, which are recursively sorted. Here's an example implementation of quick sort in Python: ``` def quick_sort(arr): if len(arr) <= 1: return arr else: pivot = arr[0] left = [] right = [] for i in range(1, len(arr)): if arr[i] < pivot: left.append(arr[i]) else: right.append(arr[i]) return quick_sort(left) + [pivot] + quick_sort(right) ``` This implementation selects the first element of the array as the pivot, and then uses list comprehensions to create the left and right sub-arrays. The left sub-array contains all elements less than the pivot, while the right sub-array contains all elements greater than or equal to the pivot. The function then recursively sorts the left and right sub-arrays and combines them with the pivot element to produce the final sorted array.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值