如何在插入排序中保持数据配对在一起

huangapple 未分类评论50阅读模式
英文:

How to Keep Data Paired Together in Insertion Sort

问题

我一直在处理来自文件的数据,并将它们配对在一起。我需要对数据进行字母排序,同时保持它们配对在一起。我已经成功地使用冒泡排序完成了这个任务:

for (int i = 1; i <= count; i++) {
    for (int j = 0; j < count - 1; j++) {
        if (email[j].compareToIgnoreCase(email[j + 1]) > 0) {
            temp1 = email[j];
            email[j] = email[j + 1];
            email[j + 1] = temp1;

            temp2 = fname[j];
            fname[j] = fname[j + 1];
            fname[j + 1] = temp2;

            temp3 = lname[j];
            lname[j] = lname[j + 1];
            lname[j + 1] = temp3;

            temp4 = city[j];
            city[j] = city[j + 1];
            city[j + 1] = temp4;

            temp5 = age[j];
            age[j] = age[j + 1];
            age[j + 1] = temp5;
        }
    }
}

我得到了如下输出:
Email:
bobbarley@gmail.com
First Name:
Bob
Last Name:
Barley
City:
Vancouver
Age:
13
Email:
felixfixed@gmail.com
First Name:
Felix
Last Name:
Fixed
City:
Boston
Age:
24
Email:
joejake@gmail.com
First Name:
Joe
Last Name:
Jake
City:
Toronto
Age:
32

另一方面,我有一个插入排序,但是无法保持数据配对。以下是我的代码:

for (int i = 1; i < count; i++) {
    String current = fname[i];
    int j = i - 1;

    while (j >= 0 && current.compareToIgnoreCase(fname[j]) < 0) {
        fname[j + 1] = fname[j];
        j--;
    }
    fname[j + 1] = current;
}

使用这部分代码,我得到了如下输出:

First Name:
Bob
Last Name:
Jake
City:
Toronto
Age:
32
Email:
joejake@gmail.com
First Name:
Felix
Last Name:
Barley
City:
Vancouver
Age:
13
Email:
bobbarley@gmail.com
First Name:
Joe
Last Name:
Fixed
City:
Boston
Age:
24
Email:
felixfixed@gmail.com

英文:

I've been working with data from a file and keeping them paired together. I need to sort the data alphabetically while keeping them paired together at the same time. I have successfully done it with bubble sort here:

	 for (int i = 1; i &lt;= count; i++)
	 {
		 for (int j = 0; j &lt; count - 1; j++)
		 {
			 if (email [j].compareToIgnoreCase (email [j + 1]) &gt; 0)
			 {
				 temp1 = email [j];
				 email [j] = email [j + 1];
				 email [j + 1] = temp1;
				 
				 temp2 = fname [j];
				 fname [j] = fname [j + 1];
				 fname [j + 1] = temp2;
				 
				 temp3 = lname [j];
				 lname [j] = lname [j + 1];
				 lname [j + 1] = temp3;
				 
				 temp4 = city [j];
				 city [j] = city [j + 1];
				 city [j + 1] = temp4;
				 
				 temp5 = age [j];
				 age [j] = age [j + 1];
				 age [j + 1] = temp5;
			 }
		 }
	 }
}

I get an output like this:
Email:
bobbarley@gmail.com
First Name:
Bob
Last Name:
Barley
City:
Vancouver
Age:
13
Email:
felixfixed@gmail.com
First Name:
Felix
Last Name:
Fixed
City:
Boston
Age:
24
Email:
joejake@gmail.com
First Name:
Joe
Last Name:
Jake
City:
Toronto
Age:
32

On the other hand I have an insertion sort, but I can't keep the data together. Here is my code:

for (int i = 1; i &lt; count; i++)
	{
		String current = fname [i];
		int j = i - 1;
		
		while (j &gt;= 0 &amp;&amp; current.compareToIgnoreCase (fname [j]) &lt; 0)
		{
			fname [j + 1] = fname [j];
			j--;
		}
		fname [j + 1] = current;
	}

With this part, I get this output:

First Name:
Bob
Last Name:
Jake
City:
Toronto
Age:
32
Email:
joejake@gmail.com
First Name:
Felix
Last Name:
Barley
City:
Vancouver
Age:
13
Email:
bobbarley@gmail.com
First Name:
Joe
Last Name:
Fixed
City:
Boston
Age:
24
Email:
felixfixed@gmail.com

答案1

得分: 0

你通过比较邮件和其他字段来执行冒泡排序,但在插入时只比较了名字,为什么?也许在插入时不应与其他字段进行比较,这样它会被排序。

for (int i = 1; i < count; i++)
{
    String current = fname[i];
    int j = i - 1;

    while ((j >= 0 && current.compareToIgnoreCase(fname[j]) < 0) && j >= 0 && current.compareToIgnoreCase(email[j]....))
    {
        fname[j + 1] = fname[j];
        j--;
    }
    fname[j + 1] = current;
}
英文:

You perform the ordering by bubbles comparing by mail and then your other fields, but the insert only compares the name why? perhaps you should not compare in the insert with the others so that it will be ordered

{for (int i = 1; i &lt; count; i++)
    String current = fname [i];
    int j = i - 1;

    while ((j &gt;= 0 &amp;&amp; current.compareToIgnoreCase (fname [j]) &lt; 0) and j&gt;=0 &amp;&amp; current.compareToIgnoreCase (email[j]....)
    {
        fname [j + 1] = fname [j];
        j--;
    }
    fname [j + 1] = current;
}

huangapple
  • 本文由 发表于 2020年4月11日 10:45:59
  • 转载请务必保留本文链接:https://java.coder-hub.com/61151490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定