1 year ago

#387121

test-img

Dustin

Implementing date sort (day, month, year) with quicksort in C++

I tried to sort the dates by implementing quicksort myself. However, the sample output is correct, but when I submit the correct answer, it says incorrect. I don't know where I'm going wrong, so I'm asking this question.

#include <iostream>
#include <vector>

struct Date
{
    int day;
    int month;
    int year;
};

void swap(std::vector<Date>& vec, int a, int b)
{
    Date temp = vec[a];
    vec[a] = vec[b];
    vec[b] = temp;
}
void func(std::vector<Date>& vec, int a, int b)
{
    if (vec[a].year > vec[b].year)
    {
        swap(vec, a, b);
    }
    else if (vec[a].year == vec[b].year && vec[a].month > vec[b].month)
    {
        swap(vec, a, b);
    }
    else if (vec[a].year == vec[b].year && vec[a].month == vec[b].month && vec[a].day > vec[b].day)
    {
        swap(vec, a, b);
    }
}

int partition(std::vector<Date>& vec, int left, int right, int index)
{
    int pivot;
    int value;

    if (index == 2)              // Year is year, month is month, day is day to day, the pivot is initialized by putting an if statement to compare.
        pivot = vec[right].year;
    if (index == 1)
        pivot = vec[right].month;
    if (index == 0)
        pivot = vec[right].day;

    int i = left - 1;

    for (int j = left; j <= right - 1; j++)
    {
        if (index == 2)
            value = vec[j].year;
        if (index == 1)
            value = vec[j].month;
        if (index == 0)
            value = vec[j].day;

        if (value <= pivot)
        {
            i++;

            func(vec, i, j);
        }
    }
    func(vec, i + 1, right);

    return i + 1;
}
void quick_sort(std::vector<Date>& vec, int left, int right, int index)
{
    if (left < right)
    {
        int p = partition(vec, left, right, index);
        quick_sort(vec, left, p - 1, index);
        quick_sort(vec, p + 1, right, index);
    }
}


int main()
{
    int num;
    Date date;

    std::cin >> num; // 1≤ num ≤500,000
    std::vector<Date> vec(num);

    for (int i = 0; i < num; i++)
    {
        std::cin >> vec[i].day >> vec[i].month >> vec[i].year;
    }

    quick_sort(vec, 0, vec.size() - 1, 2); // In order to sort by year, index 2 is added to the quick function.
    quick_sort(vec, 0, vec.size() - 1, 1); // In order to sort by month, index 1 is added to the quick function.
    quick_sort(vec, 0, vec.size() - 1, 0); // In order to sort by day, index01 is added to the quick function.

    for (int i = 0; i < vec.size(); i++)
    {
        std::cout << vec[i].day << " " << vec[i].month << " " << vec[i].year << "\n";
    }

    return 0;
}

Sample input 1

6
20 1 2014
25 3 2010
3 12 2001
18 11 2001
19 4 2015
9 7 2005

Sample output

18 11 2001
3 12 2001
9 7 2005
25 3 2010
20 1 2014
19 4 2015

My output also came out with the sample output, but the problem submission site says it's wrong.

void func(std::vector<Date>& vec, int a, int b)
{
    if (vec[a].year > vec[b].year)
    {
        swap(vec, a, b);
    }
    else if (vec[a].year == vec[b].year && vec[a].month > vec[b].month)
    {
        swap(vec, a, b);
    }
    else if (vec[a].year == vec[b].year && vec[a].month == vec[b].month && vec[a].day > vec[b].day)
    {
        swap(vec, a, b);
    }
}

Maybe something went wrong when calling func from the partition function? Or is the logic wrong in the func function?

algorithm

quicksort

0 Answers

Your Answer

Accepted video resources