快速排序--递归实现

#include <iostream> 
#include <iomanip> 
#include <cstdlib>
#include <ctime>
using namespace std;

const int SIZE = 10;
const int MAX_NUMBER = 1000;

void quicksort( int * const, int, int );
int partition( int * const, int, int );
void swap( int * const, int * const );

int main()
{
   int arrayToBeSorted[ SIZE ] = {};
   int loop;

   //设置种子
   srand( time( 0 ) );

   //使用随机数填充数组
   for ( loop = 0; loop < SIZE; loop++ )
      arrayToBeSorted[ loop ] = rand() % MAX_NUMBER;

   cout << "Initial array values are:\n";

   //排序前
   for ( loop = 0; loop < SIZE; loop++ )
       cout << setw( 4 ) << arrayToBeSorted[ loop ];

   cout << "\n\n";

   if ( SIZE == 1 )
      cout << "Array is sorted: " << arrayToBeSorted[ 0 ] << '\n';
   else 
   {
      quicksort( arrayToBeSorted, 0, SIZE - 1 );
      cout << "The sorted array values are:\n";

      for ( loop = 0; loop < SIZE; loop++ )
         cout << setw( 4 ) << arrayToBeSorted[ loop ];

      cout << endl;
   } 
} 

void quicksort( int * const array, int first, int last )
{
   int currentLocation;

   if ( first >= last )
      return;

   currentLocation = partition( array, first, last ); // place an element
   quicksort( array, first, currentLocation - 1 ); // sort left side
   quicksort( array, currentLocation + 1, last ); // sort right side
} 

int partition( int * const array, int left, int right )
{
   int position = left;

   while ( true ) 
   {
      //从后向前找,直到找到一个比枢轴小的数,或者均比枢轴大时结束
      while ( array[ position ] <= array[ right ] && position != right )
         right--;
      //如果枢轴之后的元素均比枢轴大,必须要交换,直接返回
      if ( position == right )
         return position;
      //找到比枢轴小的数,交换
      if ( array[ position ] > array[ right ]) 
      {
         swap( &array[ position ], &array[ right ] );
         position = right;
      } 
      //从前往后找
      while ( array[ left ] <= array[ position ] && left != position )
         left++;

      if ( position == left )
         return position;

      if ( array[ left ] > array[ position ] ) 
      {
         swap( &array[ position ], &array[ left ] );
         position = left;
      } 
   } 
} 

void swap( int * const ptr1, int * const ptr2 )
{
   int temp;

   temp = *ptr1;
   *ptr1 = *ptr2;
   *ptr2 = temp;
} 
快速排序是一种常用的排序算法,它通过递归地将数组分成较小和较大的两个子数组来实现排序。下面是快速排序的C语言递归实现: ```c #include <stdio.h> // 交换两个元素的值 void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } // 将数组分成两部分,并返回分割点的索引 int partition(int arr[], int low, int high) { int pivot = arr[high]; // 选取最后一个元素作为基准值 int i = (low - 1); // 记录小于基准值的元素的索引 for (int j = low; j <= high - 1; j++) { // 如果当前元素小于等于基准值,则将其与小于基准值的元素交换位置 if (arr[j] <= pivot) { i++; swap(&arr[i], &arr[j]); } } swap(&arr[i + 1], &arr[high]); // 将基准值放到正确的位置上 return (i + 1); // 返回分割点的索引 } // 快速排序递归函数 void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); // 获取分割点的索引 // 对分割点左边的子数组进行递归排序 quickSort(arr, low, pi - 1); // 对分割点右边的子数组进行递归排序 quickSort(arr, pi + 1, high); } } // 打印数组 void printArray(int arr[], int size) { for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); } int main() { int arr[] = {64, 25, 12, 22, 11}; int n = sizeof(arr) / sizeof(arr); printf("原始数组:\n"); printArray(arr, n); quickSort(arr, 0, n - 1); printf("排序后的数组:\n"); printArray(arr, n); return 0; } ``` 这段代码实现快速排序算法的递归版本。首先定义了一个`swap`函数用于交换两个元素的值。然后定义了`partition`函数,它选择最后一个元素作为基准值,将数组分成两部分,并返回分割点的索引。接下来是`quickSort`函数,它通过递归调用自身来对分割点左右两个子数组进行排序。最后,在`main`函数中测试了快速排序算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值