• #旧代码# 基本算法练习:常见排序/KMP字符串查找/链表反转


    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <cassert>
    using namespace std;
    
    void print(int numbers[], int size)
    {
        for (int i = 0; i < size; i++)
        {
            cout << numbers[i] << " ";
        }
    
        cout << endl;
    }
    
    void swap(int numbers[], int lhs, int rhs)
    {
        if (lhs == rhs) return;
    
        int temp = numbers[lhs];
        numbers[lhs] = numbers[rhs];
        numbers[rhs] = temp;
    }
    
    void quick_sort(int numbers[], int low, int high)
    {
        if (low >= high) return;
    
        const int init_low = low;
        const int init_high = high;
    
        while(low < high)
        {
            while (low < high && numbers[high] > numbers[init_low]) { high --; }
            while (low < high && numbers[low] <= numbers[init_low]  ) { low ++; }
            swap(numbers, low, high);
        }
    
        swap(numbers, init_low, high);    
        quick_sort(numbers, init_low, high - 1);
        quick_sort(numbers, high + 1, init_high);
    
        return;
    }
    
    void bubble_sort(int numbers[], int size)
    {
        for (int i = 0; i < size; i ++)
        {
            for (int j = i + 1; j < size; j ++)
            {
                if (numbers[j] < numbers[i])
                {
                    swap(numbers, i, j);
                }
            }
        }
    }
    
    void insert_sort(int numbers[], int size)
    {
        for (int i = 1; i < size; i ++)
        {
            int new_value = numbers[i];
    
            int j = i;
            for (; j > 0 && numbers[j - 1] > new_value; j --)
            {
                numbers[j] = numbers[j - 1];
            }
    
            numbers[j] = new_value;
    
        }
    }
    
    void merge(int numbers[], int first, int mid, int last)
    {
        int *buffer = new int[last - first + 1];
        assert(buffer);
    
        int lhs_first = first;
        int lhs_last = mid;
    
        int rhs_first = mid + 1;
        int rhs_last = last;
    
        int index = 0;
    
        while ((lhs_first <= lhs_last) && (rhs_first <= rhs_last))
        {
            if (numbers[lhs_first] <= numbers[rhs_first])
            {
                buffer[index] = numbers[lhs_first];
                lhs_first ++;
            }
            else
            {
                buffer[index] = numbers[rhs_first];
                rhs_first ++;
            }
    
            index ++;
        }
    
        while (lhs_first <= lhs_last)
        {
            buffer[index] = numbers[lhs_first];
            lhs_first ++;
            index ++;
        }
    
        while (rhs_first <= rhs_last)
        {
            buffer[index] = numbers[rhs_first];
            rhs_first ++;
            index ++;
        }
    
        for (int i = 0; i < index; i ++)
        {
            numbers[first] = buffer[i];
            first ++;
        }
    
    
        delete [] buffer;
    }
    
    void merge_sort(int numbers[], int first, int last)
    {
        if (first < last)
        {
            int mid = (last + first)/2;
    
            merge_sort(numbers, first, mid);
            merge_sort(numbers, mid + 1, last);
            merge(numbers, first, mid, last);
        }
    }
    
    
    const char * normal_find(const char *source, int source_size, const char *target, int target_size)
    {
        int last_cmp_pos = source_size - target_size + 1;
        for (int i = 0; i < last_cmp_pos; i++)
        {
            int k = i;
            int j = 0;
    
            for (; (j < target_size) && (k < source_size); j ++, k ++)
            {
                if (source[k] != target[j])
                {
                    break;
                }
            }
    
            if (j == target_size)
            {
                return &(source[i]);
            }
    
            if (k == source_size)
            {
                return NULL;
            }
        }
    
        return NULL;
    }
    
    const char * kmp_find(const char *source, int source_size, 
                          const char *target, int target_size,
                          const int *next)
    {
        assert(source != NULL);
        assert(source_size > 0);
        assert(target != NULL);
        assert(target_size > 0);
        assert(next != NULL);      
    
    
        int i = 0;
        int j = 0;
    
        for (; (j < target_size) && (i < source_size); )
        {
            if (j == -1 || source[i] == target[j])
            {
                j ++;
                i ++;
            }
            else
            {
                j = next[j];
            }
    
        }
    
        if (j == target_size)
        {
            return &(source[i - target_size]);
        }
        else
        {
            return NULL;
        }
    }
    
    void get_next(const char * target, const int target_size, int * next)
    {
        assert(target != NULL);
        assert(target_size > 0);
        assert(next != NULL);
    
        int i = 0;
        int j = -1;
    
        next[0] = -1;
    
        for (; i < target_size; )
        {
            if (j == -1 || target[i] == target[j])
            {
                i ++;
                j ++;
    
                if (target[i] != target[j])
                {
                    next[i] = j;
                }
                else
                {
                    next[i] = next[j];
                }
            }
            else
            {
                j = next[j];
            }
        }
    }
    
    void print_sub_str(const char * pos)
    {
        if ( pos == NULL) 
        {
            cout << "Not found!!" << endl;
            return ;
        }
    
        for(;*pos !=''; pos ++)
        {
            cout << *pos << " ";
        }
    
        cout << endl;
    }
    
    
    struct Node
    {
        int value;
        Node * next;
    };
    
    Node * reverse(Node * node)
    {
        Node * nextNode = node->next;
    
        if (nextNode == NULL) 
        {
            return node;
        }
    
        Node * headerNode = reverse(nextNode);
        
        nextNode->next = node;
        node->next = NULL;
    
        return headerNode;
    }
    
    void print_list(Node * list)
    {
        while (list != NULL)
        {
            cout << list->value << " " ;
            list = list->next;
        }
    
        cout << endl;
    }
    
    
    class Other : public Base
    {
    };
    int _tmain(int argc, _TCHAR* argv[])
    {
        // sort algorithm
        /*
        int numbers[] = {9, 8, 7, 6, 5, 8, 4, 3, 2, 1, 4, 2, 3, 4, 7, 8};
        int size =  sizeof(numbers)/sizeof(int);
    
        //quick_sort(numbers, 0, size - 1);
        //bubble_sort(numbers, size);
        //insert_sort(numbers, size);
        //merge_sort(numbers, 0, size - 1);
    
        print(numbers, size);
        */
    
        // find algorithm
        /*
        const char * source = "aaaaabababcaaa";
        const char * target = "ababc";
    
        int source_size = strlen(source);
        int target_size = strlen(target);
    
        const char * const pos = normal_find(source, source_size, target, target_size);
        print_sub_str(pos);
    
        int * next = new int[target_size];
    
        if (next != NULL)
        {
            get_next(target, target_size, next);
            kmp_find(source, source_size, target, target_size, next);
            print_sub_str(pos);
        }
    
        delete []next;
        */
    
        // list operation
        /*
        Node n1 = {1, NULL};
        Node n2 = {2, &n1};
        Node n3 = {3, &n2};
        Node n4 = {4, &n3};
        Node n5 = {5, &n4};
    
        Node * list = &n5;
    
        print_list(list);
    
        list = reverse(list);
    
        print_list(list);
        */
    
    	return 0;
    }
    

      

  • 相关阅读:
    NSCharacterSet 最经常使用的使用方法
    IOS
    hdu 3117 Fibonacci Numbers
    Hibernate5配置与使用具体解释
    SDNU 1206.蚂蚁感冒 【代码如此简单,思维练习】【7月29】
    2048游戏分析、讨论与扩展
    hash_set和hash_map
    实现邮箱找回的思路分析
    学习OpenCV——粒子滤波(网上两篇文章总结)
    学习OpenCV——配置CUDA环境
  • 原文地址:https://www.cnblogs.com/maizhongfei/p/3945484.html
Copyright © 2020-2023  润新知