[英]how to sort two arrays together where the first one is sorted using bubble sort?
我有兩個數組,一個是持有學生ID的編號,第二個是持有成績的..我想以一種排序的方式向該成績的數組中插入一個成績..在此,我假設之前的數組已排序。 我的問題是我想讓學生的數組也根據年級的數組排序。
成績數組= [100 90 80]
students_id array = [11 22 33]
我想插入ID為44的95年級,那么我將有:
成績數組= [100 95 90 80]
students_id array = [11 44 22 33]
//關於如何執行此操作的任何想法? 我想我可能將成績插入數組,然后對第一個數組使用冒泡排序,但是我也不知道如何保持另一個排序:\\
void insertGrade(int* grades, int* students_id, int id , int grade){
}
void bubble_sort(int* array, int n)
{
int c, d, t;
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
/* Swapping */
t = array[d];
array[d] = array[d+1];
array[d+1] = t;
}
}
}
}
將第二個數組傳遞給函數。 在交換array1中的項目的同時交換array2中的項目。
void bubble_sort(int* array, int* array2, int n)
{
int c, d, t;
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
/* Swapping */
t = array[d];
array[d] = array[d+1];
array[d+1] = t;
t = array2[d];
array2[d] = array2[d+1];
array2[d+1] = t;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.