static void Main(string[] args)
{ Console.WriteLine("************快速排序*****************");
int[] list = new int[] { , , , , , , , };
QuickSort qs = new QuickSort();
qs.quikSort(list, , list.Length - );
for (int i = ; i < list.Length; i++)
{
Console.WriteLine("第{0}位是{1}", i + , list[i]);
}
// Console.WriteLine(list); Console.ReadKey();
} //递归快速排序法
public class QuickSort
{
//快速排序
public void quikSort(int[] arr, int startIndex, int endIndex)
{
//递归结束条件 startIndex>endIndex
if (startIndex >= endIndex)
{
return;
} //得到基准元素的位置
int pivotIndex = partiton(arr, startIndex, endIndex); //根据基准元素,分成两部分递归排序
quikSort(arr, startIndex, pivotIndex - );
quikSort(arr, pivotIndex + , endIndex);
}

//指针交换法
private static int partiton(int[] arr, int startIndex, int endIndex)
{
//取第一个位置的元素作为基准元素
int pivot = arr[startIndex];
int left = startIndex;
int right = endIndex; while (left != right)
{
//控制right指针比较并左移
while (left < right && arr[right] > pivot)
{
right--;
} //控制right指针比较并右移
while (left < right && arr[left] <= pivot)
{
left++;
} //交换left和right指向的元素
if (left < right)
{
int p = arr[left];
arr[left] = arr[right];
arr[right] = p;
}
} //pivoe和指针重合点交换
int s = arr[left];
arr[left] = arr[startIndex];
arr[startIndex] = s; return left;
}
//挖坑法
private static int partiton1(int[] arr, int startIndex, int endIndex)
{
// 4, 7, 6, 5, 3, 2, 8, 1
//获取第一个位置的元素作为基准元素
int pivot = arr[startIndex]; //
int left =startIndex; //
int right = endIndex; //7 //坑的位置,初始等于pivot的位置
int index = startIndex; //0 //大循环在左右指针重合或交错时结束
while (right >= left)
{
//right指针从右向左进行比较
while (right >= left)
{
int a = arr[right];
if (arr[right] < pivot)
{
int s = arr[right];
int t = arr[left]; arr[left] = arr[right];
index = right;
left++;
break;
}
right--;
} //left指针从左向右进行比较
while (right > left)
{
if (arr[left] >= pivot)
{
int a = arr[right];
int b = arr[left]; arr[right] = arr[left];
index = left;
right--;
break;
}
left++; }
} arr[index] = pivot;
return index;
}
     }

C# 递归式快速排序算法的更多相关文章

  1. 《c程序设计语言》读书笔记-递归实现快速排序算法

    #include <stdio.h> void swap(int v[],int i,int j) { int temp; temp = v[i]; v[i] = v[j]; v[j] = ...

  2. 算法——快速排序迭代式和递归式的Java实现

    快速排序迭代式和递归式的Java实现 快速排序基于分治法的思想,在待排序表中任选一值作为中枢值 pivot,一趟快排将所有大于该值的元素置于一边,小于该值的元素置于另一边,这样一个元素在排序中的最终位 ...

  3. 【Python算法】递归与递归式

    该树结构显示了从1(根节点)到n(n个叶节点)的整个倍增过程.节点下的标签表示从n减半到1的过程. 当我们处理递归的时候,这些级数代表了问题实例的数量以及对一系列递归调用来说处理的相关工作量. 当我们 ...

  4. 分析递归式 Solving Recurrences------GeeksforGeeks 翻译

    在上一章中我们讨论了如何分析循环语句.在现实中,有很多算法是递归的,当我们分析这些算法的时候我们要找到他们的的递归关系.例如归并排序,为了排序一个数组,我们把它平均分为两份然后再重复平分的步骤.最后我 ...

  5. C# 集合扩展快速排序算法

    /// <summary> /// 对集合进行排序,如 /// List<Person> users=new List<Person>(){.......} /// ...

  6. 快速排序算法-C语言实现

    注:本篇内容为翻译,之所以选择这篇进行翻译原因是该文章含有动画,能够更加直观地展示快速排序.同时,可以仔细看一下代码,代码中把结构化的思想给予了更加充分地表现.按照功能进行模块划分的思想得到了彻底地贯 ...

  7. C#快速排序算法基础入门篇

    相信算法对于许多开发人员来说都是一大难点,之所以难,就像设计模式一样,许多人在阅读之后,没有很好地理解,也不愿意动手上机操作,只停留在理论的学习上面,随着时间推移就慢慢淡忘. 有些东西,你可以发明创造 ...

  8. 快速排序算法 Quick sort

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4046189.html 首先随机选择一个轴,并调整数组内各个数字,使得比轴值大的数在轴的右边, ...

  9. 快速排序算法(C#实现)

    想到了快速排序,于是自己就用C#实现了快速排序的算法: 快速排序的基本思想:分治法,即,分解,求解,组合 . 分解:在 无序区R[low..high]中任选一个记录作为基准(通常选第一个记录,并记为k ...

随机推荐

  1. [windows菜鸟]C#中调用Windows API参考工具

    很多windows API都不知道签名,可以从下面几种方式进行查询 1.微软出的工具 P/Invoke Interop Assistant version 1.0 2.网站 pinvoke.net 3 ...

  2. 进程分配内存的两种方式--brk() 和mmap()(不设计共享内存)(转)

    如何查看进程发生缺页中断的次数? 用ps -o majflt,minflt -C program命令查看. majflt代表major fault,中文名叫大错误,minflt代表minor faul ...

  3. Elasticsearch mapping映射文件设置没有生效

    Elasticsearch mapping映射文件设置没有生效 问题背景 我们一般会预先创建 Elasticsearch index的 mapping.properties 文件(类似于MySQL中的 ...

  4. Access access中,查询字段是否存

    '===========================================================' 过程及函数名:  ListAllTableAndAllField' 版本号  ...

  5. Xcode真机报错clang: error: linker command failed with exit code 1 (use -v to see invocation)

    出现这种错误,如下图所示,搜索bitcode,置为NO即可.

  6. JavaScript(2):函数

    <!DOCTYPE html> <html> <body> <p>JavaScript 函数</p> <script> // 函 ...

  7. YAML基础知识及搭建一台简洁版guestbook

    一,前言 前面我们已经搭建过简易版k8s集群了,在此基础上可以搭建一个简洁版guestbook ,以便来学习k8s创建pod的整个过程. 二,在此之前,我们还需要学习一下YAML基础知识 YAML 基 ...

  8. python计算机二级考试知识点——文件操作

    1. 文件的使用:文件打开.关闭和读写 python通过open函数打开一个文件,并返回一个操作文件的变量,语法形式如下: <变量名>=open(<文件路劲及文件名>,< ...

  9. ERROR】Unable to open underlying table which is differently defined or of non-MyISAM type or ...

    Error: Unable to open underlying table which is differently defined or of non-MyISAM type or doesn’t ...

  10. 分片式图片服务器fastDFS安装过程

    1. 什么是FastDFS FastDFS 是用 c 语言编写的一款开源的分布式文件系统.FastDFS 为互联网量身定制, 充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标, ...