C program: please help to implement a program that sorts data in a record by a k
ID: 3776845 • Letter: C
Question
C program: please help to implement a program that sorts data in a record by a key.
The data i have to sort is a 200x7 matrix.
Each row is composed of 7 columns whose elements are
all integer numbers.
The first column contains the record key.
in records.h i have:
int records[200][7]={{47,5061,6392,855,3458,7367,5948},{61,2479… .. .. ..
Unsorted data:
3
21
55
6544
6
78
8
1
886
44
6
7
77
44
2
77
O9
55
3332
45
778
Sorted data:
1
886
44
6
7
77
44
2
77
O9
55
3332
45
778
3
21
55
6544
6
78
8
#include "records.h"
#include <stdio.h>
void bubblesort(int a[], int N){ int i, j, t;
for (i=N-1; i>=1; i--) for(j=1; j<=i; j++)
if (a[j-1]>a[j]){ t=a[j-1];
a[j-1]=a[j];
a[j]=t; }
}
int mian(){
//need to implement
}
3
21
55
6544
6
78
8
1
886
44
6
7
77
44
2
77
O9
55
3332
45
778
Explanation / Answer
Modified bubblesort method for you is:
void bubblesort(int a[][7], int N){ int i, j, t;
for (i=0; i<200; i++)
for(j=0; j<200-i-1; j++)
if (a[j+1][0]<a[j][0]){
for(k=0;k<7;k++){
t=a[j+1][k];
a[j+1][k]=a[j][k];
a[j][k]=t;
}
}
}
Just pass 2D array to this method and it will sort on basis of first column.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.