1

I'm working on a simple program that, for now, writes inputted names and marks to two separate arrays, with the array counters synchronised. For example, the name in entry #3 on the first array corresponds to the marks in entry #3 on the second array. My code up to now is like this:

import java.io.*;

public class project
{
    int ctr;
    int ctr1;
    int pos;
    int temp;
    int max;

    public static void main(String args[]) throws IOException
    {
        new project().input();
    }

    void input() throws IOException
    {
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
        System.out.println ("How many children?");
        int n = Integer.parseInt(obj.readLine());
        String a[] = new String[n];
        int b[] = new int[n];
        for(ctr=0;ctr<n;ctr++)
        {
            System.out.println("Enter name");
            a[ctr]=obj.readLine();
            System.out.println("Enter marks");
            b[ctr]=Integer.parseInt(obj.readLine());
        }
    }
}

I now want to alphabetically sort the list of names, so that when the array is printed in order, the names are alphabetically arranged from A to Z.

What's the best way to sort the string array alphabetically without using any functions like compareTo() or sort(), but rather with bubble or exchange sorting using nested loops?

How can I synchronise the sorting, so that as names get shuffled in the first array, the corresponding marks get shuffled identically, keeping the data correct?

2
  • Hi, beside educational purposes or very specific domains (like bio-engineering) there is no best way to sort standard data types without the build-in sort() method. If it is for educational purposes check out the books from Knuth en.wikipedia.org/wiki/The_Art_of_Computer_Programming. If you have a specific domain, you should explain in more detail.
    – Wintermute
    Apr 21, 2014 at 16:24
  • 2
    To keep them in sync, just perform the exact same operations on the second array that you perform on the first array. It would be better to create a Student class that combined the names and marks into a single object, and sort them together, but it looks like this is for a class project. Apr 21, 2014 at 16:24

2 Answers 2

0

First, I would advise against doing this at all. I would make a class holding a name and a mark, store this in the array, and sort the resulting array.

Then, if you really want to do that, in bubble sort for example, when the normal code says to exchange elements i and j in the array you are sorting, simply also exchange elements i and j in the other array. This will do the job.

0

The code is in c++ but you can translate it to java and change it in accordance to your need. I faced similar problem while i was trying to write code for Priority based CPU scheduling's non pre emptive version. Trying to synchronize the sorting of two

#include <bits/stdc++.h>
using namespace std;
  
// Function to sort character array b[]
// according to the order defined by a[]
void pairsort(int a[], char b[], int n)
{
    pair<int, char> pairt[n];
  
    // Storing the respective array
    // elements in pairs.
    for (int i = 0; i < n; i++) 
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
  
    // Sorting the pair array.
    sort(pairt, pairt + n);
      
    // Modifying original arrays
    for (int i = 0; i < n; i++) 
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}
  
// Driver function
int main()
{
    int a[] = {2, 1, 5, 4, 9, 3, 6, 7, 10, 8};
    char b[] = {'A', 'B', 'C', 'D', 'E', 'F', 
                         'G', 'H', 'I', 'J'};
                           
    int n = sizeof(a) / sizeof(a[0]);
      
    // Function calling
    pairsort(a, b, n);
  
    for (int i = 0; i < n; i++)
        cout << a[i] << " ";
    cout << endl;
  
    for (int i = 0; i < n; i++)
        cout << b[i] << " ";
          
    return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.