O - young singer Jury Grand Prix _ scoring HDU - 2014+ different sort of analysis

O - young singer Jury Grand Prix _ scoring   HDU - 2014 

Young Singer Grand Prix, the judges gave players scoring. Players score points rule to remove a maximum and a minimum score, then calculate the average score, please programmed output of a player's score. 

Input

A plurality of input data sets, each one row, each row is the first number n (2 <n <= 100), represents the number of judges, then the n judges scoring.

Output

For data input, the output of each player's score, the results of 2 decimal places, each output per line.

Sample Input

3 99 98 97
4 100 99 98 97

Sample Output

98.00
98.50

Code Example:

#include<stdio.h>
#define N 100
int main(){
    int n;
    while(~scanf("%d",&n)){
        int temp,sum,i,j,a[N]={0};
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(j=0;j<n-1;j++)   //冒泡排序
            for(i=0;i<n-1-j;i++)
                if(a[i]>a[i+1]){
                    temp=a[i];
                    a[i]=a[i+1];
                    a[i+1]=temp;
                }
        for(sum=0,i=1;i<n-1;i++)
            sum=sum+a[i];
        printf("%.2f\n",(float)sum/(n-2));
    }
}

Sort of question

Before introducing scheduling problem, time complexity (an algorithm complexity) concept first introduced.

In various algorithms, the algorithm if the number of statement execution is a constant, the time complexity is O (1), Further, the frequency is not the same time, the same time complexity possible, such as T (n) = n ^ 2 + 3n + 4 and 4n ^ 2 + 2n + different T (n) = 1 to their frequency, but the same time complexity, are O (n ^ 2).

Bloggers Due to limited capacity, currently only a brief introduction in several ways

  1. Bubble Sort:
  2. Selection Sort
  3. Insertion Sort
  4. Quick Sort

1. Bubble Sort

Bubble Sort (Bubble Sort), is a computer science simpler in the field of sorting algorithms .

It repeatedly visited elements of the column to sort, in order to compare two adjacent elements, if their order (such as descending, the first letter from A to Z) errors they exchange took over. Working visit element is repeated until there is no need to swap adjacent elements, which means that the element has the sort is complete.

Bubble sort is a relatively simple sort, the average time complexity   .


2. Select Sort

Selection Sort (Selection sort) is a simple and intuitive sorting algorithm . It works are to be sorted from each data element of the selected minimum (or maximum) of an element stored in the starting position of the sequence, and then continue to look for the minimum (large) elements from the remaining unsorted elements, then sorted into the end of the sequence. And so on, until all the data elements to be sorted drained. Select sort is unstable sorting method.

Time complexity with bubble sort.

Code Example:

void swap(int*a,int*b){
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}
void sort(int a[], int n){
    int i, j, min, temp;
    for (i = 0; i < n - 1; i++){
        min = i;
        for (j = i + 1; j < n; j++)
        if (a[j] < a[min]) min = j;
        swap(a[i], a[min], temp);
    }
}

3. Insertion Sort

There has ordered a sequence of data required in this row of good data has been inserted into the sequences of a number, but still requires the insertion of this sequence data ordered, this time is necessary to use a new sorting method - Insertion Sort method , the basic operation is the insertion sort data into an already sorted ordered data to obtain a new, plus a number of sequenced data, sorting algorithm is suitable for small amounts of data, the time complexity is O (n ^ 2). Stable sorting method.

Proceed as follows:

  1. Starting with the first element, which can be considered to have been sorted. 
  2. Remove the next element, element scanning sequence has been sorted from back to front.
  3. If the element (sorted) is greater than the new element, the element to the next position.
  4. Repeat step 3 until it finds the sorted elements less than or equal to the new position of the element.
  5. The new element is inserted into the rear position.
  6. Repeat steps 2-5.

In the worst case, bubble sort requires secondary swap, and insertion sort as long as most exchange. Achieve bubble sort will generally have poorly sorted columns perform ( ), is inserted in this case only needs to sort op. Based on the usual reasons for using textbooks as a case of bubble sort, insertion sort that most people rarely use.

Code Example:

