======================================================================
Name : sort.cpp
Author : CTK
Version :
Copyright : Your copyright notice
Description : Hello World in C++, Ansi-style
=======================================================================
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
using namespace std;
// thuat toan insertSort
void insertSort(int A[], int n) { // khai bao ham insertSort
for (int i = 0; i < n; i++) { //cho bien i lam bien chay
int j = i - 1; // gan gia tri i cho j-1, j la bien lui ve
int tmp = A[i]; // gan gia tri i cho tmp
while (tmp < A[j] && j >= 0) {
A[j + 1] = A[j];
j--;
}
A[j + 1] = tmp;
}
}
// doi cho
void swap(int &a, int &b) {
int tmp = a;
a = b;
b = tmp;
}
//thuat toan selectSort
void selectSort(int A[], int n) {
for (int i = 0; i < n - 1; i++) {
int count = i;
for (int j = i + 1; j < n; j++) {
if (A[count] > A[j]) {
swap(A[count], A[j]);
}
}
if (i != count) {
swap(A[count], A[i]);
}
}
}
// thuat toan sap xep bubble sort
void bubbleSort(int A[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (A[i] > A[j])
swap(A[i], A[j]);
}
}
}
int findMax(int A[], int n) {
int max = A[0], i;
for (int i = 0; i < n; i++) {
if (A[i] > max) {
max = A[i];
}
}
return max;
}
int findMin(int A[], int n) {
int min = A[0], i;
for (int i = 0; i < n; i++) {
if (A[i] < min) {
min = A[i];
}
}
return min;
}
// tinh gia tri trung binh trong mang
float trungBinh(int A[], int n) {
float s = 0;
float avg = 0;
for (int i = 0; i < n; i++) {
s += A[i];
}
avg = (float) (s / n);
return avg;
}
// tim do lech giua hai gia tri trong mang
void chenhLech(int A[], int n) {
int i, j;
cout << "nhap phan tu thu nhat:";
cin >> i;
cout << "nhap phan tu thu hai:";
cin >> j;
cout<<"do chenh lech giua hai phan tu la:";
if(A[i-1]>=A[j-1])cout<<(A[i-1]-A[j-1]);
else cout<<(A[j-1]-A[i-1]);
}
void check(int A[], int n){
for(int i=0; i<n;i++){
cout<<"do lech trung binh so thu "<<i+1<<" la: ";
if((float)A[i]>=trungBinh(A,n)) cout<<((float)A[i]-trungBinh(A,n));
else cout<<(trungBinh(A,n)-(float)A[i]);
cout<<endl;
}
}
// tim kiem mot phan tu trong mang
void search(int A[], int n, int x){
for(int i=0; i<n; i++){
if(A[i]==x) cout<<i<<"\t";
}
}
int main() {
int n;
int A[10];
cout << "nhap gia tri n:";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "so thu " << i + 1 << " la: ";
cin >> A[i];
}
// 3 thuat toan sap xep
insertSort(A, n);
//selectSort(A,n);
//bubbleSort(A, n);
// phan tu lon nhat va vi tri
for (int i = 0; i < n; i++)
cout << "\t" << A[i];
cout << "gia tri lon nhat la: ";
cout << findMax(A, n);
cout << endl << "vi tri phan tu lon nhat trong mang la: ";
for (int i = 0; i < n; i++) {
if (A[i] == findMax(A, n))
cout << i;
}
// phan tu nho nhat va vi tri
cout << endl << "gia tri nho nhat la: ";
cout << findMin(A, n);
cout << endl << "vi tri phan tu nho nhat trong mang la: ";
for (int i = 0; i < n; i++) {
if (A[i] == findMin(A, n))
cout << i;
}
// gia tri trung binh
cout << endl << "gia tri trung binh cac phan tu trong mang la:";
cout << trungBinh(A, n);
cout<<endl;
//chenhLech(A,n);
check(A,n);
cout<<"nhap so ban can tim";
int k;
cin>>k;
cout<<"vi tri ban can tim la :";
search(A,n,k);
}