By GokiSoft.com| 20:11 20/09/2023|
Lập Trình C

Bài toán sắp xếp trong C - Lập trình C BT1910

Nhập vào một mảng số nguyên t gồm N phần tử. Trong đó N được nhập từ bàn phím

Yêu cầu : sắp xếp mảng N theo thứ tự giảm dần

Ví du: mảng nhập vào là 1, 6, 2, 5, 10, 80

Mảng sau khi sắp xếp : 80, 10, 6, 5, 2, 1

Liên kết rút gọn:

https://gokisoft.com/1910

Bình luận

avatar
Nguyễn Hữu Hiếu [T2008A]
2020-10-01 04:49:10



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	
	int n, i, j, temp;
	printf("\nNhap so phan tu n = ");
	scanf("%d", &n);
	int mang[n];
	for(i=0;i<n;i++) {
		printf("Nhap phan tu thu %d: ", i+1);
		scanf("%d", &mang[i]);
	}
	printf("\nCac phan tu cua mang la: ");
	for(i=0;i<n;i++) {
		printf("%d, ", mang[i]);
	}
	printf("\nCac phan tu cua mang sau khi sap xep theo thu tu giam dan la: ");
	for(i=0;i<n-1;i++) {
		for(j=i;j<n;j++) {
			if (mang[i] < mang[j]) {
				temp = mang[i];
				mang[i] = mang[j];
				mang[j] = temp;
			}
		}
	}
	for(i=0;i<n;i++) {
		printf("%d, ", mang[i]);
	}	
	
	return 0;
}


avatar
Nguyễn Anh Vũ [T2008A]
2020-09-22 17:33:00



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	//bat dau code;
	int N;
	printf("\nNhap N : ");
	scanf("%d", &N);
	int t[N];
	
	int i, j, temp;
	for(i=0;i<N;i++){
		printf("\nNhap N[%d] = ", i);
		scanf("%d", &t[i]);
		}
		
	for (i=0;i<N-1;i++){
		for(j=0;j<N;j++) {
		if(t[i]<t[j]){
			temp= t[i];
			t[i]=t[j];
			t[j]=temp;                       
	}		
	
	}
	
	}
		printf("\nMang sau khi sap xep: ");
		for (i=0;i<N;i++);{
		printf("%d ",t[i] );}
		}
	//ket thuc code;
	return 0;
}


avatar
Nguyễn Xuân Mai [T2008A]
2020-09-22 13:55:08



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int i,n,a,j;
	printf("\nNhap so nguyen N:");
	scanf("%d",&n);
	int t[n];
	for(i=0;i<=n-1;i++){
		printf("\nNhap t[%d]:",i);
		scanf("%d",&t[i]);
	}
	for(i=0;i<=n-1;i++){
		for(j=i+1;j<n;j++){
			if(t[i]<t[j]){
				a = t[i];
				t[i] = t[j];		
				t[j] = a;
			}
		}
	}
	for(i=0;i<=n-1;i++){
		printf("%d",t[i]);
	}

	return 0;
}


avatar
Trần Văn Lâm [T2008A]
2020-09-22 13:27:55



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int N;
	printf("\nNhap so N=");
	scanf("%d", &N);
	int h[N],i;
	for(i = 0; i < N; i++){
		printf("\nNhap h[%d]=%d", i);
		scanf("%d", &h[i]);
	}
	int j, temp;
	for(i = 0; i < N - 1; i++){
		for(j = i + 1; j < N; j++){
			if(h[i] > h[j]){
				temp = h[i];
				h[i] = h[j];
				h[j] = temp;
			}
		}
	}
	printf("\nMang giam dan la:");
	for(i = 0; i < N; i++){
		printf("%d", &h[N]);
	}

	return 0;
}


avatar
hainguyen [T2008A]
2020-09-22 12:20:52



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int N;
	printf("\nNhap N: ");
	scanf("%d", &N);
	int t[N];
	int i, j, num;
	for(i=0; i<N; i++){
		printf("\nNhap t[%d] = ", i);
		scanf("%d", &t[i]);
	}
	for(i=0; i<N-1; i++){
		for(j=i+1; j<N; j++){
			if(t[i]<t[j]){
				num = t[i];
				t[i] = t[j];
				t[j] = num;
			}
		}
	}
	printf("\nSau khi sap xep: ");
	for(i=0; i<N; i++){
		printf("\n%d", t[i]);
	}
	
	return 0;
}


