Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

Sort 2D matrix in C

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:
");
    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("
");
    }
    // Sort matrix
    for(i=0; i<10; i++) {
        for(j=0; j<10; j++) {
            // Sort algorithm goes here
        }
    }
    // Present sorted matrix
    printf("
Sorted matrix:
");
    for(i=0; i<10; i++) {
        for(j=0; j<9; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("
");
    }
    return 0;
}

I would be most thankful for an answer!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
#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:
");
    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("
");
    }
    // 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("
Sorted matrix:
");
    for(i=0; i<10; i++) {
        for(j=0; j<10; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("
");
    }
    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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...