Cracking The Coding Interview 9.0
#include <iostream>
#include <vector>
using namespace std; void mswap(int &a, int &b)
{
int c = a;
a = b;
b = c;
} void print(int *a, int size)
{
for (int i = 0; i<size ; i++)
{
cout<<a[i]<<" ";
}
}
//Start at the beginning of an array and swap the first two elements if the first is bigger than
// the second Go to the next pair, etc, continuously making sweeps of the array until sorted
// O(n^2)
void bubbleSort(int *a, int size)
{
if (a == NULL || size<0)
{
return;
}
for (int j = 0; j <size ; j ++)
{
for (int i = 0; i< size-1-j; i++)
{
if (a[i] > a[i+1])
{
mswap(a[i], a[i+1]);
}
}
} } //Find the smallest element using a linear scan and move it to the front Then, find the second
// smallest and move it, again doing a linear scan Continue doing this until all the elements
// are in place O(n^2)
void selectSort(int *a, int size)
{
if (a == NULL || size<0)
{
return;
}
for (int i = 0; i<size ;i ++)
{
int k = i;
for (int j = i; j<size; j++)
{
if (a[k] > a[j])
{
k = j;
}
}
swap(a[k],a[i]);
}
} //Sort each pair of elements Then, sort every four elements by merging every two pairs Then,
//sort every 8 elements, etc O(n log n) expected and worst case void mSort(int *a, int begin, int end, int* temp)
{
if (a == NULL)
{
return;
}
int mid = (begin + end)/2;
int i = begin;
int j = mid+1;
int p = 0; while (i<=mid && j<=end)
{
if (a[i]>a[j])
{
temp[p] = a[j];
p++;
j++;
} else
{
temp[p] = a[i];
p++;
i++;
} } while(i<=mid)
{
temp[p] = a[i];
p++;
i++;
} while(j<=end)
{
temp[p] = a[j];
p++;
j++;
}
for (i = 0; i < p; i++)
a[begin + i] = temp[i];
} void merge(int *a, int begin, int end, int *temp)
{
if (begin<end)
{
int mid = (begin + end)/2;
merge(a,begin,mid,temp);
merge(a,mid +1,end,temp);
mSort(a,begin,end,temp);
}
}
//Quick Sort
//Pick a random element and partition the array, such that all numbers that are less than it
// come before all elements that are greater than it Then do that for each half, then each
//quarter etc O(n log n) expected, O(n^2) worst case. int partion(int *a, int begin, int end)
{
int t = a[begin];
int low = begin;
int high = end;
while(low < high)
{
while(low < high&& t<=a[high])
{
high--;
}
mswap(a[low],a[high]); while(low < high && t>=a[low])
{
low++;
}
mswap(a[low],a[high]); }
return low;
} void quickSort(int *a, int begin, int end)
{
if (begin < end)
{
int i = partion(a, begin, end);
quickSort(a, begin, i-1);
quickSort(a, i + 1, end);
}
} int main()
{
int a[] = {78, 17, 39, 26, 72, 94, 21, 12, 23, 91};
//bubbleSort(a,6);
//selectSort(a,6);
// int *p = new int[6];
// merge(a,0,5,p);
// quickSort(a,0,5); print(a,10); return 0;
}
Cracking The Coding Interview 9.0的更多相关文章
- Cracking The Coding Interview 2.0 单链表
#include <iostream> #include <string> using namespace std; class linklist { private: cla ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
随机推荐
- centos 安装npm node
最近那vue全套造了个管理系统的轮子,发现node简直太好用了. elment-UI的出现就是不懂ui设计的后台工程师的福音~ 正好自己买的两个云服务器空闲着没用,就拿来试试看了 首先软件都安装在/u ...
- 分享:selenium(一) xpath
xpath无所不能定位. https://www.w3.org/TR/xpath/all/#axes 两个神器:firebug.xpath-checker 举例:混合定位 //td[a//fron ...
- 雷林鹏分享:XML 命名空间
XML 命名空间 XML 命名空间提供避免元素命名冲突的方法. 命名冲突 在 XML 中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突. 这个 XML 携带 HTML ...
- django笔记 - 建站
1,建站步骤:1)django-admin.exe startproject mysite 创建完后的目录结构: - mysite # 对整个程序进行配置 - init - settings # 配置 ...
- python2.x编码问题实例
1,编码问题,主要是区分面向人类的字符串,面向计算机的字节序列 在python3中,字符串是str(默认即unicode),字节序列是bytes 在python2中,字符串是unicode,字节序列是 ...
- 【消息队列】kafka是如何保证消息不被重复消费的
一.kafka自带的消费机制 kafka有个offset的概念,当每个消息被写进去后,都有一个offset,代表他的序号,然后consumer消费该数据之后,隔一段时间,会把自己消费过的消息的offs ...
- 4.1.4 Nim
Problem description: 有n堆石子,每堆各有ai颗石子.A和B轮流从非空的石子堆中取走至少一颗石子.A先取,取光所有石子的一方获胜.当双方都采用最佳策略时,谁会获胜? 1<=n ...
- Python基础之文件的基本操作
概述:文件的基本操作1.open 打开文件 f = open("xxx",mode="r",encoding="utf-8") #常用形式 ...
- React文档(三)介绍JSX
我们先看看这个变量声明: const element = <h1>Hello, world!</h1>; 这个有趣的标签语法既不是字符串也不是HTML. 这种写法叫做JSX,这 ...
- SpringMVC,Controller的返回页面类型以及路径设置默认值
一般设置在spring-servlet.xml里面设置 <!-- 对转向页面的路径解析.prefix:前缀, suffix:后缀 --> <bean class="org. ...