Hi, I am

Ngô Tôn

I am a programmer.

Home / Programming / C/C++ / Sắp xếp mảng theo thứ tự giảm dần

Sắp xếp mảng theo thứ tự giảm dần

Yêu cầu:

– Sắp xếp mảng theo thứ tự giảm dần.

Thuật toán:

– Có rất nhiều thuật toán sắp xếp. Ở đây, mình lựa chọn phương pháp bubble sort với lý do là cài đặt đơn giản.

Code:

/************************************************************
#include "stdio.h"
#include "conio.h"

#define LEN_MAX 100 // do dai toi da cua mang

void sort_in_desending_order(int A[], int len);

void main()
{
int arr[LEN_MAX];
int n;
int i;
do
{
printf("\nNhap kich thuoc mang: ");
scanf("%d", &n);
}
while (n > LEN_MAX || n <= 0);

//Nhap du lieu cho day so
for (i = 0; i < n; i++)
{
printf("\nA[%d] = ", i);
scanf("%d", &arr[i]);
}

// Sap xep theo thu tu giam dan
sort_in_desending_order(arr, n);

printf("\nDay so sau khi sap xep: ");
for (i = 0; i < n; i++)
{
printf("\nA[%d] = %d", i, arr[i]);
}
getch();
}

void sort_in_desending_order(int A[], int len)
{
int i, j;
int temp;
// Thuat toan bubble sort
for (i = 0; i < len - 1; i++)
for(j = i+1; j < len; j++)
if(A[i] < A[j])
{
temp = A[j];
A[j] = A[i];
A[i] = temp;
}
}

Kết quả:

About ngoton

Ngô Tôn is a programmer with passion for tailored software solutions. Comes with 7+ years of IT experience, to execute beautiful front-end experiences with secure and robust back-end solutions.

Check Also

Xây dựng hàm bạn để tính diện tích hình chữ nhật

Yêu cầu: Xây dựng hàm bạn để tính diện tích hình chữ nhật Code: Giải …

Leave a Reply

avatar
  Subscribe  
Notify of