C++ C++;如何使用基于非比较的方法进行排序

C++ C++;如何使用基于非比较的方法进行排序,c++,arrays,sorting,C++,Arrays,Sorting,-通过学生数组计算每个学校有多少学生。将计数存储在计数数组中 -您可以分配另一个大小等于输入数组的学生数组。您将使用此临时数组保存已排序的元素 -使用计数数组,您可以通过注意学校的起始索引是所有学校的累积学生数来计算排序数组中每个学校的起始索引 前面的学校排成一排。例如:如果a、b、c学校的计数是5、10、12,那么 排序数组中每个学校学生的起始索引为0、5、15 –当您遇到来自一个团队的学生时,您将再次通过学生阵列 将该学生添加到助手数组的右侧部分,并使其递增 部分的索引 -助手数组现在按学校

-通过学生数组计算每个学校有多少学生。将计数存储在计数数组中

-您可以分配另一个大小等于输入数组的学生数组。您将使用此临时数组保存已排序的元素

-使用计数数组,您可以通过注意学校的起始索引是所有学校的累积学生数来计算排序数组中每个学校的起始索引 前面的学校排成一排。例如:如果a、b、c学校的计数是5、10、12,那么 排序数组中每个学校学生的起始索引为0、5、15

–当您遇到来自一个团队的学生时,您将再次通过学生阵列 将该学生添加到助手数组的右侧部分,并使其递增 部分的索引

-助手数组现在按学校和每个学校内的ID进行排序。复制辅助对象 将数组的元素添加到输入数组中

我在最后两个要点上遇到了麻烦。从理论上讲,我理解算法在做什么,但实现代码对我来说很困难

我下面的代码使用一个计数器来跟踪有多少学生进入了由计数器数组索引表示的7所不同学校。然后进行累积计数。我现在需要编写的代码从临时数组的右侧开始,并使用累积值对数组进行排序

 void sortByGroupById2(Student students[], int len) {
    int temp[len];
    int count[7];

        // Creates an array where schools are converted to ints by using schoolToIndex
        for(int i = 0; i < len; i++) {
            temp[i] =schoolToIndex(students[i].getSchool());
            cout << i << " " << temp[i] << endl;
        }


        // Sets every index in count array to 0
        for(int i =0; i < 7;  i++) {
            count[i]=0;
        }

        //Counts the number of student in 7 different schools
        for(int j =0; j < len;  j++) {
          count[temp[j]]++;
        }
        cout << "====" << endl;

        for(int i =0; i < 7;  i++) {
            cout<< i << " "  <<count[i] <<endl;
        }

          cout << "====" << endl;


        // cumulative counts so we get the starting index for each group
        int total = 0;
        for (int i=0; i<7; i++) {
            int tmp = count[i];
            count[i] = tmp +total;
            total += tmp;
          cout << i << " " << count[i] << endl;
        }

        // Sorts into a new array

        for(int i=0; i <7;i++){

        }

    }
void sortByGroupById2(学生[],int len){
内部温度[透镜];
整数计数[7];
//创建一个数组,通过使用schoolToIndex将学校转换为整数
对于(int i=0;i不能只需按照所写的步骤进行操作:

// -You allocate another array of Students with size equal to the input array. 
Student* sorted_students = new Student[len];

// – You do another pass through the student array ....
for (int i = 0; i < len; ++i) 
{
    // and whenever you encounter a student from a school ...
    int key = schoolToIndex(students[i].getSchool());

    // you add that student to the right portion of the helper array ...
    sorted_students[count[key]] = students[i];

    //  and increment that portion's index.
    count[key] += 1;
}

// -The helper array is now sorted by school and by ID within each school. 
// Copy the helper array's elements to the input array.
std::copy(sorted_students, sorted_students + len, students);

// and don't forget to delete!
delete [] sorted_students;
/-分配另一个大小与输入数组相同的学生数组。
学生*已排序的学生=新学生[len];
//–您在学生阵列中进行另一次传递。。。。
对于(int i=0;i
您只是在问这个问题吗?您能解释一下算法的第三个循环吗?这就是我在描述时遇到的问题。请注意,最后一个要点是,“现在,助手数组按学校和每个学校内的ID进行排序”。在助手数组中,仅当原始输入按ID排序时,才会对每个学校分区内的学生ID进行排序。