摘要: 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) 编辑
摘要: 设计程序的一个良好习惯是首先将程序所涉及的操作列出来。明确需要提供的操作有助于建立需要的数据结构和实现这些行为。本例有如下需求:1.它必须允许用户指明处理的文件名字。程序将存储该文件的内容,以便输出每个单词所在的原始行。2.它必须将每一行分解为各个单词,并记录每个单词所在的所有行。字输出行号时,应保证以升序输出,并且不重复。3.对特定单词的查询将返回出现该单词的所有行的行号。4.输出某单词所在的行文本时,程序必须能根据给定的行号从输入的文件中获取相应的行。数据结构TestQuery类1.使用一个vector<string>类型的对象存储整个输入文件的副本。输入文件的每一行是该vec 阅读全文
posted @ 2012-11-30 19:47 Tiu.G 阅读(321) 评论(1) 推荐(0) 编辑
摘要: 现在有一个整数,对其进行逆序存储。例如,3 (0000 0000 0000 0000 0000 0000 0000 0011)2 3221225472 (1100 0000 0000 0000 0000 0000 0000 0000)2/*整数逆序存储*/#include <stdio.h>unsigned int reverse(unsigned int a){unsigned int b=0;for(int i=0;i<31;i++){if(a & 0x01){b = b | 0x01;}b = b<<1;a = a>>1;}return 阅读全文
posted @ 2012-11-27 23:58 Tiu.G 阅读(365) 评论(0) 推荐(0) 编辑
摘要: /*****************************************************//*假设有一个没有头指针的单链表。一个指针指向此单链表**//*中间的一个节点,请将该节点从单链表中删除************//*****************************************************/#include <stdlib.h>#include <stdio.h>#include <assert.h>typedef struct Node{ char value; struct Node *next; 阅读全文
posted @ 2012-08-15 16:51 Tiu.G 阅读(292) 评论(0) 推荐(0) 编辑
摘要: /*********************字符串移位包含的问题********************//*给定两个字符串s1和s2,要求判定s2是否能够被s1做循环移位得到的*//*字符串包含。例如,给定s1=AABCD和s2=CDAA,返回true;给定s1= *//*ABCD和s2=ACBD,返回false。 *//*************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h># 阅读全文
posted @ 2012-08-13 16:40 Tiu.G 阅读(202) 评论(0) 推荐(0) 编辑
摘要: vsprintf /* 函数名: vsprintf 功 能: 送格式化输出到串中 返回值: 正常情况下返回生成字串的长度(除去\0),错误情况返回负值 用 法: int vsprintf(char *string, char *format, va_list param); // 将param 按格式format写入字符串string中 注: 该函数会出现内存溢出情况,建议使用vsnprintf 程序例: */ #include <stdarg.h> char buffer[80]; int vspf(char *fmt, ...) { va_list argptr;... 阅读全文
posted @ 2012-07-26 15:35 Tiu.G 阅读(573) 评论(0) 推荐(0) 编辑