#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的更多相关文章

  1. Cracking The Coding Interview 2.0 单链表

    #include <iostream> #include <string> using namespace std; class linklist { private: cla ...

  2. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  3. 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 ...

  4. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  5. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  6. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  7. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  9. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

随机推荐

  1. 2d游戏和 3d游戏的区别

    2D游戏和3D游戏的主要区别 一.总结 一句话总结:2D中的单位就是贴图,3D中的单位还有高 1. 3D 和 2D 游戏的区别主要体现在呈现画面和文件体积上: 2. 借助 3D 引擎可以提升 2D 游 ...

  2. linux下逻辑卷管理 调整分区大小

    [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 50 ...

  3. 雷林鹏分享:jQuery EasyUI 数据网格 - 创建子网格

    jQuery EasyUI 数据网格 - 创建子网格 使用数据网格(datagrid)的详细视图,用户可以展开一行来显示附加的详细信息. 任何内容都可以加载作为行详细,子网格也可以动态加载. 本教程将 ...

  4. 雷林鹏分享:XML 元素

    XML 元素 XML 文档包含 XML 元素. 什么是 XML 元素? XML 元素指的是从(且包括)开始标签直到(且包括)结束标签的部分. 一个元素可以包含: 其他元素 文本 属性 或混合以上所有. ...

  5. Permutations CodeForces - 736D (矩阵逆)

    对于删除每个对(x,y), 可以发现他对答案的贡献为代数余子式$A_{xy}$ 复习了一下线代后发现代数余子式可以通过伴随矩阵求出, 即$A_{xy}=A^*[y][x]$, 伴随矩阵$A^*=|A| ...

  6. 『MXNet』第一弹_基础架构及API

    MXNet是基础,Gluon是封装,两者犹如TensorFlow和Keras,不过得益于动态图机制,两者交互比TensorFlow和Keras要方便得多,其基础操作和pytorch极为相似,但是方便不 ...

  7. Pycharm中安装package出现microsoft visual c++ 14.0 is required问题解决办法

    在利用pycharm安装scrapy包是遇到了挺多的问题.在折腾了差不多折腾了两个小时之后总算是安装好了.期间各种谷歌和百度,发现所有的教程都是利用命令行窗口安装的.发现安装scrapy需要的包真是多 ...

  8. phpStorm中Structure窗口中的符号代表的意思

    参考:https://www.jetbrains.com/help/phpstorm/2016.3/symbols.html Icon   Description Class 类 Final clas ...

  9. SpringMvc4.2.5 零配置出现 No mapping found for HTTP request with URI(转)

    原文地址:SpringMvc4.2.5 零配置出现 No mapping found for HTTP request with URI 采用 spring 零配置,参考 http://hanqunf ...

  10. Spring Boot系列之配置日志输出等级

    我们都知道Spring boot 默认使用 logback作进行日志输出,那么 在配置Spring boot日志输出时有两种方式: 通过application.properties 配置文件的方式来配 ...