如何3阵列一起用C排序 [英] how to sort 3 arrays together in C
问题描述
我有3个数组: A,B,和 C
char A[6][10];
int B[6];
int C[6];
欲上述3阵列(其具有元件的相同的N#)被同时排序。如果我有点阵列的乙从最低到最高值,然后我想阵列的 A 和 C 遵循 B的格局。这也许更好explaines下面的例子。
I want the above 3 arrays (which have the same n# of elements) to be sorted simultaneously . If i sort array B from lowest to highest value then i want arrays A and C to follow B's pattern. this maybe better explaines with an example below.
下面我有尝试这个code,我的问题是如何编辑/修复我的code做在下面描述simulataneous排序这个功能吗
I have a code below attempting this, my question is how can i edit/fix my code to do this function of simulataneous sorting which is described below?
未排序列表:
A[6] B[6] C[6]
John 12 2
David 30 1
Michael 12 2
Steve 12 1
Sam 19 2
Claire 12 1
然后我要排序从最低到最高,而 A [I] &功放的乙[I] ; C [I] 按 B的排列[I]
A[6] B[6] C[6]
John 12 2
Michael 12 2
Steve 12 1
Claire 12 1
Sam 19 2
David 30 1
这时如果在 b全等/相同号码[I] ,然后我要排序的从最低到最高 >引起的 A [I] 和乙[I] 可根据排序的 C [I]:
Then if there are identical/same numbers in B[i], then i want to sort C[i] from lowest to highest causing A[i] and B[i] to be sorted according to C[i]:
A[6] B[6] C[6]
Steve 12 1
Claire 12 1
John 12 2
Michael 12 2
Sam 19 2
David 30 1
那么,如果从2名的 A [I] 在 B〔I] 和 C [I] ,然后排序<强的相似性> A [I] 按字母顺序排列。
Then if 2 names from A[i] have similarities in B[i] and C[i], then sort A[i] in alphabetical order.
A[6] B[6] C[6]
Claire 12 1
Steve 12 1
John 12 2
Michael 12 2
Sam 19 2
David 30 1
我写了一个code,试图做到这一点,但我具有造成1阵列simulatanuosly排序到另一个阵列有点挣扎。
任何人都可以指出我在做什么错误或描述做这类排序更好/最好的方法?
#include <stdio.h>
main()
{
char A[6][10]={"John","David","Michael","Steve","Sam","Claire"};
int B[6] = {12,30,12,12,19,12};
int C[6] = {2,1,2,1,2,1};
int i=0;
int true=1;
for (i=0;i<6;i++)
{
printf("%s\t%d\t%d\n",A[i],B[i],C[i]);
}
while(1)
{
true = 0;
for (i=0;i<6;i++){
if(B[i]> B[i+1]){
int temp = B[i];
B[i] = B[i+1];
B[i+1] = temp;
C[i]= C[i+1];
swapped = 1;
}
}
if (true==0){
break;
}
}
printf("\nnew list\n");
for (i=0;i<6;i++)
{
printf("%s\t%d\t%d\n",A[i],B[i],C[i]);
}
}
code的输出:
Output of code:
John 12 2
David 30 1
Michael 12 2
Steve 12 1
Sam 19 2
Claire 12 1
new list
John 12 2
David 12 2
Michael 12 1
Steve 12 1
Sam 19 1
Claire 30 1
感谢
推荐答案
它的更好的取代的索引(或指针),而不是在这样的情况下,以替换每个元素
it's better to replace the index(or pointer) rather than to replace each element in such a case.
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool isDes(int x, int y, char A[][10], int B[], int C[]){
return B[x]>B[y] ||
B[x]==B[y] && (C[x]>C[y] || C[x]==C[y] && strcmp(A[x], A[y])>0);
}
int main(){
char A[6][10]={"John","David","Michael","Steve","Sam","Claire"};
int B[6] = {12,30,12,12,19,12};
int C[6] = {2,1,2,1,2,1};
int index[6] = { 0,1,2,3,4,5};//prepare an array of index
int i=0;
bool swapped;
for (i=0;i<6;i++){
printf("%s\t%d\t%d\n",A[i],B[i],C[i]);
}
while(1){
swapped = false;
for (i=0; i<6-1;i++){//i<6 : bad because when i==5 , X[i+1] is out-of-range access
if(isDes(index[i], index[i+1], A, B, C)){ //check needs to be replaced.
int temp = index[i];
index[i] = index[i+1];
index[i+1] = temp;
swapped = true;
}
}
if (swapped==false){
break;
}
}
printf("\nnew list\n\n");
for (i=0;i<6;i++){
printf("%s\t%d\t%d\n",A[index[i]],B[index[i]],C[index[i]]);//indirect reference by index
}
return 0;
}
这篇关于如何3阵列一起用C排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!