In: Computer Science
C programming:
Assume you have the following spreadsheet as example.csv. This .csv file can have more than 1000 lines of data.
time | latitude | longitude | depth | mag |
2020-10-19T23:28:33.400Z | 61.342 | -147.3997 | 12.3 | 1.6 |
2020-10-19T23:26:49.460Z | 38.838501 | -122.82684 | 1.54 | 0.57 |
2020-10-19T23:17:28.720Z | 35.0501667 | -117.6545 | 0.29 | 1.51 |
2020-10-19T22:47:44.770Z | 38.187 | -117.7385 | 10.8 | 1.5 |
2020-10-19T22:42:26.224Z | 54.4198 | -159.9943 | 18.7 | 2.9 |
2020-10-19T22:39:38.900Z | 18.004 | -66.761 | 14 | 2.87 |
Read the spreadsheet which is example.csv file in your new c program and then sort the above data by latitude in ascending order. Use bubble or insertion sort and time it. Please do not hard code anything, especially do not hard code .csv file.
Dear Student I understand you need help with the following program on Insertion Sort/Bubble Sort and arrange them(latitude) in ascending order.Given below is the following program in bubble sort to arrange the latitudes in ascending order.
#include <stdio.h>
void
swap(
int
*xp,
int
*yp)
{
int
temp
= *xp;
*xp = *yp;
*yp =
temp;
}
//The above function basically swaps the lower value of latitude mentioned in the csv file.
// A function to implement bubble sort
void
bubbleSort(
int
arr[],
int
n)
{
int
i,
j;
for
(i = 0; i
< n-1; i++)
//
Last i elements are already in place
for
(j = 0; j < n-i-1; j++)
if
(arr[j] > arr[j+1])
swap(&arr[j],
&arr[j+1]);
}
//Given Below is the function to print an array
void
printArray(
int
arr[],
int
size)
{
int
i;
for
(i=0; i < size; i++)
printf
(
"%d
"
, arr[i]);
printf
(
"\n"
);
}
int
main()
{
int
arr[] = {
61.342,
38.838501,
35.0501667, 38.187, 54.4198,18.004};
int
n
=
sizeof
(arr)/
sizeof
(arr[0]);
bubbleSort(arr,
n);
printf
(
"Sorted
array: \n"
);
printArray(arr,
n);
return
0;
}
Output:-
Sorted array: 18.004 35.0501667 38.187 38.838501 54.4198 61.342
This program gives the following Output Hope I resolved your problem using the following program given.I hope this will help you understand the topic better.