De On Tap C TH HDHDD
De On Tap C TH HDHDD
De On Tap C TH HDHDD
#include<stdio.h>
#include<conio.h>
#include<math.h>
int binarysearch(int a[], int n, int x)
{
int left = 0;
int right = n-1;
int mid;
while (left <= right)
{
mid = (left + right) / 2;
if (x == a[mid]) return mid;
else
if (x < a[mid]) right = mid - 1;
else left = mid + 1;
}
return -1;
}
void nhapmang(int a[], int n)
{
int i ;
for (i = 0; i < n; i++)
{
printf("a[%d]=",i);
scanf_s("%d", &a[i]);
}
}
void xuatmang(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("a[%d]=%d ", i,a[i]);
}
}
void main()
{
int n;
int a[100];
printf("nhap so phan tu n=");
scanf_s("%d", &n);
nhapmang(a, n);
xuatmang(a, n);
int pos=0;
int x;
printf("nhap gia tri can tim kiem x=");
scanf_s("%d", &x);
pos = binarysearch(a, n, x);
printf("vi tri so can tim la: %d", pos);
_getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
int linearsearch(int x, int N, int a[])
{
int i = 0;
a[N] = x;
while (a[i] != x)
i++;
if (i == N) return -1;
else return i;
}
void nhapmang(int a[], int N)
{
int i;
for (i = 0; i < N; i++)
{
printf("nhap a[%d]:",i);
scanf_s("%d",&a[i]);
}
}
void xuatmang(int a[], int N)
{
int i;
for (i = 0; i < N; i++)
{
printf("a[%d]=%d ", i, a[i]);
}
}
void main()
{
int n;
int a[100];
int x;
printf("nhap so phan tu n: ");
scanf_s("%d", &n);
nhapmang(a, n);
xuatmang(a, n);
printf("\n nhap gia tri can tim kiem x= ");
scanf_s("%d", &x);
int pos = linearsearch(x, n, a);
printf(" vi tri so can tim la: %d", pos);
_getch();
}
temp = a;
a = b;
b = temp;
}
void selectionsort(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])
min = j;
hoanvi(a[min], a[i]);
}
}
void main()
{
int n;
int a[100];
printf("nhap so phan tu n: ");
scanf_s("%d", &n);
nhapmang(a, n);
selectionsort(a, n);
printf("mang sau khi sap xep la :");
xuatmang(a, n);
_getch();
}
}
a[pos + 1] = x;
}
}
void nhapmang(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("a[%d] = ", i);
scanf_s("%d", &a[i]);
}
}
void xuatmang(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("a[%d] = %d ", i, a[i]);
}
}
void main()
{
int n;
int a[100];
printf("nhap so phan tu n :");
scanf_s("%d", &n);
nhapmang(a, n);
insertionsort(a, n);
printf(" mang sau khi sap xep la: ");
xuatmang(a, n);
_getch();
}
}
}
}
void main()
{
int n;
int a[100];
printf("nhap so phan tu cua n :");
scanf_s("%d", &n);
nhapmang(a, n);
inserchangesort(a, n);
printf("mang sau khi sap xep la: ");
xuatmang(a, n);
_getch();
}
//// phương pháp nổi bọt///////////
#include<stdio.h>
#include<conio.h>
#include<math.h>
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void bubblesort(int a[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = n-1; j > i; j--)
{
if (a[j] < a[j - 1])
swap(a[j], a[j - 1]);
}
}
}
void main()
{
int n;
int a[100];
printf("nhap so pha tu cua mang n=");
scanf_s("%d", &n);
nhapmang(a, n);
bubblesort(a, n);
printf(" mang sau khi sap xep la : ");
xuatmang(a, n);
_getch();
}
shellsort_ sắp xếp với độ dài bước giảm dần ( code
trong sách)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void nhapmang(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("a[%d]= ", i);
scanf_s("%d", &a[i]);
}
}
void xuatmang(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("a[%d]= %d ", i, a[i]);
}
}
void shellsort(int a[], int n, int h[], int k)
{
int step, i, j;
int x, len;
for (step = 0; step < k; step++)
{
len = h[step];
for (i = len; i < n; i++)
{
x = a[i];
j = i - len;
while ((x < a[j]) && (j >= 0))
{
a[j + len] = a[j];
j = j - len;
}
a[j + len] = x;
}
}
}
void main()
{
int n;
int a[100];
printf("nhap so phan tu cua mang n:");
scanf_s("%d", &n);
nhapmang(a, n);
shellsort(a, n);
printf("mang sau khi sap xep la : ");
xuatmang(a, n);
}
sắp xêp với độ dài bước giảm dần shellsort (code hạnh)
/*
* C Program to sort an array using Shell Sort technique
*/
#include <stdio.h>
#include<conio.h>
#include<math.h>
void shellsort(int arr[], int num)
{
int i, j, k, tmp;
for (i = num / 2; i > 0; i = i / 2)
{
for (j = i; j < num; j++)
{
for (k = j - i; k >= 0; k = k - i)
{
if (arr[k + i] >= arr[k])
break;
else
{
tmp = arr[k];
arr[k] = arr[k + i];
arr[k + i] = tmp;
}
}
}
}
}
int main()
{
int arr[30];
int k, num;
printf("Nhap n: ");
scanf_s("%d", &num);
printf("Nhap mang: \n");
}
void heapsort(int a[], int n) {
int i, t;
heapify(a, n);
for (i = n - 1; i>0; i--) {
t = a[0];
a[0] = a[i];
a[i] = t;
adjust(a, i);
}
}
void heapify(int a[], int n) {
int k, i, j, item;
for (k = 1; k<n; k++) {
item = a[k];
i = k;
j = (i - 1) / 2;
while ((i>0) && (item>a[j])) {
a[i] = a[j];
i = j;
j = (i - 1) / 2;
}
a[i] = item;
}
}
void adjust(int a[], int n) {
int i, j, item;
j = 0;
item = a[j];
i = 2 * j + 1;
while (i <= n - 1) {
if (i + 1 <= n - 1)
if (a[i] <a[i + 1])
i++;
if (item<a[i]) {
a[j] = a[i];
j = i;
i = 2 * j + 1;
}
else
break;
}
a[j] = item;
}
int main()
{
int a[100], i, l, r;
printf("Nhap n: ");
scanf_s("%d", &n);
printf("Nhap mang: \n");
for (i = 0; i<n; i++)
scanf_s("%d", &a[i]);
l = 0;
r = n - 1;
qsort(a, l, r);
printf("Mang sau khi duoc sap xep: ");
for (i = 0; i<n; i++)
printf("%d ", a[i]);
_getch();
return 0;
}
///// sắp xếp theo phương pháp trộn trực tiếp _mergesort/////
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#include<math.h>
void merge_sort(int x[], int MAX_ARY, int end, int start) {
int j = 0;
const int size = start - end + 1;
int mid = 0;
int mrg1 = 0;
int mrg2 = 0;
int executing[100];
if (end == start)
return;
mid = (end + start) / 2;
merge_sort(x, MAX_ARY, end, mid);
merge_sort(x, MAX_ARY, mid + 1, start);
for (j = 0; j < size; j++)
executing[j] = x[end + j];
mrg1 = 0;
mrg2 = mid - end + 1;
for (j = 0; j < size; j++) {
if (mrg2 <= start - end)
if (mrg1 <= mid - end)
if (executing[mrg1] > executing[mrg2])
x[j + end] = executing[mrg2++]; else
x[j + end] = executing[mrg1++]; else
x[j + end] = executing[mrg2++]; else
x[j + end] = executing[mrg1++];
}
}
int main(void) {
int MAX_ARY;
int n;
int ary[100];
printf("Nhap n: ");
scanf_s("%d", &n);
MAX_ARY = n;
int j = 0;
printf("Nhap mang: ");
for (j = 0; j<MAX_ARY; j++)
scanf_s("%d", &ary[j]);
merge_sort(ary, MAX_ARY, 0, MAX_ARY - 1);
printf("Mang sau khi duoc sap xep: ");
for (j = 0; j < n; j++)
printf("%d ", ary[j]);
_getch();
}
int main()
{
int no;
printf("Nhap so dia: ");
scanf_s("%d", &no);
if (no<1)
printf("\nKhong co dia nao.");
else
printf("Cach chuyen: ");
hanoiNonRecursion(no, 'A', 'B', 'C');
_getch();
return 0;
}
/////// dùng phương pháp đệ quy để tính thap ha noi////
#include<stdio.h>
#include<conio.h>
#include<math.h>
void thaphanoi(int n, char a, char b, char c)
{
if (n == 1)
{
printf("\t%c-----------%c\n", a, c);
return;
}
thaphanoi(n - 1, a, c, b);
thaphanoi(1, a, b, c);
thaphanoi(n - 1, b, a, c);
}
int main() {
char a = 'A', b = 'B', c = 'C';
int n;
printf("nhap n:");
scanf_s("%d", &n);
thaphanoi(n, a, b, c);
_getch();