摘要: quicksort可以说是应用最广泛的排序算法之一,它的基本思想是分治法,选择一个pivot(中轴点),将小于pivot放在左边,将大于pivot放在右边,针对左右两个子序列重复此过程,直到序列为空或者只有一个元素。这篇blog主要目的是关注quicksort可能的改进方法,并对这些改进方法做评测。其目的是为了理解Arrays.sort(int [ ]a)的实现。实现本身有paper介绍。 quicksort一个教科书式的简单实现,采用左端点做pivot(《算法导论》上伪代码是以右端点做pivot):Code highlighting produced by Actipro CodeHigh. 阅读全文
posted @ 2012-12-09 14:59 Tiu.G 阅读(189) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>using namespace std;//元素交换函数void lvSwap(int *apiArray, int index1, int index2){ int temp = apiArray[index1]; apiArray[index1] = apiArray[index2]; apiArray[index2] = temp;}//快速排序void lvQuickSort(int *apiArray, int arraySize){ int liLast=0; int i; if(arraySize > 1){ lvSwap( 阅读全文
posted @ 2012-12-09 11:32 Tiu.G 阅读(274) 评论(0) 推荐(0) 编辑
摘要: 题目:将输入的字符串逆序存储,要求不能改变原字符串。例如:str="Hello world",str2="dlrow olleH".#include <iostream>#include <assert.h>#include <string>using namespace std;void StringReverse(const char* str, int len, char *str2){ assert(str); for(int i = len; i > 0; i--){ *(str2++) = *(str 阅读全文
posted @ 2012-12-09 11:24 Tiu.G 阅读(508) 评论(0) 推荐(0) 编辑
摘要: 请设计一个算法过滤掉字符串中的某个字母(可能多次出现),比如adecdefg过滤掉e后得到abcdfg。要求时间复杂度为O(n),空间复杂度为O(1)。char*StrPurify(char*src,chardelCh){ if(NULL==src)returnNULL; char*p1,*p2; p1=p2=src; while(*src){ if(*src!=delCh) *(p1++)=*(src++); else src++; } *p1='\0'; returnp2;}用一次循环及两个指针,符合时间复杂度O(n),空间复杂度O(1) 阅读全文
posted @ 2012-12-09 11:20 Tiu.G 阅读(138) 评论(0) 推荐(0) 编辑
摘要: 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的谦虚遍历和中序遍历的结果中都不含重复的数字。#include <iostream>#include <string>using namespace std;int find(const string &str, char c) //在字符串中查找字符的函数,参数是string类引用和字符{ for (int i = 0; i < str.size(); ++ i) if (c == str[i]) return i; //找到之后返回 return -1;}void PreMid(c 阅读全文
posted @ 2012-12-09 11:17 Tiu.G 阅读(197) 评论(0) 推荐(0) 编辑