常用算法的C++实现


//
//  DZAppDelegate.m
//  AlgorithmTest
//
//  Created by dzpqzb on 13-8-4.
//  Copyright (c) 2013年 dzpqzb inc. All rights reserved.
//

#import "DZAppDelegate.h"

#import <vector>
#import <iostream>
#import <fstream>
#import <ctime>

static char* const kDataFilePath = "data.txt";
const int MaxDataLength = 10;

using namespace std;

vector<int> sortData;

void randNum()
{
    ofstream fp(kDataFilePath);
    if (fp.fail()) {
        cout << "open data file error!" << endl;
        exit(EXIT_FAILURE);
    }
    srand((unsigned) time(NULL));
    for (int i = 0; i < MaxDataLength; ++i) {
        sortData.push_back(rand()%100);
    }
    fp.close();
}

void printVectorInt(vector<int> a )
{
    cout << endl;
    for (vector<int>::iterator itor= a.begin(); itor != a.end(); itor++) {
        cout << *itor << "\t";
    }
    cout << endl;
}

int partion(vector<int> &v, int low, int high)
{
    int pivotkey;
    pivotkey = v[low];
    while (low < high) {
        while (low < high && v[high] >= pivotkey) {
            high--;
        }
        v[low] = v[high];
        
        while (low < high && v[low] <= pivotkey) {
            low ++;
        }
        v[high] = v[low];
    }
    v[low] = pivotkey;
    return low;
}

void quickSort(vector<int> &v, int left, int right)
{
    if (left < right)
    {
        int i = partion(v, left, right);
        quickSort(v, left, i-1);
        quickSort(v, i+1, right);
    }
}


void mergeverctor(vector<int>& v, int start, int mid, int end)
{
    int j, k,l;
    vector<int> temp(v);
    
    for(j = mid+1, k = start; start <= mid && j <= end; k++)
    {
        if (temp[start] < temp[j]) {
            v[k] = temp[start++];
        }
        else
        {
            v[k] = temp[j++];
        }
    }
    if (start <= mid) {
        for (l=0; l < mid-start; ++l) {
            v[k+1] = temp[start+1];
        }
    }
    if (j <= end) {
        for (l=0; l < end - j; ++l) {
            v[k+1] = temp[j+1];
        }
    }
}

void mergeSort(vector<int>&v, int low, int high)
{
    if (low < high) {
        int mid =(low + high)/2;
        mergeSort(v, low, mid);
        mergeSort(v, mid+1, high);
        mergeverctor(v, low, mid, high);
    }
}
void (^MergeSort)(vector<int>&) = ^(vector<int>&v)
{
    mergeSort(v, 0, (int)v.size()-1);
};

void (^QuickSort)(vector<int>&) = ^(vector<int>& v)
{
    quickSort(v, 0, (int)(v.size()-1));
};

void (^InsertSort)(vector<int>&) = ^(vector<int>&v)
{
    int i , j ;
    int key;
    for(i =1; i < v.size(); ++i)
    {
        if(v[i] < v[i-1])
        {
            key = v[i];
            for(j = i-1; key < v[j] ; --j)
            {
                v[j+1] = v[j];
            }
            v[j+1] = key;
        }
    }
};
void (^ASort)(vector<int>&) = ^(vector<int>&v)
{
    
};

void (^SelectSort)(vector<int>&) = ^(vector<int>&v)
{
    int i, j , min;
    for(i = 0; i < v.size(); ++i)
    {
        min = i;
        for(j=i+1; j<v.size(); ++j)
        {
            if(v[j] < v[min]) min = j;
        }
        if(min != i)
        {
            swap(v[i], v[min]);
        }
    }
};

void (^BubbleSort)(vector<int>&) = ^(vector<int>&v)
{
    int i ,j;
    for( i = 0; i < v.size(); ++i)
    {
        for( j = i+1; j < v.size(); ++j)
        {
            if(v[i] > v[j])
            {
                swap(v[i], v[j]);
            }
        }
    }
};


void heapAdjust(vector<int>&v, int s, int m)
{
    int rc, j;
    rc = v[s];
    for (j = 2*s ; j<=m; j *=2) {
        if (j<m && v[j] < v[j+1]) {
            j++;
        }
        
        if (rc >= v[j])
        {
            break;
        }
        
        v[s] = v[j];
        s = j;
    }
    v[s] =rc;
}
void (^HeapSort)(vector<int>&) = ^(vector<int>&v)
{
    int i;
    for(i=(int)v.size()/2; i >0; i--)
    {
        heapAdjust(v, i, (int)v.size()-1);
    }
    for(i = (int)v.size() -1; i >0; i--)
    {
        int t = v[0];
        v[0] = v[i];
        v[i] = t;
    
        heapAdjust(v, 0, i-1);
    }
};


typedef void(^SortAlgorithm)(vector<int>&);
void (^TestSortTime)(SortAlgorithm algrotithm) = ^(SortAlgorithm algorithm)
{
    double start, end;
    start = clock();
    vector<int> a(sortData);
    cout << "Origin is:";
    printVectorInt(a);
    algorithm(a);
    end = clock();
    cout << "Result is :";
    printVectorInt(a);
    cout << "Time is:" << end - start << endl;
    cout <<"******************************************" <<endl;
};


@implementation DZAppDelegate


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    randNum();
    // Insert code here to initialize your application
    TestSortTime(QuickSort);
//
    TestSortTime(InsertSort);
//
    TestSortTime(BubbleSort);
//
    TestSortTime(SelectSort);
//
    TestSortTime(MergeSort);
    TestSortTime(HeapSort);
}

@end

 posted on 2013-08-04 12:06  文鬼  阅读(310)  评论(0编辑  收藏  举报