I didn't know much about ascending programs.

Asked 1 years ago, Updated 1 years ago, 42 views

Some of the ascending programs are listed below.

 void sort (inta[], intn)
{
    inti,j,min,t;
    i = 0;
    while(i<n){
        min = i;
        j = i+1;
        while(j<n){
            if(a[j]<a[min]){
                min = j;
            }
            j = j+1;
        }
        t = a[i], a[i] = a[min], a[min] = t;
        i = i+1;
    }
}

First, I don't know what intn in void sort(inta[], intn) means (inta[] probably means a list).

Second, regarding the relationship between inti,j,i is simply referring to the numbers on the list, and j is referring to the list number, but can't min=a[j] be used for the 10th line above?

Third, I would like to know what j=i+1 means.

I don't understand the meaning of the second half at all, so for the time being, I would like to know about the three meanings above.And I'd like to think about the meaning of the second half.

c

2022-09-30 21:47

2 Answers

inta[] is sometimes referred to as a list in other languages, but it is an array in the C language.
And intn is the length (number of elements) of the array passed (inta[]).
An array of characters indicates termination by adding 0x00 at the end, but since inta[] passed here is an array of int, 0 is one valid value and cannot be used.
(The specification can be an array of values greater than or equal to 1, but I don't think it's used often.)

i and j are both indexers of the array that point to the sort comparison process.
Similarly, min is the index that points to the lowest value.min=a[j] cannot be min=a[j] because it does not hold the minimum value itself. min aa[j] is the process of retrieving the value, not the index.

j=i+1 sets the index of the other value for size comparison to the next of i.
j=i is my own so I can handle it, but it's better than increasing the comparison by one.


2022-09-30 21:47

I think it's better to understand the algorithm (solutions) before you understanding the program.

This sort is called select sort.The logic is that if you find the minimum value in an unsorted data column and replace it with the beginning, it will be sorted in ascending order.

Example: In the starting state, set the number of data 231 data to 3 this time starting position 0.

·Select the data 2 with subscript [0] (this time starting position) as the temporary minimum value.
·I have already checked the subscript [0], so I will check if there are any other subscripts [1,3) that are smaller than the temporary minimum. (You can check the range [0,3), but it is useless to check [0])
·The minimum value found is in the subscript [2], so replace [0] and [2] to end the first time (data is 132).
·The processing has been completed once = I found one minimum element, so I will continue the loop with the starting position as 1
·The last element inevitably becomes the maximum value, so the number of loops is one less than the number of data

The proposed source code faithfully implements the selection sorting procedure.Therefore, if you can read which of the words in the Oira's description the variables in the source code correspond to, you will understand why this code is written.


2022-09-30 21:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.