-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.cpp
More file actions
158 lines (148 loc) · 4.03 KB
/
ShellSort.cpp
File metadata and controls
158 lines (148 loc) · 4.03 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
156
157
158
/*Shell Sort
Nombre: Erik Villarreal
Fecha: 8 de diciembre 2022
*/
#include <bits/stdc++.h>
#include <iostream>
#include <stdlib.h>
#include <sys/time.h>
using namespace std;
double ShellSort(int[], int);
void create(int[], int);
void imprimir(int[], int);
void menu(int);
void create_random(int array[], int);
double time_exe;
long getTime()
{
struct timeval inicio;
gettimeofday(&inicio, NULL);
return inicio.tv_sec * 5000 + inicio.tv_usec;
}
int main()
{
srand(time(NULL));
bool salida = false;
do
{
int dim = 100 + rand() % (1001 - 100);
int V[dim];
int op;
bool repetir = false;
do
{
menu(dim);
cout << "Ingrese una opcion: ";
cin >> op;
switch (op)
{
case 1:
cout << "***INGRESO DE DATOS***" << endl;
create_random(V, dim);
cout << "Vector cargado..." << endl;
system("pause");
break;
case 2:
cout << "***ORDENANDO VECTOR***" << endl;
time_exe = ShellSort(V, dim);
cout << "Tiempo empleado: " << time_exe << " segundos." << endl;
system("pause");
break;
case 3:
cout << "***VECTOR***" << endl;
imprimir(V, dim);
system("pause");
break;
case 4:
cout << endl << "Saliendo del programa..." << endl;
repetir = true;
break;
case 5:
cout << endl << "Saliendo del programa..." << endl;
repetir = true;
salida = true;
break;
default:
cout << "***OPCION INVALIDA - INGRESE NUEVAMENTE***" << endl;
system("pause");
break;
}
} while (!repetir);
} while (!salida);
return 0;
}
void menu(int dim)
{
system("cls");
cout << "\t***Shell Sort en vector de " << dim << " datos***" << endl << endl;
cout << "1. Ingresar vector" << endl;
cout << "2. Ordenar vector" << endl;
cout << "3. Presentar vectores" << endl;
cout << "4. Repetir" << endl;
cout << "5. SALIR" << endl;
}
void create(int V[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "Elemento [" << i + 1 << "]: ";
cin >> V[i];
}
}
void imprimir(int V[], int n)
{
cout << endl;
cout << "\t|------------------|" << endl;
cout << "\t| VECTOR |" << endl;
cout << "\t|------------------|" << endl;
for (int i = 0; i < n; i++)
{
cout << "[" << i + 1 << "]: " << V[i] << endl;
}
}
double ShellSort(int V[], int dim)
{
long inicio = getTime();
double timeSec;
long timeMicroSec;
double exe_time;
cout << "SE ORDENARAN LOS DATOS..." << endl;
system("pause");
int cont = 1;
for (int i = dim / 2; i > 0; i = i / 2)
{
for (int j = i; j < dim; j++)
{
for (int k = j - i; k >= 0; k = k - i)
{
if (V[k] < V[k + i])
{
break;
}
else
{
int temp = V[k + i];
V[k + i] = V[k];
V[k] = temp;
cout << "Cargando..." << i << endl;
cont++;
}
cout << "\e[A";
}
}
}
cout << "Vector ordenado con: " << cont++ << " iteraciones..." << endl;
long final = getTime();
timeMicroSec = final - inicio;
timeSec = timeMicroSec * pow(10, -6);
exe_time = timeSec;
return exe_time;
}
void create_random(int array[], int dim)
{
for (int i = 0; i < dim; i++)
{
array[i] = 1 + rand() % (10000+1 - 1);
cout << "[" << i + 1 << "]: " << array[i] << endl;
}
}