void sort(int a[],int first,int last){ //在数组a中对first到last元素进行排列。
    int i,j,temp;
    for(i=first+1;i<last;i++){
        temp=a[i];
        j=i-1;
        //与已排序的数逐一比较,大于temp时,该数移后
        while(j>=0&&a[j]>temp){
            a[j+1]=a[j];
            j--;
        }
        //存在大于temp的数
        if(j!=i-1) a[j+1]=temp;
    }
}

Code optimization:

void sort(int *a,int n){
    int i,j,temp;
    for(i=1;i<n;i++){
        temp=*(a+i);
        for(j=i;j>0&&*(a+j-1)>temp;j--)
            *(a+j)=*(a+j-1);
        *(a+j)=temp;
    }
}

4. quick sort

Quick sort (Quicksort) is a bubble sort An improved.

Quick sort proposed by CAR Hoare in 1962. The basic idea is: a trip by ordering the data to be sorted into two independent parts, the part where all the data than the other part of all data to be small, then this method of data for the two parts separately fast sorting, sorting the entire process can be recursively carried out, in order to achieve the whole data into an ordered sequence .

A trip to quick sort algorithm is:

  1. Set two variables i, j, ordered beginning: i = 0, j = N -1;
  2. In the first array element as the key data, assigned to Key , i.e. Key = A [0];
  3. J forward search from the beginning, i.e. starting from the forward search (J,), to find a less than first key value A [j], the A [j] and A [i] are interchangeable;
  4. Search backward starting from i, i.e., before the start of the backward search (i ++), find the first is greater than the key of A [i], the A [i] and A [j] interchangeable;
  5. Repeat steps 3 and 4 until i = j; (3,4 step, did not find qualified value, i.e. 3 A [j] is not less than the key , in. 4 A [i] is not greater than the key time change j the value of i such that j = j-1, i = i + 1, until you find found qualified value, when the exchange for i, j pointer position unchanged. Further, i == j certain process exactly when i + j- or completed, so that at this time the end of the cycle).

Sort presentation

Examples

Suppose the user inputs the following array:

Subscript

0

1

2

3

4

5

data

6

2

7

3

8

9

Create the variable i = 0 (first data point), j = 5 (the last data point), k = 6 ( assigned to the first data value).

We should all be smaller than the number k k move to the left, so we can start to look smaller than the number 6, j from the beginning, from right to left to find, diminishing the value of the variable j, we find the first subscript 6 is smaller than 3 data, then the data 3 to the position of index 0, the index 0 to the next lower subscript 3 6 data, the first comparison is completed:

Subscript

0

1

2

3

4

5

data

3

2

7

6

8

9

i=0 j=3 k=6

Then, start the second comparison, this time looking to become larger than k of it, and from front to find. Incrementing the variable i, the data found at the first subscript 2 is larger than k, and in exchange 6, the data state so that data with the subscript 2 and 7 3 j index points to data becomes the following table:

Subscript

0

1

2

3

4

5

data

3

2

6

7

8

9

i=2 j=3 k=6

The above said two comparisons is a cycle.

Subsequently, then decrement the variable j, repeated cycles compared above.

In this example, we conduct a cycle, discovered i, and j "meet": they all point to the subscript 2. So, the first pass completion of the comparison. The results obtained are as follows, any number k (= 6) to the left than it is small, all right number of k than it big:

Subscript

0

1

2

3

4

5

data

3

2

6

7

8

9

If i and j do not meet, then it is a sliding scale i find a large, but also not, then find a small decrements j, and so forth, constantly circulating. Note judgment and are looking for the same time.

Then, the data on both sides of k, then each packet in the above-described process until a packet can not be so far.

Note: The first pass quick sort will not directly get the final result, only the ratio k k large and small than the number assigned to both sides k. In order to obtain a final result, the need to re-index the array 2 are performed on both sides of this step, and then decomposing the array until the array can not be decomposed until the (only one data), to get the correct result.

Code Example:

void sort(int *a, int left, int right){
    if(left >= right)
        return 
    int i = left, j = right, key = a[left];
    while(i < j)           {
        while(i < j && key <= a[j])  j--;
        a[i] = a[j];
        while(i < j && key >= a[i])  i++;
        a[j] = a[i];
    a[i] = key;
    sort(a, left, i - 1);
    sort(a, i + 1, right);
}
                     

Quick Sort time complexity is O (n × log2n)

 

Published 24 original articles · won praise 7 · views 1903

Guess you like

Origin blog.csdn.net/weixin_43426647/article/details/84725284