#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. 使用两个栈来完成一个队列,需要是实现的功能有add,poll.peek

    2017-06-23 19:15:16 队列时先进先出型,而栈是先进后出型,这就需要建立一个联系.我想到的一个简单的表示方式是: 这样就需要两个栈,栈1是用来实现add操作,即直接push进去就行:栈 ...

  2. Java中类似C#中Task.wait()的类CountDownLatch

    当主线程开辟多个子线程,而又需要这些子线程都执行完成后再执行主线程后续的操作,在C#中可以通过Task的wait方法来实现,然而在Java中也有类型的类CountDownLatch,具体用法如下: p ...

  3. python中函数与函数式编程(二)

    首先要明白为什么要用到返回值,返回值的作用就是为了分情况来处理下面的程序(个人见解总结) 1.函数返回值 def test1(): pass def test2(): return 0 def tes ...

  4. R语言画点状误差线

    现在项目需要R语言做几个线性拟合,画一些点图,突然需要画误差线,网上找了下,可以用代码实现..效果如下 xx1<-c(xxxxxx,xxxx,xxxxx) yy1<-c(xxxxxx,xx ...

  5. [Spring] ClassPathXmlApplicationContext类

    1. 该类在package org.springframework.context.support包下. 该包在4.0.1中封装在spring-context-***.jar中. 其无参构造函数的文档 ...

  6. 上传RNA-seq数据到NCBI GEO数据库

    SRA - NCBI example - NCBI 要发文章了,审稿时编辑肯定会要求你上传NGS测序数据. 一般数据都是放在集群,不可能放在个人电脑上,因为有的数据大的吓人(几个T). 所以我们就建一 ...

  7. zookeeper在搭建的时候,解决后台启动为standalone模式问题

    今天在搭建zookeeper,搭建完成之后,启动一直报错: 上网查了好多资料:有几种解决方案: 1.在配置文件conf目录下,将zoo_sample.cfg删除,只留zoo.cfg(然而就我的情况而言 ...

  8. H264编码 封装成MP4格式 视频流 RTP封包

    H264编码 封装成MP4格式 视频流 RTP封包         分类:             多媒体编程              2013-02-20 21:31     3067人阅读    ...

  9. LeetCode--231--2的幂函

    问题描述: 给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 示例 1: 输入: 1 输出: true 解释: 2 0  = 1 示例 2: 输入: 16 输出: true 解释: 2 4   ...

  10. android -------- 蓝牙Bluetooth

    什么是蓝牙? 也可以说是蓝牙技术.所谓蓝牙(Bluetooth)技术,实际上是一种短距离无线电技术,是由爱立信公司公司发明的.利用“蓝牙”技术,能够有效地简化掌上电脑.笔记本电脑和移动电话手机等移动通 ...