This C++ program defines several functions for searching and sorting arrays: LinearSearch to perform linear search on an array, BinarySearchTang for binary search, InterchangeSortTang and SelectionSortTang for direct selection and interchange sorts, QuickSortTang for quicksort, and DemNghichThe to count inversions in an array. It also contains functions to input/output arrays and test calling the sorting and searching functions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
26 views5 pages
CTDL
This C++ program defines several functions for searching and sorting arrays: LinearSearch to perform linear search on an array, BinarySearchTang for binary search, InterchangeSortTang and SelectionSortTang for direct selection and interchange sorts, QuickSortTang for quicksort, and DemNghichThe to count inversions in an array. It also contains functions to input/output arrays and test calling the sorting and searching functions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5
#include<iostream>
using namespace std;
int LinearSearch( int a[], int n, int x) { for ( int i = 0; i < n; i++) { if ( a[i] == x) return i; } return -1; } int BinarySearchTang(int a[], int n, int x) { int l = 0, r = n - 1, m; while (l <= r) { m = (l + r) / 2; if (x == a[m]) return m; else if (x < a[m]) r = m - 1; //Giam => l=m+1 else l = m + 1; // r=-1 } return -1; } void InterchangeSortTang(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]) // Giam a[i] < a[j] swap(a[i], a[j]); } } void SelectionSortTang(int a[], int n) { int min; for (int i = 0; i < n - 1; i++) { min = i; for (int j = i + 1; j < n; j++) if (a[j] < a[min]) // Giam a[j] > a[min]; min = j; swap(a[min], a[i]); } } void QuickSortTang(int a[], int l, int r) { int i, j, x; x = a[(l + r)/2]; i = l , j = r; do { while (a[i] < x) i++; while (a[j] > x) j--; if (i <= j) { swap(a[i], a[j]); i++; j--; } } while (i <= j); if (l < j) QuickSortTang(a, l, j); if (i < r) QuickSortTang(a, i, r); } int DemNghichThe(int a[], int n) { int dem = 0; //for (int j = 1; j < n ; j++) //for (int i = 0; i < j; i++) for (int i = 0; i < n-1; i++) for (int j = i+1; j <n; j++) { if (a[i] > a[j]) dem++; cout << "(" << a[i] << "," << a[j] << ") "; } return dem; } int Timvitrimin(int a[], int n) { int vt = 0; for (int i = 0; i < n - 1; i++) { if (a[i] > a[vt]) vt = i; }return vt; } void input(int a[], int &n) { cout << "Nhap so luong mang: "; cin >> n; for (int i = 0; i < n; i++) { cout<<"a["<<i<<"] = "; cin >> a[i]; } } void output(int a[], int n) { for (int i = 0; i < n; i++) { cout<< a[i]<<" "; } } void main() { int a[100],n; input(a, n); cout << "Mang vua nhap la: a[" << n << "] = "; output(a, n); cout << endl; cout << "Cac cap nghich the trong mang la: "; int kq = DemNghichThe(a, n); cout << endl; cout<<"So cap nghich the: "<<kq; cout << endl; cout << "Sap xep doi cho truc tiep: "; InterchangeSortTang(a, n); output(a, n); cout << endl; cout << "Sap xep chon truc tiep: "; SelectionSortTang(a, n); output(a, n); cout << endl; cout << "Sap xep phan hoach: "; QuickSortTang(a, 0, n - 1); output(a, n); cout << endl; int x; cout << "Nhap gia tri x can tim: "; cin >> x; int tknp = BinarySearchTang(a, n, x); if (tknp == -1) { cout<<"Khong tim thay"<<endl; } else { cout<<"Tim thay x tai vi tri a =["<< tknp<<"]"<<endl; } system("pause"); }