用C排序二维矩阵 [英] Sort 2D matrix in C

查看:218
本文介绍了用C排序二维矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的C语言编程,并有但我似乎不能一个问题找出我自己。

我已经创建了一个2维矩阵,并且需要使用冒泡排序矩阵中的所有100个随机产生的数排序。我还需要为以后使用输出保存在我的计划。有没有人有任何想法?

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;诠释主(){
    INT I,J,矩阵[10] [10];    //在矩阵中产生随机数
    的printf(数字生成:\\ n);
    函数srand((INT)时间(NULL));
    对于(I = 0; I&小于10;我++){
        为(J = 0; J&10 9; J ++){
            矩阵[I] [J] =(RAND()900%)+ 100;
            的printf(%d个,矩阵[I] [J]);
        }
        的printf(\\ n);
    }
    //矩阵排序
    对于(I = 0; I&小于10;我++){
        为(J = 0; J&小于10; J ++){
            //排序算法到这里
        }
    }
    // present分类矩阵
    的printf(\\ nSorted矩阵:\\ n);
    对于(I = 0; I&小于10;我++){
        为(J = 0; J&10 9; J ++){
            的printf(%d个,矩阵[I] [J]);
        }
        的printf(\\ n);
    }
    返回0;
}

我将不胜感激的答案!


解决方案

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;
#包括LT&;&string.h中GT;无效bubble_sort(无效*基地​​,为size_t N,为size_t大小,
                 INT(* CMP)(常量无效*,常量无效*));INT CMP_INT(常量无效*一,常量无效* B){
    INT X = *(const int的*)一个;
    INT Y = *(const int的*)B:
    返回X< ÿ? -1:X> ÿ;
}诠释主(){
    INT I,J,矩阵[10] [10];    //在矩阵中产生随机数
    的printf(数字生成:\\ n);
    函数srand((无符号)时间(NULL));
    对于(I = 0; I&小于10;我++){
        为(J = 0; J&小于10; J ++){
            矩阵[I] [J] =兰特()%900 + 100;
            的printf(%d个,矩阵[I] [J]);
        }
        的printf(\\ n);
    }
    //矩阵排序
    对于(I = 0; I&小于10;我++){
        bubble_sort(矩阵[I],10,的sizeof(**基质),CMP_INT);行的//排序列
    }
    bubble_sort(矩阵,10的sizeof(*矩阵),CMP_INT); //排序第一列行。
    // present分类矩阵
    的printf(\\ nSorted矩阵:\\ n);
    对于(I = 0; I&小于10;我++){
        为(J = 0; J&小于10; J ++){
            的printf(%d个,矩阵[I] [J]);
        }
        的printf(\\ n);
    }
    返回0;
}无效掉期(void *的一个,无效* B,为size_t大小){
    无效* TEMP =的malloc(大小);
    的memcpy(温度,一,大小);
    的memcpy(A,B,大小);
    的memcpy(B,温度,大小);
    免费(TEMP);
}无效bubble_sort(无效*基地​​,为size_t N,为size_t大小,INT(* CMP)(常量无效*,常量无效*)){
    为(; N大于0; --N){
        INT I,swaped = 0;
        对于(i = 0; I< N-1 ++ I){
            无效* A =(字符*)基地+我*的大小;
            无效* B =(字符*)基地+(I + 1)*尺寸;
            如果(CMP(A,B)大于0){
                交换(A,B,大小);
                swaped = 1;
            }
        }
        如果(!swaped)
            打破;
    }
}

I am new to programming in C, and have but one problem that I can't seem to figure out on my own.

I have created a 2-dimensional matrix and need to sort all 100 randomly generated numbers in the matrix using bubble sort. I also need to save the output for later use in my program. Has anybody got any idea?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    int i, j, matrix[10][10];

    // Generate random numbers in a matrix
    printf("Numbers generated:\n");
    srand((int)time(NULL));
    for(i=0; i<10; i++) {
        for(j=0; j<9; j++) {
            matrix[i][j] = (rand()%900) + 100;
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    // Sort matrix
    for(i=0; i<10; i++) {
        for(j=0; j<10; j++) {
            // Sort algorithm goes here
        }
    }
    // Present sorted matrix
    printf("\nSorted matrix:\n");
    for(i=0; i<10; i++) {
        for(j=0; j<9; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    return 0;
}

I would be most thankful for an answer!

解决方案

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

void bubble_sort(void *base, size_t n, size_t size,
                 int (*cmp)(const void*, const void *));

int cmp_int(const void *a, const void *b){
    int x = *(const int *)a;
    int y = *(const int *)b;
    return x < y ? -1 : x > y;
}

int main() {
    int i, j, matrix[10][10];

    // Generate random numbers in a matrix
    printf("Numbers generated:\n");
    srand((unsigned)time(NULL));
    for(i=0; i<10; i++) {
        for(j=0; j<10; j++) {
            matrix[i][j] = rand()%900 + 100;
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    // Sort matrix
    for(i=0; i<10; i++) {
        bubble_sort(matrix[i], 10, sizeof(**matrix), cmp_int);//sort column of row
    }
    bubble_sort(matrix, 10, sizeof(*matrix), cmp_int);//sort row by 1st column.
    // Present sorted matrix
    printf("\nSorted matrix:\n");
    for(i=0; i<10; i++) {
        for(j=0; j<10; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    return 0;
}

void swap(void *a, void *b, size_t size){
    void *temp = malloc(size);
    memcpy(temp, a   , size);
    memcpy(a   , b   , size);
    memcpy(b   , temp, size);
    free(temp);
}

void bubble_sort(void *base, size_t n, size_t size, int(*cmp)(const void*, const void *)){
    for(; n>0; --n){
        int i, swaped = 0;
        for(i=0; i<n-1; ++i){
            void *a = (char*)base + i*size;
            void *b = (char*)base + (i+1)*size;
            if(cmp(a, b)>0){
                swap(a, b, size);
                swaped = 1;
            }
        }
        if(!swaped)
            break;
    }
}

这篇关于用C排序二维矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