avatar
Triệu Văn Lăng [T2008A]
2020-09-22 08:43:31



#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int n, i, j, sx;
	printf("\nnhap n: "); scanf("%d",&n);
	int *a;
	a=(int*) malloc (n* sizeof(int));
	for(i=0;i<n;i++) {
		printf("\nnhap a[%d]= ",i);
		scanf("%d",a+i);
	}
	for(i=0;i<n-1;i++){
		for(j=i+1;j<n;j++) {
			if(*(a+i)<*(a+j)) {
				sx=*(a+i);
				*(a+i)=*(a+j);
				*(a+j)=sx;
			}
		}
	}
	printf("\nday sau khi sap xep: ");
	for(i=0;i<n;i++){
		printf("%d", *(a+i));
	}
	return 0;
}


avatar
Do Trung Duc [T2008A]
2020-09-22 04:53:25



//Nhap mang N phan tu, sap xep phan tu tho thu tu giam dan

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	
	int N;
	printf("Nhap N = ");
	scanf("%d", &N);
	
	int temp,a,i, j,t[N];
	
	for(i=0; i<N; i++){
		printf("Nhap gia tri phan tu t[%d] = ", i);
		scanf("%d", &t[i]);	
	}
	
	for(i=0; i<N-1; i++){
		for(j=i+1; j<N; j++){
			if(t[i]<t[j]){
				temp = t[i];
				t[i] = t [j];
				t[j] = temp;
	}
	}		
	}
	printf("\nDay phan tu giam dan nhu sau:");
			
	for (i=0;i<N;i++){
		printf("%d", t[i]);	
	}
	return 0;
	
}


avatar
Trần Thị Khánh Huyền [T2008A]
2020-09-22 00:40:58



int main(int argc, char *argv[]) {
	int N;
	printf("\nNhap N: ");
	scanf("%d", &N);
	int t[N];
	int i, j, tam;
	for(i=0;i<N;i++) {
		printf("\nNhap N[%d] = ", i);
		scanf("%d", &t[i]);}
	for (i=0;i<N-1;i++){
		for (j=i+1;j<N;j++){
		if(t[i]<t[j]){
			tam= t[i];
			t[i]=t[j];
			t[j]=tam;
		}
		}
		
	}
	printf("Sau khi sap xep:");
	for (i=0;i<N;i++){
	printf("%d;",t[i]);}
	
	return 0;
}


avatar
nguyễn Sử [T2008A]
2020-09-21 14:42:25



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int N, i, j;
	printf("\nNhap N: ");
		scanf("%d",&N);
		int n[N];
	for(i=0;i<N;i++){
		printf("\n nhap N %d = ",i);
		scanf("%d",&n[i]);		
}	
		  int tg;
    for( i = 0; i < N - 1; i++){
        for( j = i + 1; j < N; j++){
            if(n[i] < n[j]){
            	  tg = n[i];
                n[i] = n[j];
                n[j] = tg;        
            }
        }
    }

    printf("\nMang da sap xep la: ");
    for( i = 0; i < N; i++){
        printf("%5d", n[i]);
    


   }         	

	
	return 0;
}


avatar
vuong huu phu [T2008A]
2020-09-21 13:14:04



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int n,i,f,t;
	printf("\n nhap so phan tu ");
	scanf("%d",&n);
	int a[n];
	for(i=0;i<n;i++){
		printf("\n nhap phan tu %d = ",i);
		scanf("%d",&a[i]);}
	for(i=0;i<=n-1;i++){
	for(f=i+1;f<n;f++) {
		if (a[i]<a[f]){
			t=a[i];
			a[i]=a[f];
			a[f]=t;		}}	}
	for (i=0;i<n;i++){
		printf(" %d ",a[i]);
	}

	return 0;
}