标签:-- select char hal 递归 通过 key har \n
冒泡排序——————————————————————————————
void swap(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void bubSort(int *p, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 1; j < n-i; j++)
{
if (p[j - 1] > p[j])
{
swap(&p[j - 1], &p[j]);
}
}
}
}
void print(int *p, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d\n", p[i]);
}
}
void main01()
{
int arr[] = { 1, 2, 5, 25, 19, 23, 12, 11, 16 };
bubSort( arr , sizeof(arr) / sizeof(int));
print(arr, sizeof(arr) / sizeof(int));
getchar();
}
选择排序——————————————————————————————————————
void print(int *p, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d\n", p[i]);
}
}
void swap(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void SelectSort(int *p, int n)
{
int temp;
int min;
for (int i = 0; i < n; i++)
{
temp = p[i];
min = i;
for (int j =i+1 ; j < n; j++)
{
if (temp<p[j])
{
temp = p[j];
min = j;
}
}
if (min != i)
{
swap(&p[min], &p[i]);
}
}
}
void main()
{
int arr[] = { 12, 10, 39, 73, 6, 23, 2,13,123,45,25 };
SelectSort(arr, sizeof(arr) / sizeof(int));
print(arr, sizeof(arr) / sizeof(int));
getchar();
}
二分查找——————————————————————————————————---
void swap(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
//二分查找
void halfFind(int *p, int low, int hight, int key)
{
int mid = 0;
int temp;
int min;
for (int i = 0; i < hight -low; i++)
{
min = i;
temp = p[i];
for (int j = i +1; j < hight - low; j++)
{
if (temp > p[j])
{
temp = p[j];
min = j;
}
}
if (min != i)
{
swap(&p[min], &p[i]);
}
}
while (low <= hight)
{
mid = (low + hight) / 2;
if (p[mid] == key)
{
printf("%d", mid);
return;
}
else if (key > p[mid])
{
low += 1;
}
else
{
hight -= 1;
}
}
}
//通过递归实现二分查找————————————————————————————————————————
void binFind(int *p, int low, int hight, int key)
{
int mid = (low + hight) / 2;
if (low <= hight)
{
if (p[mid] == key)
{
printf("%d", mid);
return;
}
else if (p[mid] > key)
{
binFind(p, low + 1, hight, key);
}
else
{
binFind(p, low, hight - 1, key);
}
}
return;
}
排序算法
标签:-- select char hal 递归 通过 key har \n
原文地址:https://www.cnblogs.com/jefy/p/9388015.html