c语言中同时为两个数组排序

1、

#include <stdio.h>
#include <string.h>

#define NUMBER 5
#define NAME_LEN 64

void swap_int(int *x, int *y)
{
    int tmp = *x;
    *x = *y;
    *y = tmp;
}

void swap_str(char *s1, char *s2) //字符串的交换程序 
{
    char tmp[NAME_LEN];
    strcpy(tmp, s1);
    strcpy(s1, s2);
    strcpy(s2, tmp);
}

void sort(int x[], char y[][NAME_LEN], int n)
{
    int i, j;
    for(i = 0; i < n - 1; i++)
    {
        for(j = n - 1; j > i; j--)
        {
            if(x[j - 1] > x[j])
            {
                swap_int(&x[j - 1], &x[j]);
                swap_str(y[j - 1], y[j]);
            }
        }
    }
}

int main(void)
{
    int i;
    int height[] = {178,175,173,165, 179};
    char name[][NAME_LEN] = {"Sato","Sanata","Takao","Mike","Masaki"};
    
    for(i = 0; i < NUMBER; i++)
        printf("NO-%d: %-8s %3d
", i + 1, name[i], height[i]);
    sort(height, name, NUMBER);
    puts("
==================
");
    for(i = 0; i < NUMBER; i++)
        printf("NO-%d: %-8s %3d
", i+ 1, name[i], height[i]);
    return 0; 
}

原文地址:https://www.cnblogs.com/liujiaxin2018/p/14851577.html