快排的partition算法解题

2016-09-19  本文已影响0人  AlwaysFrank
#include <iostream>

#define N 10
#define K 4
using namespace std;

//使用引用,完成两数交换
void Swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}
//快排的partition算法,这里的基准数是随机选取的
template <typename T>
int Partition(T* data,int length, int low, int high)
{
    if(low<high)
    {
        int key = data[low];
        int i = low;
        for(int j=low+1; j<=high; j++)
        {
            if(data[j]<=key)
            {
                i = i+1;
                Swap(data[i], data[j]);
            }
        }
        Swap(data[i],data[low]);
        return i;
    }
    else
    {
        return low;
    }

}
//最小的K个元素
template <typename T>
void GetLeastNumbers_by_partition(T* input, int n, T* output, int k)
{
    if(input == NULL || output == NULL || k > n || n <= 0 || k <= 0)
    {
        return;
    }
    int start = 0;
    int end = n - 1;
    int index = Partition(input, n, start, end);
    while(index != k - 1)
    {
        if(index > k - 1)
        {
            end = index - 1;
            index = Partition(input, n, start, end);
        }
        else
        {
            start = index + 1;
            index = Partition(input, n, start, end);
        }
    }
    for(int i = 0; i < k; ++i)
        output[i] = input[i];
}
//输出
template <typename T>
void displayArray(T &myArray)
{
    for (auto m :myArray)
    {
        cout<<m<<"  ";
    }
}

int main()
{
    int arrayInt[] = {4,5,1,6,2,7,3,8,1,2};
    int arrayIntResult[K];
    //输出初始化结果
    cout<<"原始数组"<<endl;
    displayArray(arrayInt);
    cout<<endl;
    //最小的4个元素
    GetLeastNumbers_by_partition(arrayInt,N,arrayIntResult,4);
    cout<<"最小的"<<K<<"个元素"<<endl;
    displayArray(arrayIntResult);
    cout<<endl;
    return 0;
}

#include <iostream>

using namespace std;

//使用引用,完成两数交换
void Swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}
//快排的partition算法,这里的基准数是随机选取的
template <typename T>
int Partition(T* data,int length, int low, int high)
{
    if(low<high)
    {
        int key = data[low];
        int i = low;
        for(int j=low+1; j<=high; j++)
        {
            if(data[j]<=key)
            {
                i = i+1;
                Swap(data[i], data[j]);
            }
        }
        Swap(data[i],data[low]);
        return i;
    }
    else
    {
        return low;
    }

}
bool g_bInputInvalid = false;
template <typename T>
bool CheckInvalidArray(T* numbers, int length)
{
    g_bInputInvalid = false;
    if(numbers == NULL && length <= 0)
        g_bInputInvalid = true;
    return g_bInputInvalid;
}
template <typename T>
bool CheckMoreThanHalf(T* numbers, int length, int number)
{
    int times = 0;
    for(int i = 0; i < length; ++i)
    {
        if(numbers[i] == number)
            times++;
    }
    bool isMoreThanHalf = true;
    if(times * 2 <= length)
    {
        g_bInputInvalid = true;
        isMoreThanHalf = false;
    }
    return isMoreThanHalf;
}
template <typename T>
int MoreThanHalfNum_Solution(T* numbers, int length)
{
    if(CheckInvalidArray(numbers, length))
        return 0;
    int middle = length >> 1;
    int start = 0;
    int end = length - 1;
    int index = Partition(numbers, length, start, end);
    while(index != middle)
    {
        if(index > middle)
        {
            end = index - 1;
            index = Partition(numbers, length, start, end);
        }
        else
        {
            start = index + 1;
            index = Partition(numbers, length, start, end);
        }
    }
    int result = numbers[middle];
    if(!CheckMoreThanHalf(numbers, length, result))
        result = 0;
    return result;
}
//输出
template <typename T>
void displayArray(T &myArray)
{
    for (auto m :myArray)
    {
        cout<<m<<"  ";
    }
}

int main()
{
    int arrayInt[] = {4,5,1,6,6,6,6,6,6,6,6,6,6,2,7,3,8,1,2};
    //输出初始化结果
    cout<<"原始数组"<<endl;
    displayArray(arrayInt);
    cout<<endl;

    int length = sizeof(arrayInt)/sizeof(int);

    //超过一半的数字
    cout<<"超过一半的数字: ";
    cout<<MoreThanHalfNum_Solution(arrayInt,length);
    cout<<endl;

    //另解   排序后在中间的数字,统计次数如果大于一半即为所求
    cout<<"超过一半的数字: ";
    cout<< arrayInt[length/2];
    cout<<endl;

    return 0;
}

#include <iostream>
using namespace std;
void Process( char *str )
{
    int i = 0;
    int j = 0;
    //移动指针i, 使其指向第一个大写字母
    while( str[i] != '\0' && str[i] >= 'a' && str[i] <= 'z' ) i++;
    if( str[i] != '\0' )
    {
    //指针j遍历未处理的部分,找到第一个小写字母
        for( j=i; str[j] != '\0'; j++ )
        {
            if( str[j] >= 'a' && str[j] <= 'z' )
            {
                char tmp = str[i];
                str[i] = str[j];
                str[j] = tmp;
                i++;
            }
        }
    }
}
int main()
{
    char data[] = "HelloWorld";
    cout<<"Before : "<<data<<endl;
    Process( data );
    cout<<"After : "<<data;
    return 0;
} 
上一篇下一篇

猜你喜欢

热点阅读