-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting_main.c
More file actions
156 lines (138 loc) · 3.67 KB
/
sorting_main.c
File metadata and controls
156 lines (138 loc) · 3.67 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//------------------------------------------------------------
// Sorting_main
// establish the main (menu driven) program to help with
// project 1.
//
// compile: gcc -Wall -Werror -O3 sorting.c sorting_main.c -o proj1
//------------------------------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <time.h>
#include "sorting.h"
#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC 1000000
#endif
#define MAXFILELEN (50) // length of string (path and filename)
// functions
int main (int Argc, char **Argv)
{
// main program - menu for functions with using database
double cstart, cend;
int Response = 0;
int Saved = 0;
int Size = 0;
double N_Comp = 0;
double N_Move = 0;
long *Array = NULL;
char Filename[MAXFILELEN] = "";
while (1)
{
printf("\n");
printf("TEST MENU\n");
printf("1. Load array from file\n");
printf("2. Save array to file\n");
printf("3. Shell Sort (Insertion)\n");
printf("4. Improved Bubble Sort\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d",&Response);
getchar();
if (Response == 5) // quit program
{
if (Array != NULL)
{
printf("Removing and deallocating array\n");
free((void *)Array);
Array = NULL;
printf("done!\n");
}
return (OK);
}
if (Response == 1) // load file
{
if (Array != NULL)
{
printf("\nRemoving and deallocating array\n");
free((void *)Array);
Array = NULL;
printf("done!\n");
}
printf("\nEnter input file (including path): ");
scanf("%s", Filename);
Array = Load_File (Filename, &Size);
if (Size <= 0)
{
printf("\nError in inputs, file not loaded..\n");
if (Array != NULL)
{
printf("Removing and deallocating array\n");
free((void *)Array);
Array = NULL;
printf("done!\n");
}
}
else
{
printf("\nLoaded %d long integers\n", Size);
}
}
if (Response == 2) // save file
{
if (Array == NULL)
{
printf("\nMust load in data from file (option 1) before saving one!\n");
}
else
{
printf("\nEnter output file (including path): ");
scanf("%s", Filename);
Saved = Save_File (Filename, Array, Size);
if (Saved != Size)
{
printf("Error in saving! Only %d out of %d long integers saved\n",
Saved, Size);
}
else
{
printf("Saved all %d long integers\n", Saved);
}
}
}
if ( (Response > 2)
&& (Response < 5))
{
if (Array == NULL)
{
printf("\nMust load in data from file (option 1) before sorting one!\n");
}
else
{
// initialize time function
cstart = (double) clock();
// initialize numbers of comparisons and moves
N_Comp = 0;
N_Move = 0;
switch(Response)
{
case 3:
printf("Sorting by Shell Sort (Insertion)\n");
Shell_Insertion_Sort (Array, Size, &N_Comp, &N_Move);
break;
case 4:
printf("Sorting by Improved Bubble Sort\n");
Improved_Bubble_Sort (Array, Size, &N_Comp, &N_Move);
break;
}
// print results: Time, N_Comp, N_Move
cend = (double) clock();
printf("\n");
printf(" Elapsed Time (sec): %f\n", (cend - cstart)/CLOCKS_PER_SEC);
printf(" # Comparisons: %f\n", N_Comp);
printf(" # Moves: %f\n", N_Move);
}
}
}
return (OK);
} // main()