快速排序(c++代码)

//快速排序
/*
快速排序的思路是:
1.先从数列中取出一个数作为基准数.
2.分区.将比这个数大的数全部放到它的右边,小于或等于它的数全放到左边.
3.再对左右区间重复第二步,直到各区间只有一个数或没有数.
*/
//如有错误,欢迎指正.


#include <stdio.h>
#include <vector>
#include <stack>
using namespace std;


//《算法导论》上的分区方法,具体图例请参考本书.
//选取最右边的数为基准数,从左向右单边扫描.将数组分为4部分:
//区域一(left至i) : 值<= 基准数的区域
//区域二(i+1至j-1): 值> 基准数的区域
//区域三(j至right-1) :未划分区域
//区域四(right) :基准数
//我们假设left = 0 , right = 10
int _partition(vector<int> &vec , int left , int right){
    int pivotkey = vec[right];
    int temp;
	//一开始区域一为空: left(0) -> i(-1)
	//区域二也为空 : i+1(0) -> j-1(-1)
	//区域三为: j(0) -> right-1(9)
    int i = left - 1;
	for ( int j = left ; j < right ; j++ ) {
		//找到 <= 基准数的数的位置,我们将其和区域二最前面一个元素交换.
		//从而扩大区域一.
		if ( vec[j] <= pivotkey ){
            i++;
            //exchange( vec[i] , vec[j])
            temp = vec[i];
            vec[i] = vec[j];
            vec[j] = temp;
        }
		//j++扩大区域二的位置,减少区域三的位置.
	}
    //exchange( vec[i+1] , vec[right])
    temp = vec[i+1];
    vec[i+1] = vec[right];
    vec[right] = temp;
	//
    return i+1;
}

//另一种分区方法.俗称挖坑法.
//选取最左边的数为基准数,从两边向中间扫描.
int _partition_1(vector<int> &vec , int left , int right){
    int pivotkey = vec[left];
    while( left < right ){
		//从右向左扫描
        while ( left < right && vec[right] > pivotkey ) right--;
        if ( left < right ) {
			vec[left] = vec[right];
			left++;
		}
		//从左向右扫描
        while ( left < right && vec[left] <= pivotkey ) left++;
        if ( left < right ) {
			vec[right] = vec[left];
			right--;
		}
    }
    //两个坑坑到一起了.把基准数塞入这个坑.
    vec[left] = pivotkey;
    return left;
}

//快速排序(递归方式)
void _quickSort(vector<int> &vec , int left , int right){
    if ( left < right ){
        int pivot_pos = _partition( vec , left , right);
        //int pivot_pos = _partition_1( vec , left , right );
        _quickSort( vec , left , pivot_pos - 1);
        _quickSort( vec , pivot_pos + 1 , right);
    }
}

//快速排序包装函数(递归方式)
void quickSort(vector<int> &vec){
    _quickSort( vec , 0 , vec.size() - 1);
}


//快速排序(使用栈的非递归方式)
//使用栈来模拟递归,栈中保存需要排序元素的边界
void quickSort_Using_Stack(vector<int> &vec){
	stack<int> s;
	//存储第一对边界.
    s.push(0);
    s.push(vec.size()-1);
	//
    while( !s.empty() ){
		//
		int right = s.top(); s.pop();
		int left = s.top(); s.pop();
		int pivot_pos = _partition( vec , left , right );
		//
		if ( pivot_pos - 1 > left){
			s.push( left );
			s.push( pivot_pos - 1 );
		}
		if ( pivot_pos + 1 < right){
			s.push( pivot_pos + 1 );
			s.push( right );
		}
    }
}

void test_push(vector<int> &vec){
	vec.push_back(100);
    vec.push_back(129);
    vec.push_back(88);
    vec.push_back(90);
    vec.push_back(111);
    vec.push_back(1);
}

void test_print_and_clear(vector<int> &vec){
	for(int i = 0 ; i < vec.size() ; i++){
        printf("%d " , vec[i]);
    }
	printf("\n");
	vec.clear();
}

int main(){
    vector<int> vec;
	//quickSort
    test_push(vec);
    quickSort(vec);
    test_print_and_clear(vec);
	//quickSort_Using_Stack
	test_push(vec);
    quickSort_Using_Stack(vec);
    test_print_and_clear(vec);
}

