6-11 求自定类型元素序列的中位数 (25point(s)).c

本题要求实现一个函数,求N个集合元素A[]的中位数,即序列中第⌊(N+1)/2⌋大的元素。其中集合元素的类型为自定义的ElementType

函数接口定义:

ElementType Median( ElementType A[], int N );

其中给定集合元素存放在数组A[]中,正整数N是数组元素个数。该函数须返回NA[]元素的中位数,其值也必须是ElementType类型。

裁判测试程序样例:

#include <stdio.h>

#define MAXN 10
typedef float ElementType;

ElementType Median( ElementType A[], int N );

int main ()
{
    ElementType A[MAXN];
    int N, i;

    scanf("%d", &N);
    for ( i=0; i<N; i++ )
        scanf("%f", &A[i]);
    printf("%.2f\n", Median(A, N));

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

3
12.3 34 -5

输出样例:

12.30
//   Date:2020/3/25
//   Author:xiezhg5
#include <stdio.h>

#define MAXN 10
typedef float ElementType;

ElementType Median( ElementType A[], int N );

int main ()
{
    ElementType A[MAXN];
    int N, i;

    scanf("%d", &N);
    for ( i=0; i<N; i++ )
        scanf("%f", &A[i]);
    printf("%.2f\n", Median(A, N));

    return 0;
}

/* 你的代码将被嵌在这里 */
ElementType Median( ElementType A[], int N )
{
	//必须采用希尔排序 
	ElementType temp;
    int gap,i,j;
    for(gap= N/2; gap> 0; gap= gap/ 2) //gap是每次排序分组的间隔,每次间隔缩小两倍(其他缩小办法也可以)                                       
    {
      for(i= gap; i< N; i++)//相当于在同一组内采用直接插入排序
      {
        for(j= i- gap; j>= 0 && A[j]> A[j+ gap]; j= j- gap)//如果同一组内前一个元素大于相 gap个位置的元素,则两者交换位置
        { 
          temp= A[j];
          A[j]= A[j+ gap];
          A[j+ gap]= temp;
      }
    }
  }
    return A[N/2];//返回中间元素
}

 

Let's review the insertion sorting:

As the name implies, insertion sorting is to insert each element of an array into the corresponding position of the previous ordered area according to the size relationship during the sorting process. For example, element 3 in the following array needs to be inserted in front of the three elements in the front ordered area according to the size relationship, and the first three elements move backward accordingly: 

By analogy, the insertion sort will take a total of (array length - 1) rounds, and the results of each round are as follows: 

 

The average time complexity of insertion sorting is O (n ^ 2). This sorting algorithm is not complex, but it is obviously not an efficient sorting algorithm. So, how to optimize the insertion sorting algorithm? We can start with two characteristics of insertion sorting: 1. When most elements are ordered, the workload of insertion sorting is small. It is obvious that if most elements of an array are ordered, then the elements in the array naturally do not need frequent comparison and exchange. 2. When the number of elements is small, the conclusion that the workload of insertion sorting is small is more obvious. The workload of insertion sorting is proportional to the square of N. if n is small, the workload of sorting is much smaller. 

How to preprocess the original array? Clever scientists have come up with a method of grouping and sorting to make some "rough adjustments" to the array. 

 

The so-called grouping means that the span between two elements of the same group is half of the total length of the array, that is, the span is 4. 

 

As shown in the figure, elements 5 and 9, 8 and 2, 6 and 1, 3 and 7 are in four groups. Next, let's have each group of elements sort independently. The sorting method can be inserted directly. Since the number of elements in each group is very small, there are only two, so the workload of inserting and sorting is very small. The array after each group of sorting is as follows: 

 

In this way, after only a few simple exchanges, the overall order of the array has been significantly improved, which greatly reduces the workload of subsequent direct insertion sorting. This method can be understood as "rough adjustment" to the original array. But this is not enough. We can further reduce the group span and repeat the above work. Reduce the span to half of the original, i.e. span 2, and regroup the elements: 

 

As shown in the figure, elements 5, 1, 9 and 6 are in one group, elements 2, 3, 8 and 7 are in one group, two groups in total. Next, let's continue to let each group of elements sort independently by inserting directly. The array after each group of sorting is as follows: 

 

At this point, the order degree of the array is further improved, which paves the way for the subsequent sorting. Finally, we further reduce the grouping span to 1, which is the same as direct insertion sorting. After a series of rough adjustments, the workload of direct insertion sorting is greatly reduced. The sorting results are as follows: 

 

Let's review the whole process of grouping and sorting: 

 

发布了131 篇原创文章 · 获赞 94 · 访问量 2949

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/105104957