首页 > 代码库 > #旧代码# 算法练习:排序/字符串查找/链表反转

#旧代码# 算法练习:排序/字符串查找/链表反转

#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 = http://www.mamicode.com/numbers[i];"Not found!!" << endl;        return ;    }    for(;*pos !=‘\0‘; 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;}

  

#旧代码# 算法练习:排序/字符串查找/链表反转