-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear-search-improved.c
103 lines (82 loc) · 2.53 KB
/
linear-search-improved.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
/* A program that generates a random array of specifed sized and them sort them using
sort, bubble and merge*/
//#define TEST_NOW true
#define LEN 50
void create_random_values_into_array(int *array_addres, int array_size, int range_of_value)
{
int current_index, random_number;
srand(time(NULL));
for (current_index = 0; current_index < array_size; current_index++)
{
random_number = rand() % range_of_value;
array_addres[current_index] = random_number;
}
}
// TO DO
// Create a binary search the condition
// TO DO
// Change the name of the function because linear search is to find a value in that case
// We are using the Selection Sort to ordenate that array
void linear_order_asc_and_desc_array(
int *array_addres, int size, bool is_cresent_param)
{
bool swap_now = false;
int current_idx, next_idx, aux, first_value, second_value;
for (current_idx = 0; current_idx < size; current_idx++)
{
for (next_idx = current_idx+1; next_idx < size; next_idx++)
{
first_value = array_addres[current_idx];
second_value = array_addres[next_idx];
if (is_cresent_param)
{
swap_now = first_value > second_value;
}
else
{
swap_now = first_value < second_value;
}
if (swap_now)
{
aux = first_value;
array_addres[current_idx] = second_value;
array_addres[next_idx] = aux;
}
}
}
}
int main(int argc, char **argv)
{
FILE *file_output;
int current_index;
char caracter;
// argv[0] filename
// argv[1] total values
// argv[2] filename
if (argc != 3){
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
}
int total_values = atoi(argv[1]);
int array[total_values];
create_random_values_into_array(array, total_values, 100);
if ((file_output = fopen(argv[2], "w")) == NULL)
{
fprintf(stderr, "I couldn't open the file \"%s\"\n", argv[2]);
exit(EXIT_FAILURE);
}
for (current_index = 0; current_index<total_values; current_index++)
{
fprintf(file_output, "%d\n", array[current_index]);
}
// Generate randomic numbers between 0 and 99
if (fclose(file_output) != 0)
{
fprintf(stderr, "Error in closing file!\n");
}
return 0;
}