快速排序的自底向上
Merger Sort
void mergearray(int numbers[],int first,int mid,int last,int temp[])
{
int start = first;
int end = mid + 1;
int middle = mid;
int index = 0;
int i = 0;
while(start <= middle && end <= last)
{
if(numbers[start] < numbers[end])
{
temp[index++] = numbers[start++];
}
else
{
temp[index++] = numbers[end++];
}
}
while(start <= middle)
{
temp[index++] = numbers[start++];
}
while(end <= last)
{
temp[index++] = numbers[end++];
}
for(i = 0; i < index ; i++)
{
numbers[i] = temp[i];
}
}
/* merge sort */
int merge_sort(int numbers[] , int first ,int last,int temp[])
{
if(first < last)
{
int mid = (last + first) / 2;
/* left of array is sort */
merge_sort(numbers,first,mid,temp);
/* right of array is sort */
merge_sort(numbers,mid + 1,last,temp);
/* merge the left and rigth of array */
mergearray(numbers,first,mid,last,temp);
}
}
网友评论