温故而知新,算法在我心
2009.9.1号添加快速排序:
//快速排序
//一:你可以随机找一个基点,然后和第一个数字交换
//二:left表示把指针指向第二个数字和right表示指针指向最后一个数字
//三:left向右移动,找到大于基点的(也就是第一个)就停止移动,right向左移动找到小于基点停止移动,left和right交换数字
//四:当left和right 交叉时,把right的值和基点交换,
//当right和left指向一个数字时,交换,排序完成
//当right和left分别指向第一个和最后一个时,就改为基点的一个数字为基点
//50 65 30 43 76 80 18 50
//第一次:18 50 30 42 50 80 76 65
//第二次:18 30 43 50 50 80 76 65
//第三次:18 30 43 50 50 65 76 80
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { 3, 4, 1, 5, 9, 2, 7, 6, 8 };
Sort(array, 0, array.Length - 1);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (int a in array)
{
Console.Write(a);
Console.Write(", ");
}
Console.Read();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static void Sort(int[] array, int min, int max)
{
if (min >= max)
return;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int minTemp = min;
int maxTemp = max;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int middle = min;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int temp;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while (min < max)
{
while (array[max] > array[middle])
max--;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
temp = array[max];
array[max] = array[middle];
array[middle] = temp;
middle = max;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while (array[min] < array[middle])
min++;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
temp = array[min];
array[min] = array[middle];
array[middle] = temp;
middle = min;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Sort(array, minTemp, middle - 1);
Sort(array, middle + 1, maxTemp);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2008-3-8-今日又去面试了,碰到一题,回来仔细写了下,不知道是不是这样效率最好:
![ggg.jpg](https://images.cnblogs.com/cnblogs_com/guozili/ggg.jpg)
2008-3-7-最近又开始面试了,由于长期做NET应用,导致基础算法不清,错失很多良机,故回家补习,列出几例,以便面试临时必备:
![aaa.jpg](https://images.cnblogs.com/cnblogs_com/guozili/aaa.jpg)
如Combind(new int[]{1,3,5}, new int[]{2,4},c)则得到,1,2,3,4,5
![ccc.jpg](https://images.cnblogs.com/cnblogs_com/guozili/ccc.jpg)
如a,b,c,则Permute(0)则得到abc, acb, bac,.......所有组合
![ddd.jpg](https://images.cnblogs.com/cnblogs_com/guozili/ddd.jpg)
![eee.jpg](https://images.cnblogs.com/cnblogs_com/guozili/eee.jpg)
![fff.jpg](https://images.cnblogs.com/cnblogs_com/guozili/fff.jpg)
猫叫,主人醒来,老鼠逃
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2008-3-8-今日又去面试了,碰到一题,回来仔细写了下,不知道是不是这样效率最好:
![ggg.jpg](https://images.cnblogs.com/cnblogs_com/guozili/ggg.jpg)
2008-3-7-最近又开始面试了,由于长期做NET应用,导致基础算法不清,错失很多良机,故回家补习,列出几例,以便面试临时必备:
![aaa.jpg](https://images.cnblogs.com/cnblogs_com/guozili/aaa.jpg)
如Combind(new int[]{1,3,5}, new int[]{2,4},c)则得到,1,2,3,4,5
![ccc.jpg](https://images.cnblogs.com/cnblogs_com/guozili/ccc.jpg)
如a,b,c,则Permute(0)则得到abc, acb, bac,.......所有组合
![ddd.jpg](https://images.cnblogs.com/cnblogs_com/guozili/ddd.jpg)
![eee.jpg](https://images.cnblogs.com/cnblogs_com/guozili/eee.jpg)
![fff.jpg](https://images.cnblogs.com/cnblogs_com/guozili/fff.jpg)
猫叫,主人醒来,老鼠逃
![bbb.jpg](https://images.cnblogs.com/cnblogs_com/guozili/bbb.jpg)