为了账号安全,请及时绑定邮箱和手机立即绑定

快速排序用c实现出现了问题?

快速排序用c实现出现了问题?

阿乐2 2017-03-29 16:06:28
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <stack>#include <vector>using namespace std;// Exchange two elements in an arrayvoid exchange(int arr[], int i, int j) { int tmp=arr[i]; arr[i]=arr[j]; arr[j]=tmp;}// Partition an array and return the partition pointint partition(int arr[], int begin, int end){    int pivot = arr[end];    int i = begin-1; int j; for(j=begin;j<end;j++) { if(arr[j]<=pivot) { i=i+1; exchange(arr,i,j); } } exchange(arr,i+1,end);     return i+1;}void quickSort(int arr[], int begin, int end) { int q=partition(arr,begin,end); if(begin<end) { quickSort(arr,begin,q-1); quickSort(arr,q+1,end); }}void testQuickSort() {    srand (time(NULL));    int a[10], i = 0;;    printf("Before sorting, array a is\n");    for (i = 0; i < sizeof(a)/sizeof(int); ++i) {        a[i] = rand()%100;        printf("%d ", a[i]);    }    quickSort(a, 0, sizeof(a)/sizeof(int) - 1);    printf("\nAfter sorting, array a is\n");    for (i = 0; i < sizeof(a)/sizeof(int); ++i)        printf("%d ", a[i]);    printf("\n");}int main() {    testQuickSort();    return 0;}
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 1257 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号