使用不使用负数的指针进行选择排序

时间:2017-04-05 04:36:25

标签: c++ selection-sort

我有以下代码:

// selectionSortUsingPointers:  Using pointers, this function sorts
// in ascending order the n-element integer array using a selection sort.
void selectionSortUsingPointers(int *array, int n){

    int *currentSmall = array;
    int temp;

    for(int *i = array; i < array+n; i++){
        for(int *a = i; a < array+n; a++){
            if(*a < *currentSmall){
                currentSmall = a;
            }
        }

        if(*i > *currentSmall){
            temp = *i;
            *i = *currentSmall;
            *currentSmall = temp;
        }
    }
}

适用于:array{2305, 1361, 1362, 1331, 1341, 1351}。但是,如果我尝试使用负数:array{-2305, -1361, -1362, -1331, -1341, -1351},它似乎无法正确排序,而是将其反馈给我:array{-1361, -2305, -1331, -1362, -1351, -1341}。这显然没有正确排序,我一直在试图找出原因。有什么建议吗?

0 个答案:

没有答案