C++ 使用选择排序按最小数字排序

C++ 使用选择排序按最小数字排序,c++,sorting,C++,Sorting,我的任务是使用选择排序按数字的最小数字对数组进行排序。例如:61

我的任务是使用选择排序按数字的最小数字对数组进行排序。例如:61<35,因为1<3。如果数字重合,我必须打印2位中最小的一位。 这是我的代码:

    #include <iostream>
#include <time.h>

using namespace std;


int smallestOfTwo(int a, int b) {
    if (a < b) return a;
    else return b;
}
int smallestDigit(int a) {
    int smallDigit = 9;
    while (a != 0) {
        if (a % 10 < smallDigit) {
            smallDigit = a % 10;
        }
        a /= 10;
    }
    return smallDigit;
}
void selectionSort(int arr[], const int size) {
    for (int i = 0; i < size - 1; i++) {
        int min = i;
        for (int j = i + 1; j < size; j++) {
            if (smallestDigit(arr[j]) == smallestDigit(arr[min])) min = smallestOfTwo(j, min);
            else if (smallestDigit(arr[j]) < smallestDigit(arr[min])) min = j;
    }
        if (min != i) {
            swap(arr[min], arr[i]);
        }
    }
}

int main() {
    srand(time(NULL));
    const int size = 5;
    int arr[size];
    for (int i = 0; i < size; i++) {
        arr[i] = rand() % 201;
        cout << arr[i] << " ";
    }
    cout << "\n--------------------------------------------------------------------------------------\n";
    selectionSort(arr, size);
    for (int i = 0; i < size; i++) cout << arr[i] << " ";
    return 0;
}
#包括
#包括
使用名称空间std;
int smallestOfTwo(int a,int b){
如果(acout26和27都有最小的数字2,因此它们是等价的,可以是结果中的任意顺序。为了保持原始顺序,您可以尝试查找“稳定排序”算法。为了返回到第二小数字、第三小数字等。您可以计算每个数字并进行比较

struct digit_counts {
    int counts[10];
};

digit_counts convert(int a) {
    digit_counts out = {};
    while (a != 0) {
        ++out.counts[a % 10];
        a /= 10;
    }
    return out;
}

bool operator<(const digit_counts& dc1, const digit_counts& dc2) {
    for (int i = 0; i < 10; ++i) {
        if (dc1.counts[i] > dc2.counts[i]) {
            return true;
        }
        else if (dc2.counts[i] > dc1.counts[i]) {
            return false;
        }
    }
    return false;
}
struct digit\u计数{
整数计数[10];
};
数字计数转换(整数a){
数字_计数={};
while(a!=0){
++out.计数[a%10];
a/=10;
}
返回;
}
布尔运算符dc2.计数[i]){
返回true;
}
else if(dc2.counts[i]>dc1.counts[i]){
返回false;
}
}
返回false;
}
如果您想使用两者中较小的一个,那么编写您自己的比较函数

bool special_less_than(int a, int b) {
    int small_digit_a = smallestDigit(a);
    int small_digit_b = smallestDigit(b);
    if (small_digit_a < small_digit_b) {
        return true;
    }
    else if (small_digit_a > small_digit_b) {
        return false;
    }
    else {
        return a < b;
    }
}
bool special_小于(int a,int b){
int small_digit_a=最小数字(a);
int small_digit_b=最小数字(b);
if(小数位a<小数位b){
返回true;
}
否则如果(小数位a>小数位b){
返回false;
}
否则{
返回a
。除此之外,不要使用随机数据——使用实际导致问题的数据。您不想使用随机数据来调试问题的原因是,每次运行程序时,随机数据都会移动目标。你知道的调试数据首先会出现问题,当你弄明白了,然后你会引入随机数据。是的,26和27都有最小的数字2,但在这种情况下,我必须打印最小的数字,也就是26@YungHurricane不要只分离密钥获取(
smallerDigit
),而是分离整个比较。我编辑了我的答案。我明白你的意思,但我不太确定如何在我的代码中实现这一点。那么我应该如何更改selectionSort函数中嵌套for循环中的代码呢?@code代替
smallestDigit(arr[j])
write
special\u小于(arr[j],arr[min])
,您可以编写
而不是相等比较!(特殊小于(arr[j],arr[min])&&!(特殊值小于(arr[min],arr[j])