Hỏi về sắp xếp trong ma trận C

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) 
{
	int A[100][100],n,i,j,tam;
	printf("Nhap so phan tu cua mang:");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			printf("A[%d][%d]=",i,j);
			scanf("%d",&A[i][j]);
		}
	}
	printf("Ma tran vua nhap la:\n");
		for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			printf("%d ",A[i][j]);
		}
		printf("\n");
	}
	for(i=0;i<n-1;i++)
	{
		for(j=i;j<n;j++)
		{
			if(A[i][j]>A[i][j+1])
			{
				tam=A[i][j];
				A[i][j]=A[i][j+1];
				A[i][j+1]=tam;
			}
		}
	}
	printf("Ma tran sau khi sap xep:\n");
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			printf("%d ",A[i][j]);
		}
		printf("\n");
	}
	return 0;
}

Mọi người ơi em đang làm bài sắp xếp ma trận tăng dần theo dòng nhưng chạy không đúng,mọi người xem dùm

dùng lib sẵn thì dễ hơn. nhưng dùng cách cổ điển đã :smile:

à,em hieu y bac roi nhung cho em thắc mắc 1 chỗ if(A[i][j] >A[x][y]) mới đúng chứ nhỉ.xep tang dan mà :3

ở dưới là temp … A[x][y] trước :smile: (chuẩn bị đem bàn phím đi sửa, dấu cộng gõ k dc) :cry:

:3 zậy nếu đề if(A[i][j] >A[x][y]) thi temp=A[i][j] truoc dung ko ?

sao em chạy không ra dược zay @Huy -_-

bác Huy hình như hieu sai de roi thi phải, sap xep tang dần theo dòng thoi ak

làm như câu a đấy bác Huy :smile:

à. thì cho chạy trên từng dòng rồi sort, chạy khi nào bằng cột-1 thì nhảy dòng kế tiếp …

sort dong roi nhung ko dc T_T

####Đơn giản là viết 1 hàm sắp xếp , viết hàm lấy theo hàng,cột rồi sắp lại thôi.

  • Demo:
#include <stdio.h>
#include <stdlib.h>

int less(const void* a,const void * b){
    return *(int*)a - *(int*)b;    
}

int greater(const void* a,const void *b){
    return -less(a,b);
}

int m,n;
int a[100][100];

int tmp[100];

void show(const char *s){
    int i,j;
    puts(s);
    for(j=0;j<m;++j){
        for(i=0;i<n;++i){
            printf("%4d",a[j][i]);
        }
        printf("\n");
    }
}


void take_by_row(int row){
    int i;
    for(i=0;i<n;++i){
        tmp[i]=a[row][i];
    }
}

void set_by_row(int row){
    int i;
    for(i=0;i<n;++i){
        a[row][i]=tmp[i];
    }
}

void take_by_column(int col){
    int i;
    for(i=0;i<m;++i){
        tmp[i]=a[i][col];
    }
}

void set_by_column(int col){
    int i;
    for(i=0;i<m;++i){
        a[i][col]=tmp[i];
    }
}
void tang_trai_phai(){
    
    int i;
    for(i=0;i<m;++i){
        take_by_row(i);
        qsort(tmp,n,sizeof(int),less);
        set_by_row(i);
    }
}
void tang_tren_duoi(){
    
    int i;
    for(i=0;i<n;++i){
        take_by_column(i);
        qsort(tmp,m,sizeof(int),less);
        set_by_column(i);
    }
}
void giam_trai_phai(){
    
    int i;
    for(i=0;i<m;++i){
        take_by_row(i);
        qsort(tmp,n,sizeof(int),greater);
        set_by_row(i);
    }
}
void giam_tren_duoi(){
    
    int i;
    for(i=0;i<n;++i){
        take_by_column(i);
        qsort(tmp,m,sizeof(int),greater);
        set_by_column(i);
    }
}



int main() {
    scanf("%d%d",&m,&n);
    int i,j;
    for(j=0;j<m;++j){
        for(i=0;i<n;++i){
            scanf("%d",a[j]+i);
        }
    }
    tang_trai_phai();
    show("tang trai-phai");
    giam_trai_phai();
    show("giam trai-phai");
    tang_tren_duoi();
    show("tang tren-duoi");
    giam_tren_duoi();
    show("giam tren-duoi");
    
    return 0;
}

#output
tang trai-phai
   1   2   8
   3   7   9
   4   5   6
giam trai-phai
   8   2   1
   9   7   3
   6   5   4
tang tren-duoi
   6   2   1
   8   5   3
   9   7   4
giam tren-duoi
   9   7   4
   8   5   3
   6   2   1

2 Likes

:v tốn O(n^4) bước :v 4 vòng for

Cái dấu - trước less là gì vậy anh ?

thật ra còn nhiều cách khác gọn hơn nhiều :joy:

2 Likes

:joy: :joy: :joy:

int character = 20;

Đảo ngược trật tự thôi :joy:
-(a-b)= b-a :smiley:

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?