### 回答1: 我们可以使用快速排序法来排序C代码,具体的代码实现可以参考下面的示例:int partition(int arr[], int low, int high) { int pivot = arr[high]; // pivot int i = (low - 1); // Index of smaller element for (int j = low; j <= high- 1; j++) { // If current element is smaller than or // equal to pivot if (arr[j] <= pivot) { i++; // increment index of smaller element swap(&arr[i], &arr[j]); } } swap(&arr[i + 1], &arr[high]); return (i + 1); } /* The main function that implements QuickSort arr[] --> Array to be sorted, low --> Starting index, high --> Ending index */ void quickSort(int arr[], int low, int high) { if (low < high) { /* pi is partitioning index, arr[p] is now at right place */ int pi = partition(arr, low, high); // Separately sort elements before // partition and after partition quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } ### 回答2: 快速排序是一种常用的排序算法,其思想是通过一趟排序将待排序的序列分割成独立的两部分,其中一部分的所有元素都比另一部分小。然后再对这两部分分别进行快速排序,最终得到有序序列。 下面是一个用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[low]; // 选择第一个元素作为基准值 int left = low, right = high; while (left < right) { // 从右向左找第一个比基准值小的元素 while (left < right && arr[right] >= pivot) { right--; } // 从左向右找第一个比基准值大的元素 while (left < right && arr[left] <= pivot) { left++; } // 交换这两个元素的位置 if (left < right) { swap(&arr[left], &arr[right]); } } // 将基准值与 left 和 right 相遇的位置交换 swap(&arr[low], &arr[left]); return left; } // 快速排序 void quickSort(int arr[], int low, int high) { if (low < high) { int pivotIndex = partition(arr, low, high); // 将序列划分为两部分 quickSort(arr, low, pivotIndex - 1); // 对左子序列进行快速排序 quickSort(arr, pivotIndex + 1, high); // 对右子序列进行快速排序 } } int main() { int arr[] = {9, 5, 2, 7, 1, 8, 4, 3, 6}; int length = sizeof(arr) / sizeof(arr[0]); quickSort(arr, 0, length - 1); printf("排序结果:"); for (int i = 0; i < length; i++) { printf("%d ", arr[i]); } return 0; } ``` 以上代码中,`swap`函数用于交换两个元素的位置,`partition`函数用于将数组划分为两部分,并返回基准值的最终位置,`quickSort`函数用于进行快速排序。在`main`函数中,我们定义一个数组并进行快速排序,最后输出排序结果。 该快速排序算法的时间复杂度为O(nlogn),空间复杂度为O(logn)。 ### 回答3: 快速排序是一种常用的排序算法,它的思想是通过选取一个基准元素将待排序序列分成两个子序列,然后递归地对子序列进行排序,最终得到有序的序列。 下面是一个用C语言实现的快速排序代码: ```c #include<stdio.h> // 交换两个元素的位置 void swap(int* a, int* b){ int temp = *a; *a = *b; *b = temp; } // 将基准元素放到正确的位置,并返回它的下标 int partition(int array[], int low, int high){ int pivot = array[low]; // 选取第一个元素作为基准元素 int i = low, j = high; while(i < j){ // 从右往左找到第一个小于基准元素的元素 while(i < j && array[j] >= pivot){ j--; } // 从左往右找到第一个大于基准元素的元素 while(i < j && array[i] <= pivot){ i++; } // 交换找到的两个元素 if(i < j){ swap(&array[i], &array[j]); } } // 将基准元素放到正确的位置 array[low] = array[i]; array[i] = pivot; return i; } // 快速排序算法 void quickSort(int array[], int low, int high){ if(low < high){ int pivotPos = partition(array, low, high); quickSort(array, low, pivotPos - 1); quickSort(array, pivotPos + 1, high); } } int main(){ int array[] = {3, 7, 2, 9, 1, 5, 10, 8, 4, 6}; int n = sizeof(array) / sizeof(array[0]); quickSort(array, 0, n - 1); printf("排序后的数组:"); for(int i = 0; i < n; i++){ printf("%d ", array[i]); } printf("\n"); return 0; } ``` 在这段代码中,`partition`函数用于将基准元素放到正确的位置,并返回它的下标。`quickSort`函数则实现了递归地对子序列进行排序。最后,在`main`函数中,我们定义一个待排序的数组,并调用`quickSort`函数进行排序。最终输出排序后的数组。 快速排序的时间复杂度为O(n log n),其中n是待排序序列的长度。因此,它是一种高效的排序算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值