冒泡排序:
for(int i = 0; i < (length - 1); i++)
{
for(int j = 0; j < (length -1 -i); j++)
{
if(score[j] < score[j+1])
{
temp = score[j];
score[j] = score[j+1];
score[j+1] = temp;
}
}
}
快速排序:
void quick_sort(int a[], int low, int high)
{
int i, j, pivot,n;
pivot = a[low];
i = low;
j = high;
if (low < high)
{
while (i < j)
{
while (i < j && a[j] >= pivot)
j--;
if (i < j)
{
a[i++] = a[j];
for (n = 0; n<10; n++)
{
printf("%d ", a[n]);
}
printf("\n");
}
while (i < j && a[i] <= pivot)
i++;
if (i < j)
{
a[j--] = a[i];
for (n = 0; n<10; n++)
{
printf("%d ", a[n]);
}
printf("\n");
}
}
a[i] = pivot;
quick_sort(a, low, i - 1);
quick_sort(a, i + 1, high);
}
}
二分查找:
条件:针对有序数组(元素从小到大或从大到小)
优点:查询速度较快,时间复杂度为O(n)
缺点:有硬性条件的限制,而且即使查到后,插入与删除困难。
int binary_research(int a[], int left, int right, int element)
{
while(left <= right)
{
int mid = (left + right) /2;
if(a[mid] > element)
{
right = mid -1;
}
else if(a[mid] < element)
{
left = mid +1;
}
else
{
return mid;
}
}
return -1;
}