Dev - C++ - Ayuda no se como agregar el método de ordenamiento de Quicksort y Mergesort

 
Vista:
sin imagen de perfil
Val: 11
Ha aumentado su posición en 2 puestos en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

Ayuda no se como agregar el método de ordenamiento de Quicksort y Mergesort

Publicado por Camarena (7 intervenciones) el 23/02/2020 03:16:26
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <iostream>
#include <stdlib.h>
using namespace std;
 
struct nodo{
       int nro;
       struct nodo *sgte;
};
 
 
typedef struct nodo *Tlista;
 
void insertarInicio(Tlista &lista, int valor)
{
    Tlista q;
    q = new(struct nodo);
    q->nro = valor;
    q->sgte = lista;
    lista  = q;
}
 
void insertarFinal(Tlista &lista, int valor)
{
    Tlista t, q = new(struct nodo);
 
    q->nro  = valor;
    q->sgte = NULL;
 
    if(lista==NULL)
    {
        lista = q;
    }
    else
    {
        t = lista;
        while(t->sgte!=NULL)
        {
            t = t->sgte;
        }
        t->sgte = q;
    }
 
}
 
int insertarAntesDespues()
{
    int _op, band;
    cout<<endl;
    cout<<"\t 1. Antes de la posicion"<<endl;
    cout<<"\t 2. Despues de la posicion"<<endl;
 
    cout<<"\n\t Opcion : "; cin>> _op;
 
    if(_op==1)
        band = -1;
    else
        band = 0;
 
    return band;
}
 
void insertarElemento(Tlista &lista, int valor, int pos)
{
    Tlista q, t;
    int i;
    q = new(struct nodo);
    q->nro = valor;
 
    if(pos==1)
    {
        q->sgte = lista;
        lista = q;
    }
    else
    {
        int x = insertarAntesDespues();
        t = lista;
 
        for(i=1; t!=NULL; i++)
        {
            if(i==pos+x)
            {
                q->sgte = t->sgte;
                t->sgte = q;
                return;
            }
            t = t->sgte;
        }
    }
    cout<<"   Error...Posicion no encontrada..!"<<endl;
}
 
void buscarElemento(Tlista lista, int valor)
{
    Tlista q = lista;
    int i = 1, band = 0;
 
    while(q!=NULL)
    {
        if(q->nro==valor)
        {
            cout<<endl<<" Encontrada en posicion "<< i <<endl;
            band = 1;
        }
        q = q->sgte;
        i++;
    }
 
    if(band==0)
        cout<<"\n\n Numero no encontrado..!"<< endl;
}
 
void reportarLista(Tlista lista)
{
     int i = 0;
 
     while(lista != NULL)
     {
          cout <<' '<< i+1 <<") " << lista->nro << endl;
          lista = lista->sgte;
          i++;
     }
}
 
 
void eliminarElemento(Tlista &lista, int valor)
{
    Tlista p, ant;
    p = lista;
 
    if(lista!=NULL)
    {
        while(p!=NULL)
        {
            if(p->nro==valor)
            {
                if(p==lista)
                    lista = lista->sgte;
                else
                    ant->sgte = p->sgte;
 
                delete(p);
                return;
            }
            ant = p;
            p = p->sgte;
        }
    }
    else
        cout<<" Lista vacia..!";
}
 
 
void Anular(Tlista &lista)
{
   lista=NULL;
}
void Burbuja(Tlista lista){
     Tlista actual , siguiente;
     int t;
 
     actual = lista;
     while(actual->sgte != NULL)
     {
          siguiente = actual->sgte;
 
          while(siguiente!=NULL)
          {
               if(actual->nro > siguiente->nro)
               {
                    t = siguiente->nro;
                    siguiente->nro = actual->nro;
                    actual->nro = t;
               }
               siguiente = siguiente->sgte;
          }
          actual = actual->sgte;
          siguiente = actual->sgte;
 
     }
 
     cout<<"\n\n\tLista ordenada..."<<endl;
}
void Seleccion(Tlista lista){
   Tlista actual, siguiente;   int t;
   actual = lista; int minimo;
   Tlista min=lista;
 
   while(actual->sgte!=NULL){
      minimo=actual->nro;
      min=actual;
      siguiente=actual->sgte;
      while(siguiente!=NULL){
         if(siguiente->nro < minimo){
         minimo = siguiente->nro;
         min = siguiente;
         }
         siguiente=siguiente->sgte;
      }
      t= actual->nro;
      actual->nro=min->nro;
      min->nro=t;
      actual=actual->sgte;
   }
    cout<<"\n\n\tLista ordenada..."<<endl;
}
void Mergesort(Tlista lista){
 
}
void Quicksort(Tlista lista){
 
}
void menu1()
{
    cout<<"\n\t\tLISTA\n\n";
    cout<<" 1. INSERTAR AL INICIO"<<endl;
    cout<<" 2. INSERTAR AL FINAL"<<endl;
    cout<<" 3. INSERTAR EN UNA POSICION"<<endl;
    cout<<" 4. MOSTRAR LISTA"<<endl;
    cout<<" 5. BUSCAR ELEMENTO"<<endl;
    cout<<" 6. ELIMINAR ELEMENTO"<<endl;
    cout<<" 7. Anular Lista"<<endl;
    cout<<" 8. Ordenar por Burbuja"<<endl;
    cout<<" 9. Ordenar por Seleccion"<<endl;
    cout<<" 10. Ordenar por Mergesort"<<endl;
    cout<<" 11. Ordenar por Quicksort"<<endl;
    cout<<" 12. SALIR"<<endl;
 
    cout<<"\n INGRESE OPCION: ";
}
 
 
int main()
{
    Tlista lista = NULL;
    int op;
    int _dato;
    int pos;
 
 
    do
    {
        menu1();  cin>> op;
 
        switch(op)
        {
            case 1:
 
                 cout<< "\n NUMERO A INSERTAR: "; cin>> _dato;
                 insertarInicio(lista, _dato);
            break;
 
 
            case 2:
 
                 cout<< "\n NUMERO A INSERTAR: "; cin>> _dato;
                 insertarFinal(lista, _dato );
            break;
 
 
            case 3:
 
                 cout<< "\n NUMERO A INSERTAR: ";cin>> _dato;
                 cout<< " Posicion : ";       cin>> pos;
                 insertarElemento(lista, _dato, pos);
            break;
            case 4:
 
                 cout << "\n\n MOSTRANDO LISTA\n\n";
                 reportarLista(lista);
            break;
            case 5:
 
                 cout<<"\n Valor a buscar: "; cin>> _dato;
                 buscarElemento(lista, _dato);
            break;
            case 6:
 
                cout<<"\n Valor a eliminar: "; cin>> _dato;
 
                eliminarElemento(lista, _dato);
            break;
            case 7:
                Anular(lista);
                cout<<"La Lista Fue Anulada Exitosamente"<<endl;
                break;
            case 8:
            	Burbuja(lista);
				cout<<"La lista se ordena por Burbuja"<<endl;
				break;
            case 9:
            	Seleccion(lista);
            	cout<<"La lista se ordena por Seleccion"<<endl;
            	break;
            case 10:
            	Mergesort(lista);
            	cout<<"La lista se ordena por Mergesort"<<endl;
            	break;
            case 11:
            	Quicksort(lista);
            	cout<<"La lista se ordena por Quicksort"<<endl;
            	break;
            }
 
        cout<<endl<<endl;
        system("pause");  system("cls");
 
    }while(op!=12);
 
 
   system("pause");
   return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder