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
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
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Nguyễn Hữu Hiếu [T2008A]](https://www.gravatar.com/avatar/ca2884508b617fee77f000c7d99c219d.jpg?s=80&d=mm&r=g)
Nguyễn Hữu Hiếu
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;
}
![Nguyễn Anh Vũ [T2008A]](https://www.gravatar.com/avatar/8863d24ed74b396082becbc4db8331fd.jpg?s=80&d=mm&r=g)
Nguyễn Anh Vũ
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;
}
![Nguyễn Xuân Mai [T2008A]](https://www.gravatar.com/avatar/d3d863d6f47708501814fb41e9c38f31.jpg?s=80&d=mm&r=g)
Nguyễn Xuân Mai
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;
}
![Trần Văn Lâm [T2008A]](https://www.gravatar.com/avatar/cfc15c8cb7781ad669b013e01f9f1a6b.jpg?s=80&d=mm&r=g)
Trần Văn Lâm
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;
}
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
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;
}
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
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;
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
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;
}
![Trần Thị Khánh Huyền [T2008A]](https://www.gravatar.com/avatar/554e115833778e4294a01aebe228f3d6.jpg?s=80&d=mm&r=g)
Trần Thị Khánh Huyền
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;
}
![nguyễn Sử [T2008A]](https://www.gravatar.com/avatar/47487be2776ac2ec915b0936ef7ab5ae.jpg?s=80&d=mm&r=g)
nguyễn Sử
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;
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
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;
}