快排的partition算法解題

  • 最小的k個(gè)數(shù),輸入n個(gè)整數(shù),找出其中最小的k個(gè)數(shù)
    可以建立大小為K的小頂堆。
    也可以運(yùn)用partition函數(shù)進(jìn)行求解,不過(guò)我們完整的快速排序分割后要遞歸地對(duì)前后兩段繼續(xù)進(jìn)行分割,而這里我們需要做的是判定分割的位置,然后再確定對(duì)前段還是后段進(jìn)行分割,所以只對(duì)單側(cè)分割即可。
#include <iostream>

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

//使用引用,完成兩數(shù)交換
void Swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}
//快排的partition算法,這里的基準(zhǔn)數(shù)是隨機(jī)選取的
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個(gè)元素
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];
    //輸出初始化結(jié)果
    cout<<"原始數(shù)組"<<endl;
    displayArray(arrayInt);
    cout<<endl;
    //最小的4個(gè)元素
    GetLeastNumbers_by_partition(arrayInt,N,arrayIntResult,4);
    cout<<"最小的"<<K<<"個(gè)元素"<<endl;
    displayArray(arrayIntResult);
    cout<<endl;
    return 0;
}

  • 判斷數(shù)組中出現(xiàn)超過(guò)一半的數(shù)字
#include <iostream>

using namespace std;

//使用引用,完成兩數(shù)交換
void Swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}
//快排的partition算法,這里的基準(zhǔn)數(shù)是隨機(jī)選取的
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};
    //輸出初始化結(jié)果
    cout<<"原始數(shù)組"<<endl;
    displayArray(arrayInt);
    cout<<endl;

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

    //超過(guò)一半的數(shù)字
    cout<<"超過(guò)一半的數(shù)字: ";
    cout<<MoreThanHalfNum_Solution(arrayInt,length);
    cout<<endl;

    //另解   排序后在中間的數(shù)字,統(tǒng)計(jì)次數(shù)如果大于一半即為所求
    cout<<"超過(guò)一半的數(shù)字: ";
    cout<< arrayInt[length/2];
    cout<<endl;

    return 0;
}

  • 有一個(gè)由大小寫(xiě)組成的字符串,現(xiàn)在需要對(duì)他進(jìn)行修改,將其中的所有小寫(xiě)字母排在大寫(xiě)字母的前面(不要求保持原順序)
    若要求保持原序列,可用冒泡的思想來(lái)求解!??!
#include <iostream>
using namespace std;
void Process( char *str )
{
    int i = 0;
    int j = 0;
    //移動(dòng)指針i, 使其指向第一個(gè)大寫(xiě)字母
    while( str[i] != '\0' && str[i] >= 'a' && str[i] <= 'z' ) i++;
    if( str[i] != '\0' )
    {
    //指針j遍歷未處理的部分,找到第一個(gè)小寫(xiě)字母
        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;
} 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容