ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 快速排序

快速排序

原创 Linux操作系统 作者:jyang57 时间:2010-08-03 14:03:59 0 删除 编辑

 
C中的快速排序源代码

  //参照《数据结构》(C语言版)
  //调用:quicksort-->qsort-->partitions
  int partitions(int a[],int low,int high)
  {
  int pivotkey=a[low];
  //a[0]=a[low];
  while(low
  {
  while(low=pivotkey)
  --high;
  a[low]=a[high];
  while(low
  ++low;
  a[high]=a[low];
  }
  //a[low]=a[0];
  a[low]=pivotkey;
  return low;
  }
  void qsort(int a[],int low,int high)
  {
  int pivottag;
  if(low
  { //递归调用
  pivottag=partitions(a,low,high);
  qsort(a,low,pivottag-1);
  qsort(a,pivottag+1,high);
  }
  }
  void quicksort(int a[],int n)
  {
  qsort(a,1,n);
  }
  //简单示例
  #include
  //#include
  #include "myfunc.h" //存放于个人函数库中
  main()
  {
  int i,a[11]={0,11,12,5,6,13,8,9,14,7,10};
  for(i=0;i<11;printf("%3d",a[i]),++i);
  printf("\n");
  quicksort(a,10);
  for(i=0;i<11;printf("%3d",a[i]),++i);
  printf("\n");
  }

C++中的快速排序源代码

  #include
  using namespace std;
  void QuickSort(int *pData,int left,int right)
  {
  int i(left),j(right),middle(0),iTemp(0);
  middle=pData[(left+right)/2];//求中间值
  middle=pData[(rand()%(right-left+1))+left]; //生成大于等于left小于等于right的随机数
  do{
  while((pData[i]
  i++;
  while((pData[j]>middle) && (j>left))//从右扫描小于中值的数
  j--;
  //找到了一对值,交换
  if(i<=j){
  iTemp=pData[j];
  pData[j]=pData[i];
  pData[i]=iTemp;
  i++;
  j--;
  }
  }while(i<=j);//如果两边扫描的下标交错,就停止(完成一次)
  //当左边部分有值(left
  if(left  QuickSort(pData,left,j);
  }
  //当右边部分有值(right>i),递归右半边
  if(right>i){
  QuickSort(pData,i,right);
  }
  }
  int main()
  {
  int data[]={10,9,8,7,6,5,4};
  const int count(6);
  QuickSort(data,0,count);
  for(int i(0);i!=7;++i){
  cout<  }
  cout<  return 0;
  }

VB中的快速排序源代码

  '快速排序算法,对字符串数组进行排序
  Private Sub quicksort(ByRef arrValue() As String, ByVal intLx As Integer, ByVal intRx As Integer)
  'arrValue()是待排的数组,intLx,intRx为左右边界
  Dim strValue As String
  Dim I As Integer
  Dim j As Integer
  Dim intLoop As Integer
  I = intLx
  j = intRx
  Do
  While arrValue(I) <= arrValue(j) And I < j: I = I + 1: Wend
  If I < j Then
  strValue = arrValue(I)
  arrValue(I) = arrValue(j)
  arrValue(j) = strValue
  End If
  While arrValue(I) <= arrValue(j) And I < j: j = j - 1: Wend
  If I < j Then
  strValue = arrValue(I)
  arrValue(I) = arrValue(j)
  arrValue(j) = strValue
  
  End If
  Loop Until I = j
  I = I - 1: j = j + 1
  
  If I > intLx Then
  Call quicksort(arrValue, intLx, I)
  End If
  If j < intRx Then
  Call quicksort(arrValue, j, intRx)
  End If
  End Sub
  Private Sub Form_Load()
  Dim arr(8) As String
  arr(0) = “r&”
  arr(1) = “e”
  arr(2) = “a”
  arr(3) = “n”
  arr(4) = “b”
  arr(5) = “u”
  arr(6) = “c”
  arr(7) = “o”
  arr(8) = “f”
  Call quicksort(arr, 0, UBound(arr))
  End Sub

Java中的快速排序源代码

  public class QuickSort {
  /**
  * 快速排序
  */
  public static void main(String[] args) {
  Random random=new Random();
  int[] pData=new int[10];
  for(int i=0;i  Integer a =random.nextInt(100);
  pData[i]= a;
  System.out.print(pData[i]+“ ”);
  }
  System.out.println();
  int left=0;
  int right=pData.length-1;
  Sort(pData,left,right);
  for(int i=0;i  System.out.print(pData[i]+“ ”);
  }
  System.out.println();
  }
  public static int[] Sort(int[] pData, int left, int right){
  int middle,strTemp;
  int i = left;
  int j = right;
  middle = pData[(left+right)/2];
  do{
  while((pData[i]  i++;
  while((pData[j]>middle) && (j>left))
  j--;
  if(i<=j){
  strTemp = pData[i];
  pData[i] = pData[j];
  pData[j] = strTemp;
  i++;
  j--;
  }
  for(int k=0;k  System.out.print(pData[k]+“ ”);
  }
  System.out.println();
  }while(i  if(left  Sort(pData,left,j); //递归调用
  if(right>i)
  Sort(pData,i,right); //递归调用
  return pData;
  }
  }

C#中的快速排序源代码

  namespace temp
  {
  public class QuickSort
  {
  ///
  /// 排序
  ///
  /// 待排序数组
  /// 数组第一个元素索引Index
  /// 数组最后一个元素索引Index
  private static void Sort(int[] numbers, int left, int right)
  {
  //左边索引小于右边,则还未排序完成
  if (left < right)
  {
  //取中间的元素作为比较基准,小于他的往左边移,大于他的往右边移
  int middle = numbers[(left + right) / 2];
  int i = left - 1;
  int j = right + 1;
  while (true)
  {
  while (numbers[++i] < middle) ;
  while (numbers[--j] > middle) ;
  if (i >= j)
  break;
  Swap(numbers, i, j);
  }
  Sort(numbers, left, i - 1);
  Sort(numbers, j + 1, right);
  }
  }
  ///
  /// 交换元素值
  ///
  /// 数组
  /// 当前左边索引
  /// 当前右边索引
  private static void Swap(int[] numbers, int i, int j)
  {
  int number = numbers[i];
  numbers[i] = numbers[j];
  numbers[j] = number;
  }
  public static void Main()
  {
  int[] max = { 6, 5, 2, 9, 7, 4, 0 };
  Sort(max, 0, max.Length-1);
  StringBuilder temp =new StringBuilder();
  for (int i = 0; i < max.Length; i++)
  {
  temp.Append(max[i].ToString()+",");
  }
  Console.WriteLine(temp.ToString().Substring(0,temp.Length-1));
  Console.ReadLine();
  }
  }
  }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23155850/viewspace-670074/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-01-09

  • 博文量
    110
  • 访问量
    164137