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:
/************************************************************ * Author: VNCODING * History * 2014/10/05 first create VNCODING *************************************************************/ #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ả:
Nhap kich thuoc mang: 6 A[0] = 2 A[1] = -3 A[2] = 9 A[3] = 6 A[4] = 11 A[5] = 13 Day so sau khi sap xep: A[0] = 13 A[1] = 11 A[2] = 9 A[3] = 6 A[4] = 2 A[5] = -3
Leave a Reply
You must be logged in to post a comment.