| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1375 人关注过本帖
标题:求大神指教,我这个快排哪里出了问题运行不出结果
只看楼主 加入收藏
dyx20150311
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2019-3-25
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
求大神指教,我这个快排哪里出了问题运行不出结果
// sort.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

// 测试快排.cpp : 定义控制台应用程序的入口点。
//


#include<vector>
#include<iostream>
using namespace std;
//6. 快速排序  O(NlogN)   最差O(N^2)  最好O(logN)
void swap(int *arr, int a, int b) {
    if (a == b)
        return;
    arr[a] = arr[a] ^ arr[b];
    arr[b] = arr[a] ^ arr[b];
    arr[a] = arr[a] ^ arr[b];
}
int* partition(int* arr, int l, int r) {
    int less = l - 1;
    int more = r;
    while (l < more) {
        if (arr[l] < arr[r]) {
            swap(arr, ++less, l++);
        }
        else if (arr[l] > arr[r]) {
            swap(arr, --more, l);
        }
        else { l++; }
    }

    swap(arr, more, r);
    int* a = new int[2];
    a[0] = less + 1;
    a[1] = more;
    cout << "less" << less << " " << "more" << more << endl;

    return a;
}
void quickSort(int* arr, int l, int r) {
    int end = _msize(arr) / sizeof(arr);
    //int *p = new int[2];
    if (l < r) {
        swap(arr, l + (int)(rand() % (r - l + 1)), r);

        int *p = partition(arr, l, r);
        cout << "p[0]" << p[0] << " " << "p[1]" << p[1] << endl;
        quickSort(arr, l, p[0] - 1);
        quickSort(arr, p[1], r);
    }
}
void quickSort(int* arr) {
    int end = _msize(arr) / sizeof(arr);
    if (arr == NULL || end < 2) {
        return;
    }
    quickSort(arr, 0, end - 1);
}



int main() {
    int arr[] = { 7, 4, 9, 2, 8, 6, 1, 5, 3, 0 };
    int len = sizeof(arr) / sizeof(arr[0]);
    quickSort(arr);
    for (int i = 0; i < len; ++i) {
        cout << arr[i] << endl;
    }
   
    return  0;
}

搜索更多相关主题的帖子: 快排 int return end sizeof 
2019-03-25 21:20
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
_msize(arr) 你知道是干什么的么?就乱用


    arr[a] = arr[a] ^ arr[b];
    arr[b] = arr[a] ^ arr[b];
    arr[a] = arr[a] ^ arr[b];
就看得出来,你不走正道。
2019-03-26 08:51
朱煜宸
Rank: 2
等 级:论坛游民
帖 子:6
专家分:10
注 册:2019-3-27
收藏
得分:10 
_msize没有这个函数,rand()在的头文件没有,rand()在<stdlib.h>里。
2019-03-27 21:17
快速回复:求大神指教,我这个快排哪里出了问题运行不出结果
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014898 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved