快排和归并排序(基本功)

1.快排

思路:先确定一个正确的元素位置,再以此将数组划分为二,递归调用;

void quicksort(int a[],int low,int high)
{
    if(low>=high)  return;
    int key = a[low];
    int first = low;
    int last = high;
    while(first=key)   --last;
        a[first] = a[last];
        while(first

2.归并排序

void Merge(int sourceArray[],int tempArray[],int s,int mid,int t)
{
    int i = s ;
    int j = mid+1;
    int k = s;
    
    while(i<=mid && j<=t)
    {
        if(sourceArray[i]<=sourceArray[j])  
        	tempArray[k++] =  sourceArray[i++];
        else 
        	tempArray[k++]  = sourceArray[j++];
    }
    while(i<=mid)  
    	tempArray[k++]  = sourceArray[i++];
    while(j<=t)    
    	tempArray[k++]  = sourceArray[j++];
    for(i=s;i<=t;i++)  
    	sourceArray[i] = tempArray[i];
}

void MergeSort(int sourceArray[],int tempArray[],int s,int t)
{
    if(s>1;
        MergeSort(sourceArray,tempArray,s,mid);
        MergeSort(sourceArray,tempArray,mid+1,t);
        Merge(sourceArray,tempArray,s,mid,t);
    }
}

你可能感兴趣的:(数据结构)