Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- const int MAX = 1024;
- void insert_sorted(long *sorted, int count, long value);
- /* inserts <value> into <sorted> containing <count> values */
- void insert_sorted (long *sorted, int count, long value)
- {
- int i = 0;
- sorted[count] = value;
- if (count == 0) return;
- for (i = count;i >= 0; i--)
- {
- if (value < sorted[i-1])
- sorted[i] = sorted[i-1];
- else break;
- }
- sorted[i] = value;
- }
- int main ()
- {
- FILE *infile = NULL;
- long sorted[1024];
- long value;
- int count = 0;
- int i = 0;
- infile = fopen ("data.csv", "r");
- if (NULL == infile)
- {
- perror ("cant open!!");
- return -1;
- }
- /* while file not ends */
- while (!feof (infile))
- {
- fscanf (infile, "%ld\n", &value); /* fetch value */
- insert_sorted (sorted, count, value); /* sort */
- ++count; /* increase number of sorted values */
- }
- /* display values */
- printf ("Sorted values : ");
- for (i = 0; i < count; i++ )
- {
- printf ("%ld ", sorted[i]);
- }
- /*cleanup */
- if (infile)
- {
- fclose (infile);
- infile = NULL;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement