Subscript output from the array values in order of size from large to small

Source: Internet
Author: User

We can use bubble, quick row, select and other sorting methods to achieve array sorting, but sometimes we do not want to change the order of the array, just want to know its order, and then its subscript output, how should be implemented.

Simple example:

                int num[] = {9, ten, 7, 2, 8, 3, 4, 5, 1, 6};
                int flag[10];        The subscript corresponding to the value of the element from large to small, for example flag[0] 10 subscript 1 for
                (int j = 0; J <; J + +) {
                    int max = 0, min = 0;
                    for (int i = 0; i < i++) {
        
                        if (Num[i] > Num[max]) {
                            max = i;
                        }
        
                        if (Num[i] < num[min]) {
                            min = i;
                        }
                    }
                    printf ("%d%d%d\n", Max, Min, num[min]);
                    FLAG[J] = max;
                    Num[max] = num[min]-ten;
                    printf ("%d\n", Num[max]);
                }
                for (int i = 0; i < i++) {   //The flag is traversed sequentially to get a value from a large to a small subscript
                    printf ("%d", Flag[i]);
                }

Write the following code into the function Xiabiao_sort:

void Xiabiao_sort (int num[], int n) {
    int flag[n];
    for (int j = 0; J < N; j + +) {
        int max = 0, min = 0;
        for (int i = 0; i < n; i++) { 
            if (Num[i] > Num[max]) {
                max = i;
            }
            if (Num[i] < num[min]) {
                min = i;
            }
        }
        FLAG[J] = max;
        Num[max] = num[min]-ten;
    }
        for (int i = 0; i < i++) {
            printf ("%d", Flag[i]);}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.