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 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
随机推荐
- Python 编程快速上手 第十一章 Web scrapping
前言 这一章讲了如何在 Web 上抓取相关的信息,工具是三个模块: webbrowser 模块:用于打开浏览器指定页面 requests 模块:用于下载文件 Beautiful Soup 模块:用于解 ...
- nginx ----> nginx配置/反向代理/负载均衡
nginx [engine x]是一个HTTP和反向代理服务器,一个邮件代理服务器和一个通用的TCP/UDP代理服务器,最初由Igor Sysoev编写. 环境: Ubuntu16.04 安装ngin ...
- wpf 使用DocumentViewer打印
https://blog.csdn.net/duanzi_peng/article/details/14118937?locationNum=15 https://www.cnblogs.com/zl ...
- LeetCode--434--字符串中的单词数
问题描述: 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is Joh ...
- vux弹框显示
//点击按钮,执行什么成功,失败用这个合适this.$vux.toast.show({ text: '添加成功'}) //点击按钮,提示出现的问题 this.$vux.toast.show({ tex ...
- linux中用户和用户组
一.用户和组原理 一个用户可以属于多个组,一个组有多个用户 在Linux中操作系统必须依赖组和用户进行管理 二.与用户和组相关的配置文件 1.组相关配置文件 1)/etc/group :管理用户组信息 ...
- ddt 实例
from :https://blog.csdn.net/wushuai150831/article/details/78453549
- function_exists
在已经定义的函数列表(包括系统自带的函数和用户自定义的函数)中查找 function_name. 如果 function_name 存在且的确是一个函数就返回 TRUE ,反之则返回 FALSE .
- PTA L2-002 链表去重
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805072641245184 第一次做链表题,有时间多看看 解释 ...
- Oracle 多表查询、查询运算符和集合运算
一.多表查询 1.内连接 一般使用INNER JOIN关键字指定内连接,INNER可以省略,默认表示内连接.查询结果中只包含两表的公共字段值相等的行,列可以是两表中的任意列 2.外连接 包括左外连接. ...