Freshman C language problem, find a solution, and use the bubble method to output an array from larg

Updated on technology 2024-08-04
9 answers
  1. Anonymous users2024-02-15

    You first have to understand this algorithm, which is the simplest.

    #define size 8

    void bubble_sort(int a,int n)int i, j, temp;

    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;

    int main()

    int number[size] = ;

    int i;

    bubble_sort(number, size);

    for (i = 0; i < size; i++)printf("%d", number[i]);

    printf("");

  2. Anonymous users2024-02-14

    Because the subscript of the array starts from 0, the 9 of the two for statements should be changed to 8, and then the size of the order should be reversed by the bubble method, and the order should start with the last one, so the embedded for should be changed to <9-d, I hope it can help you.

    --The Language Support Group is at your service.

  3. Anonymous users2024-02-13

    The sorting step is incorrect, which is the for(b 0) segment.

  4. Anonymous users2024-02-12

    Arrays 0 to 8 are already 9 numbers.

  5. Anonymous users2024-02-11

    Arrays are stored sequentially in memory, although a[10] is out of bounds, but a[10] has a value, your program ascending sorting can be executed correctly, you change the if in the "to <, descending sorting, the program will error, this is because your a[10] is just a large number, a[9]>a[10] is not true, a[10] is meaningless.

    To make your program execute correctly in ascending or descending order, change it to for(i=0; i<9;i++)

    for(j=i;j<10-i-1;j++) on the line.

  6. Anonymous users2024-02-10

    The exact form of the bubble sorting method should look like this:

    for(i=0;i<9;i++)

    for(j=0;j<10-i-1;j++)if(a[j]>a[j+1])

    for(i=0;i<10;i++)

    printf("%4d",a[i]);

    printf("");

  7. Anonymous users2024-02-09

    Actually, it's your two loops that are wrong, for(i=0; i<9;i++)for(j=0;j<10-i;j++) These two loops should be written like this: for(i=1; i, start i=1, j<9, then the cycle is from j=0 to j=8, so that the cycle is 9 times, that is, the first number and the next 9 numbers are compared, and the following are compared in turn to the end, and the output result.

  8. Anonymous users2024-02-08

    j can't be 9; So that your two problems are solved!

    #include

    main()

    for(i=0;i<9;i++)

    for(j=0;j<9-i;j++) j<10-1-i is wrong here.

    if(a[j]>a[j+1])

    for(i=0;i<10;i++)

    printf("%4d",a[i]);

    printf("");}

  9. Anonymous users2024-02-07

    Classmates, first solve problem 1:

    Your first problem is correct, a[10] is indeed out of bounds, the more you need to deal with it again, every time j should not be 10-i but 10-i-1; Why is it working?

    vs is not going to work. There is no data boundary check under vc 6, so it works fine. When I debugged here, it showed a[10], that is, the value of the element that you said was out of bounds by default:

    1638280 So, it certainly won't affect your sorting because if(a[j]>a[j+1]).

    So it looks like it works fine. (If you know how to debug, you won't have this problem.)

    Problem 2 is the problem you understand, bubbling sorting is to put the largest one at the end each time, then the next round does not need the last element of the round, for example, 4 3 2 1 becomes 3 2 1 4 then you don't need to consider 4 when arranging. In the final round, as long as there are so many j=0 shoots j=1, it doesn't matter, and the later ones are bigger than the previous ones, so naturally don't compare!!

    It's not very clear, I hope it can solve the doubts in your mind. Please adopt!! Hee-hee......

Related questions
10 answers2024-08-04

Here's a C implementation that assigns an array a[10] to p[n]: >>>More

5 answers2024-08-04

Ok, here's the **:

#include >>>More

12 answers2024-08-04

One-dimensional arrays hold the same values, but in the case of matrices, determinants. >>>More

6 answers2024-08-04

Summary. You can put the elements of both arrays into one array and then use the sort() function. >>>More

10 answers2024-08-04

1. First write the content of the note, as shown in the figure below. >>>More