Algorithms: Design and Analysis, Part 1 【program 2/统计快排比较次数】

#include<string>
#include <vector>
#include <fstream>

using namespace std;

std::vector<int> v;
int bigArr[10000];
int helpArr[10000];

int partition( int* arr, int left, int right );

int QSort( int* arr, int left, int right )
{
    if( left >= right )
    {
        return 0;
    }

    int partionIdx = partition( arr, left, right );

    int leftCmpNum = QSort( arr,left, partionIdx - 1 );
    int rightCmpNum = QSort( arr, partionIdx + 1, right );

    return ( right - left ) + leftCmpNum + rightCmpNum;
}

int partition( int* arr, int left, int right )
{
    if( left >= right )
    {
        return left;
    }

    int key = arr[left];

    int lastLessIdx = left;
    int idx = left + 1;

    while( idx <= right )
    {
        if( arr[idx] < key )
        {
            int tmp = arr[idx];

            lastLessIdx++;
            arr[idx] = arr[lastLessIdx];
            arr[lastLessIdx] = tmp;
        }

        idx++;
    }

    int tmp = arr[lastLessIdx];
    arr[lastLessIdx] = arr[left];
    arr[left] = tmp;

    return lastLessIdx;
}

void main()
{
    fstream infile( "QuickSort.txt" );
    string tmp;
    while( getline( infile, tmp ) )
    {
        int num = atoi( tmp.c_str() );
        v.push_back( num );
    }

    for( int i = 0; i < 10000; i++ )
    {
        bigArr[i] = v.at(i);
    }

    int cmpNum = QSort( bigArr, 0, 9999 );
}
时间: 2024-11-09 01:21:53

Algorithms: Design and Analysis, Part 1 【program 2/统计快排比较次数】的相关文章

Algorithms: Design and Analysis, Part 1 【program 1/逆序数】

#include<string> #include <vector> #include <fstream> using namespace std; std::vector<int> v; int bigArr[100000]; int helpArr[100000]; _int64 MergeAndCount( int* arr, int left, int mid, int right ) { if( left >= right ) { retur

Algorithms: Design and Analysis, Part 1 - Programming Assignment #1

自我总结: 1.编程的思维不够,虽然分析有哪些需要的函数,但是不能比较好的汇总整合 2.写代码能力,容易挫败感,经常有bug,很烦心,耐心不够好 题目: In this programming assignment you will implement one or more of the integer multiplication algorithms described in lecture. To get the most out of this assignment, your pro

[Stanford Algorithms: Design and Analysis, Part 2]

Specific topics in Part 2 include: greedy algorithms (scheduling, minimum spanning trees, clustering, Huffman codes), dynamic programming (knapsack, sequence alignment, optimal search trees, shortest paths), NP-completeness and what it means for the

Algorithms: Design and Analysis Note

Week2 Master method Assumption:all subproblems has same size Recurrence Format The Master Method formula

[Stanford Algorithms: Design and Analysis, Part 2] c25 HUFFMAN CODES

原文地址:https://www.cnblogs.com/ecoflex/p/10577522.html

[Stanford Algorithms: Design and Analysis, Part 2] c27 The Knapsack Problem

原文地址:https://www.cnblogs.com/ecoflex/p/10640959.html

[Stanford Algorithms: Design and Analysis, Part 2] c28 Sequence Alignment Optimal Substructure

原文地址:https://www.cnblogs.com/ecoflex/p/10658398.html

Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency_Pseudocode

This pseudocode from the book: <<Introduction to the Design and Analysis of Algorithms_Second Edition>> _ Anany LevitinNote that throughout the paper, we assume that inputs to algorithms fall within their specified ranges and hence require no

Design and Analysis of Algorithms_Brute Froce_Pseudocode

This pseudocode from the book: <<Introduction to the Design and Analysis of Algorithms_Second Edition>> _ Anany LevitinNote that throughout the paper, we assume that inputs to algorithms fall within their specified ranges and hence require no