排序碎碎念(三):数据结构基础— 统计工龄 QuickSort

其实呢,1)Quicksort也有十分简便的写法的,比如pivot就挑第一个好啦,比如对所有的直接递归,不用考虑一定的间隔用插入排序什么的,但是要知道的是在C++的内置sort函数中确实就像老师说的那样,是判断子集的长度,小于某个值就使用插入排序,这个版本的快排是比简单粗暴版的快排要好很多的。2)输出统计的那个函数不要写错这道题就基本过了。 当然你用heapsort也可以过这道题。

10-排序4 统计工龄(20 分)

给定公司N名员工的工龄,要求按工龄增序输出每个工龄段有多少员工。

输入格式:

输入首先给出正整数N105),即员工总人数;随后给出N个整数,即每个员工的工龄,范围在[0, 50]。

输出格式:

按工龄的递增顺序输出每个工龄的员工个数,格式为:“工龄:人数”。每项占一行。如果人数为0则不输出该项。

输入样例:

8
10 2 0 5 7 2 5 2

输出样例:

0:1
2:3
5:2
7:1
10:1
//  Created by air on 2018/5/12.
//
#define Cutoff (32)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int ElementType;
void QuickSort(ElementType A[], int Left, int Right);
ElementType Median3(ElementType A[], int Left, int Right);
void swap(ElementType *a, ElementType *b);
void Insertion_Sort(ElementType* A, int N);
void print(ElementType A[], int N);
void QSort( ElementType A[], int N );
void Scan( ElementType A[], int total);

/* 统一接口 */
void QSort( ElementType A[], int N )
{
    QuickSort( A, 0, N-1 );
}

int main(int argc, const char * argv[]) {
    int total;
    scanf("%d",&total);
    ElementType * A;
    A = (ElementType *) malloc(sizeof(ElementType) * total);
    for(int i = 0; i < total; i++)
        scanf("%d",&A[i]);
    
    QSort(A, total);
    Scan(A, total);
    return 0;
}
/* 这个 Scan 函数 写对就可以过了 -  - */
void Scan( ElementType A[], int total){
    int i, count;
    count  = 1;
    for(i = 0; i < total; i++){
        if( i == 0 ){
            continue;
        }
        if( A[i] != A[i-1] ){
            printf("%d:%d\n", A[i - 1], count);
            count = 1;
        }else
            count ++;
    }
    printf("%d:%d\n", A[i - 1], count);
    
}
/*-------------------------------------------------------------
 QuickSort:
 当两边都发现有不对的元素之后 ---> 对两遍的元素进行交换
 元素相等的时候,仍然停下来交换,为了子集的均等划分的原因;
 小规模的数据(< 32),使用插入排序;
 -------------------------------------------------------------*/
void QuickSort(ElementType A[], int Left, int Right){
    if(Left + Cutoff <= Right){
        int i = Left;
        int j = Right - 1; //Right肯定在pivot的右边
        int pivot = Median3(A, Left, Right);
        for(;;){
            while(A[ ++i ] < pivot) { }
            while (A[ --j ] > pivot) { }
            if( i < j)
                swap(& A[i], & A[j]);
            else
                break;
        }
        swap( &A[i], &A[ Right - 1 ] );
        QuickSort(A, Left, i - 1);
        QuickSort(A, i + 1, Right);
    }else
        Insertion_Sort(A + Left, Right - Left + 1);
}

ElementType Median3(ElementType 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 swap(ElementType *a, ElementType *b){
    ElementType tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
}

void Insertion_Sort(ElementType* A, int N){
    ElementType  tmp;
    int i, j;
    for( i = 1; i < N; i++ ){
        tmp = *(A + i);
        for(j = i; (j > 0) && (*(A + j - 1) > tmp); j-- ){
               *(A + j) = *(A + j - 1);
        }
        *(A + j) = tmp;
    }
}
/*-------------------------------------------------------------
 QuickSort 结束
 -------------------------------------------------------------*/
/*  Print for test! */
void print(ElementType A[], int N){
    int i;
    for(i = 0; i < N-1; i++)
        printf("%d ", A[i]);
    printf("%d\n",A[N-1]);
}


        




猜你喜欢

转载自blog.csdn.net/qq_25175067/article/details/80294314
今日推荐