Dev - C++ - [Ayuda] Problema bubblesort

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

[Ayuda] Problema bubblesort

Publicado por Kevin Nicolas (4 intervenciones) el 30/09/2019 23:47:51
Hola buenas soy nuevo en el foro ojala me puedan ayudar.
Resulta que quiero ordenar unos datos en una matriz con el bubblesort, pero no puedo y no se porque, aca les dejo el código.
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
#include <iostream>
#include <stdlib.h>
using namespace std;
 
int main (){
 
	int cont1=0, cont2=0;
	int temp[4],temp_v[cont1];
 
 
	for(int i=0;i<5;i++){
 
		cin>>temp[i];
 
		if(temp[i]>=-5&&temp[i]<=40){
 
			temp_v[i]=temp[i];
 
			cont1++;
		}
 
		else{
 
			cont2++;
		}
 
	}
 
	for(int i=0;i<cont1-1;i++){
 
		for(int j=0;j<cont1-i-1;j++)
		{
 
			if(temp_v[j]>temp_v[j+1])
			{
 
				int aux=temp_v[j];
				temp_v[j]=temp_v[j+1];
				temp_v[j+1]=aux;
 
			}
 
		}
 
	}
 
 
	for(int i=0;i<cont1;i++){
 
		cout<<temp_v[i]<<endl;
	}
 
	cout<<"Numero de temperaturas validas "<<cont1<<endl;
	cout<<"Numero de temperaturas invalidas "<<cont2<<endl;
	cout<<"Tempreatura minima del dia "<<temp_v[0]<<endl;
	cout<<"Temperatura maxima del dia "<<temp_v[cont1-1]<<endl;
 
 
 
	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
Imágen de perfil de Alfil
Val: 4.344
Oro
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

[Ayuda] Problema bubblesort

Publicado por Alfil (1444 intervenciones) el 02/10/2019 21:09:27
Desconozco el objetivo de tu programa, te pongo única y exclusivamente el método de ordenación por BubbleSort, sólo debes adaptarlo a tus necesidades.

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
#include <iostream>
 
using namespace std;
 
void intercambio( int *a, int *b );
void bubbleSort( int v[], int n );
void imprimir( int v[], int n );
 
int main()
{
    int v[] = { 82, 31, 28, 19, 29, 9, 96, 57, 43};
    int n = sizeof( v ) / sizeof( v[0] );
 
    bubbleSort( v, n );
 
    cout << "\nOrdenado mediante BubleSort: ";
    imprimir( v, n );
 
    return 0;
}
 
void intercambio( int *a, int *b )
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
 
void bubbleSort( int v[], int n )
{
    for( int i = 0; i < n-1; i++ ){
        for( int j = 0; j < n-i-1; j++) {
            if( v[j] > v[j+1])
                intercambio( &v[j], &v[j+1] );
        }
   }
}
 
void imprimir( int v[], int n )
{
    for( int i = 0; i < n; i++)
        cout << v[i] << " ";
 
    cout << endl;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar