简体   繁体   中英

sorting string array with int

#include <iostream>
using namespace std;

void selectionSort (float arr[], int n) {
    int i, j;
    for (i = 0; i < n; ++i) {
        for (j = i+1; j < n; ++j) {
            if (arr[i] > arr[j]) {
                arr[i] = arr[i] + arr[j];
                arr[j] = arr[i] - arr[j];
                arr[i] = arr[i] - arr[j];
            }


        }

    }
}


int main() {
    int n = 3;

    string name[n];
    float arr[n];

    for (int i = 0; i < n; i++) {
        cout << "Name of runner " << i+1 << ": ";
        cin >> name[i];

        cout << "Runner " << i+1 << "'s finishing time: ";
        cin >> arr[i];
        cout << endl;
    }


    selectionSort(arr, n);

    cout << "1st place: " << name[0] << "\t" << arr[0] << endl;
    cout << "2nd place: " << name[1] << "\t" << arr[1] << endl;
    cout << "3rd place: " << name[2] << "\t" << arr[2] << endl;
}

So when I enter the name and finishing time, only the finishing time gets sorted into order from fastest to slowest. I need help getting the runner name matched with the finished time.

Instead of two place holders name[3] and arr[3] try using vector of pairs something like below:

std::vector<std::pair<int,string>> RunnerPairVector;

Then you can use sort to sort your pairs based on finish time like below

std::sort(RunnerPairVector.begin(), RunnerPairVector.end());

Sort names also corresponding to their finishing times.

#include <iostream>
using namespace std;

// Pass name array also to access it..
void selectionSort (string name[], float arr[], int n) {
    int i, j;
    for (i = 0; i < n; ++i) {
        for (j = i+1; j < n; ++j) {
            if (arr[i] > arr[j]) {
                arr[i] = arr[i] + arr[j];
                arr[j] = arr[i] - arr[j];
                arr[i] = arr[i] - arr[j];
                // Swap names also corresponding to their finishing times.
                string temp = name[i];
                name[i] = name[j];
                name[j] = temp;
            }


        }

    }
}


int main() {
    int n = 3;

    string name[n];
    float arr[n];

    for (int i = 0; i < n; i++) {
        cout << "Name of runner " << i+1 << ": ";
        cin >> name[i];

        cout << "Runner " << i+1 << "'s finishing time: ";
        cin >> arr[i];
        cout << endl;
    }


    selectionSort(name, arr, n);

    cout << "1st place: " << name[0] << "\t" << arr[0] << endl;
    cout << "2nd place: " << name[1] << "\t" << arr[1] << endl;
    cout << "3rd place: " << name[2] << "\t" << arr[2] << endl;
}

What you are doing wrong here is that you are sorting the time array but not making respective changes to the name array. so you can pass the name array also along with time array and when you swap the time, at the same time swap the corresponding names. something like this..

void selectionSort(float arr[], string name[], int n) {
  int i, j;
  string temp;
  for (i = 0; i < n; ++i) {
    for (j = i + 1; j < n; ++j) {
      if (arr[i] > arr[j]) {
        arr[i] = arr[i] + arr[j];
        arr[j] = arr[i] - arr[j];
        arr[i] = arr[i] - arr[j];
        //name swapping
        temp = name[i];
        name[i] = name[j];
        name[j] = temp;
      }

    }

  }
}

and one more thing is that your function not returning this changed array. so either you return this changed array or make this time and name array globally accessible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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