void Sort_select(int a[], int n)
{
int i, j, max_i, temp;
for (i = 0; i < n - 1; i++)
{
max_i = i;
for (j = i + 1; j < n; j++)
{
if (a[j] > a[max_i])
max_i = j;
if (max_i != i)
{
temp = a[max_i];
a[max_i] = a[i];
a[i] = temp;
}
}
}
}
void Buble_select(int a[], int n)
{
int i, j, temp;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
}
}
}
}
int BinSearch_(int a[], int n, int x)
{
int left = 0, right = n - 1, mid, find_i = -1;
while (left <= right && find_i == -1)
{
mid = (left + right) / 2;
if (x > a[mid])
left = mid + 1;
else if (x < a[mid])
right = mid - 1;
else
find_i = mid;
}
return find_i;//返回-1则无法找到
}
排序的指针实现:
void sort(char* strings, int num)
{
char* temp;
int top, seek;
for (top = 0; top < num - 1; top++)
for (seek = top + 1; seek < num; seek++)
if (strings[top] > strings[seek])//当第一个参数大于第二个参数时,函数strcmp返回一个负数
{ //当后面一个参数大于第一个参数时,函数返回一个正数,届时调换位置
temp = strings[top];
strings[top] = strings[seek];
strings[seek] = temp;
}
}