简体   繁体   English

Quicksort需要帮助才能完成此操作

[英]Quicksort Need help to finish this

Please help me I need to finish this The program works but it wont sort by the age it should sort the records according to the age please help. 请帮助我,我需要完成此程序,但是该程序不会按年龄进行排序,而应根据年龄对记录进行排序,请帮助。 i cant get it worked 我不能使它工作

This is what ive done so far. 到目前为止,我已经做到了。 I cant figure out whats wrong 我不知道怎么了

#include <cstdlib>
#include <iostream>

using namespace std;

struct contact
{

    char lastname[30];
    char firstname[30];
    int age;
    int cnumber;
}
c[20];

void quickSort(int arr[], int left, int right) {
    int i = left, j = right;
    int tmp;
    int pivot = arr[(left + right) / 2];

    /* partition */
    while (i <= j) {
        while (arr[i] < pivot)
            i++;
        while (arr[j] > pivot)
            j--;
        if (i <= j) {
            tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            i++;
            j--;
        }
    };

    /* recursion */
    if (left < j)
        quickSort(arr, left, j);
    if (i < right)
        quickSort(arr, i, right);
}
void addContact(int ctr)
{
    cout<<"ADD CONTACT"<<endl;
    cout<<"LAST NAME: "<<endl;
    cin>>c[ctr].lastname;
    cout<<"FIRST NAME: "<<endl;
    cin>>c[ctr].firstname;
    cout<<"AGE: "<<endl;
    cin>>c[ctr].age;
    while (c[ctr].age >= 100 || c[ctr].age <= 0)
    {
        cout<<"Input Age again: ";
        cin>>c[ctr].age;
    }
    cout<<"CONTACT NUMBER: "<<endl;
    cin>>c[ctr].cnumber;
    while (c[ctr].cnumber > 9999999 || c[ctr].cnumber < 1000000)
    {
        cout<<"Input Number Again: ";
        cin>>c[ctr].cnumber;
    }
    system("cls");
}
void display(int a)
{ cout<<"RECORDS"<<endl;
    for(int i=0;i<a;i++)
    {
        cout<<"\n";
        cout<<"LAST NAME: ";
        cout<<c[i].lastname<<endl;
        cout<<"FIRST NAME: ";
        cout<<c[i].firstname<<endl;
        cout<<"AGE: ";
        cout<<c[i].age<<endl;
        cout<<"CONTACT NUMBER: ";
        cout<<c[i].cnumber<<endl;
    }

}
int main(int argc, char *argv[])
{

    int choice, loop=0, tmp;
    while (choice!=4)
    {
        cout<<"\n";
        cout<<"CHOOSE"<<endl;
        cout<<"1.Add contacts: "<<endl;
        cout<<"2.Display "<<endl;
        cin>>choice;
        switch(choice)

        { case 1:
                addContact(loop);
                loop++;
                break;
            case 2:
                system("cls");
                display(loop);
                break;
            case 3:
                quickSort(&c[loop].age,loop,0);
                display(loop);
                break;
            default:
                cout<<"Invalid";
        }

    }


    return 0;
}

Your quickSort looks correct, but it sorts an array of int . 您的quickSort看起来正确,但是它对int数组进行了排序。 You're calling it this way: 您这样称呼它:

quickSort(&c[loop].age,loop,0);

A pointer to an int member of some element of an array of structures is not a pointer to the beginning of an array of that member of all of the structures (which doesn't exist). 指向结构数组中某个元素的int成员的指针不是指向所有结构中该成员的数组的开头的指针(不存在)。

You have a choice; 您可以选择; either rewrite quickSort to compare contacts , or else give contact a comparator and make quickSort a template function. 要么重写quickSort以比较contacts ,要么给contact提供比较器,并使quickSort成为模板功能。

EDIT: 编辑:

Rewriting quickSort to compare contacts is pretty straightforward: 重写quickSort以比较contacts非常简单:

void quickSort(contact arr[], int left, int right)
{
  int i = left, j = right;
  contact tmp;
  contact pivot = arr[(left + right) / 2];

  /* partition */
  while (i <= j) {
    while (arr[i].age < pivot.age)
      i++;
    while (arr[j].age > pivot.age)
      j--;
    if (i <= j) {
      tmp = arr[i];
      arr[i] = arr[j];
      arr[j] = tmp;
      i++;
      j--;
    }
  };

if (left < j)
  quickSort(arr, left, j);
if (i < right)
  quickSort(arr, i, right);
}

All you have to do is make sure that contact has an assignment operator that works correctly, so that you can do things like arr[i] = arr[j] ... 您要做的就是确保contact具有正确运行的赋值运算符,以便您可以执行arr[i] = arr[j]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM