排序的泛型类clsSort
来源:互联网 发布:淘宝店铺装修图片尺寸 编辑:程序博客网 时间:2024/05/18 18:55
using System;
using System.Collections.Generic;
public class clsQuickSort<T> where T : IComparable
{
// ================= instance member ====================
T[] data;
// ================= constructor ====================
public clsQuickSort(T[] values)
{
data = values; // copy rvalue of unsorted data
}
// ================= Property methods ====================
// ================= Helper methods ====================
/*****
* Purpose: This method gets the initial pivot point for the sort and then
* calls itself recursively until all values have been set.
*
* Parameter list:
* int first index of first element of partition
* int last index of last element of partition
*
* Return value:
* void
*****/
private void doSort(int first, int last)
{
if (first == last)
{
return; // Done
}
else
{
int pivot = getPivotPoint(first, last);
if (pivot > first)
doSort(first, pivot - 1);
if (pivot < last)
doSort(pivot + 1, last);
}
}
private int getPivotPoint(int first, int last)
{
int pivot = first;
int start = first; // Keep copies
int end = last;
if (last - first >= 1)
{
while (end > start)
{
while (data[pivot].CompareTo(data[start]) >= 0 &&
start <= last && end > start)
start++;
while (data[pivot].CompareTo(data[end]) <= 0 &&
end >= first && end >= start)
end--;
if (end > start)
swap(start, end);
}
swap(first, end);
doSort(first, end - 1);
}
return end;
}
/*****
* Purpose: This method performs the data swap for quickSort()
*
* Parameter list:
* int pos1 index to first value to swap
* int pos2 index to second value to swap
*
* Return value:
* void
*****/
private void swap(int pos1, int pos2)
{
T temp;
temp = data[pos1];
data[pos1] = data[pos2];
data[pos2] = temp;
}
// ================= General methods ====================
/*****
* Purpose: This is the user interface entry point for the Quicksort
*
* Parameter list:
* n/a
*
* Return value:
* void
*
* CAUTION: This routine assumes constructor is passed unsort data
*****/
public void Sort()
{
int len = data.Length;
if (len < 2) // Enough to sort?
return;
doSort(0, data.Length - 1);
}
}
对这个类实现过程曾设置断点运行试过 ,可是不懂这个排序的思想是怎么样,希望明白的朋友给你解说,谢谢!
- 排序的泛型类clsSort
- 【排序】排序的基本概念
- 数组的排序:冒泡排序、选择排序
- 常用的排序算法:插入排序,希尔排序,冒泡排序,选择排序,快速排序,归并排序
- 单链表的排序(选择排序)
- 排序的之插入排序
- 简单的排序---选择排序
- 简单的排序---插入排序
- 简单的排序---堆排序
- 不是快速排序的排序
- 【排序】链表上的归并排序
- 堆排序 - 不稳定的排序
- 排序:一些排序的总结
- 有趣的排序-百度-排序
- 三种最基本的排序 选择排序 插入排序 冒泡排序
- java数组的排序,选择排序,冒泡排序,插入排序
- 基本的排序算法:冒泡排序、插入排序、希尔排序、选择排序、归并排序、快速排序、堆排序
- 各种常见的排序,冒泡排序,选择排序,插入排序,希尔排序,堆排序,快速排序,基数排序,桶排序
- PyCharm.v1.x License
- 在应用程序中添加快捷图标
- USB KEY身份认证管理系统及网站访问控制方案(二)
- 关于显卡
- new操作符用于新建对象类型实例
- 排序的泛型类clsSort
- unrar命令
- C++中内存分配的问题
- PizzaDelivery
- 实战DeviceIoControl
- Date 是内置对象,包含日期和时间信息
- android rle格式开机logo制作
- jxl 和 itext 的 使用总结 (网上查的资料)
- 日本語の文法の筆記