Ir ao conteúdo
  • Cadastre-se

Alguém sabe de meus contadores "c++ e cc++" estão em seus devidos lugares?


Posts recomendados

Quero que o algoritmo me informe a Quantidade de trocas e de comparações, mas  não sei se coloquei nos lugares corretos;

 

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

void SelectSort(int vet[10])
{
	int min,aux,c,cc;
	for(int i=0;i<10;i++)
	{
		min=i;
		for(int j=i+1;j<10;j++)
		{
			if(vet[min] > vet[j])
			{
				cc++;
				min=j;
				
			}
			
		}
		aux=vet[min];
		vet[min]=vet[i];
		vet[i]=aux;
		c++;
	}
	cout<<"\nQuantidade de Trocas SelectSort-> "<<c<<endl;
	//cout<<"\n";
	cout<<"Quantidade de Comparacoes SelectSort-> "<<cc<<endl;
}
void InsertSort(int v[10])
{
	int i,j,key,c=0,cc=0;
	for(j=0;j<10;j++)
	{
		key=v[j];
		for(i=j-1;(i>=0)&&(v[i]>key);i--)
		{
			
			v[i+1]=v[i];
			cc++;
			
		}
		v[i+1]=key;
		c++;
	}
	cout<<"\nQuantidade de Trocas InsertSort-> "<<c<<endl;
	//cout<<"\n";
	cout<<"Quantidade de Comparacoes InsertSort-> "<<cc<<endl;
}

void BubbleSort(int vetb[10]){
	int aux,c=0,cc=0;
	for(int i=0;i<10;i++)
	{
		for(int j=i+1;j<10;j++)//AQUI EU INFORMO QUE J É O PRÓXIMO DO VETOR, POR ISSO O +1;
		{
			cc++;
			if(vetb[i] > vetb[j])
			{
				aux=vetb[i];
				vetb[i]=vetb[j];
				vetb[j]=aux;
				c++;
			}
		}
		
	}
	cout<<"\nQuantidade de Trocas BubbleSort-> "<<c<<endl;
	//cout<<"\n";
	cout<<"Quantidade de Comparacoes BubbleSort-> "<<cc<<endl;	
}
main(){
	srand(time(NULL));
	int vet[10]={};
	int v[10]={};
	int vetb[10]={};

	cout<<"<<<<<<<<<<<<<<<< NUMEROS GERADOS >>>>>>>>>>>>>>>>"<<endl;
	for(int i=0;i<10;i++){
		vet[i]=rand()%100;
		v[i]=rand()%10;
		vetb[i]=rand()%1000;
		
		cout<<" Gerados Select: "<<vet[i];
		//cout<<"\n";
		cout<<"   Gerados Insert: "<<v[i];
		//cout<<"\n";
		cout<<" Gerados Bubble: "<<vetb[i];
	}
	
	SelectSort(vet);
	for(int j=0;j<10;j++)
	{
		cout<<j+1<<"   SelectSort: "<<vet[j]<<"   |"<<endl;
		
		//cout<<"   Insert: "<<vet[j]<<"   ||"<<endl;

	}
	cout<<"     "<<endl;
	
	InsertSort(v);
	cout<<"\n\n";
	for(int i=0;i<10;i++)
	{
		cout<<"InsertSort: "<<v[i]<<"   ||"<<endl;
		
	}
	cout<<"    "<<endl;
	
	BubbleSort(vetb);
	for(int j=0;j<10;j++)
	{
		cout<<"BubbleSort: "<<vetb[j]<<"    |||"<<endl;
	}

}

 

  • Curtir 1
Link para o comentário
Compartilhar em outros sites

@DoodohRafael    as comparações e trocas no selectsort está certo, no insertsort não tem comparações e as trocas estão certas, no bubblesort  as comparações e as trocas ficam no mesmo lugar dentro do if .

veja como ficaria seu código :

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
using namespace std;
void SelectSort(int vet[10]){
	int min,aux,c=0,cc=0;
	for(int i=0;i<10;i++){
		min=i;
		for(int j=i+1;j<10;j++){
			if(vet[min] > vet[j]){
				cc++;
				min=j;
			}
		}
		aux=vet[min];
		vet[min]=vet[i];
		vet[i]=aux;
		c++;
	}
	cout<<"\nQuantidade de Trocas SelectSort-> "<<c<<endl;
	cout<<"Quantidade de Comparacoes SelectSort-> "<<cc<<"    < Tecle >"<<endl;
	getch();
}
void InsertSort(int v[10]){
	int i,j,key,c=0,cc=0;
	for(j=0;j<10;j++){
		key=v[j];
		for(i=j-1;(i>=0)&&(v[i]>key);i--){
			v[i+1]=v[i];
			//cc++;
		}
		v[i+1]=key;
		c++;
	}
	cout<<"\nQuantidade de Trocas InsertSort-> "<<c<<endl;
	cout<<"Quantidade de Comparacoes InsertSort-> "<<cc<<"    < Tecle >"<<endl;
	getch();
}
void BubbleSort(int vetb[10]){
	int aux,c=0,cc=0;
	for(int i=0;i<10;i++){
		for(int j=i+1;j<10;j++){//AQUI EU INFORMO QUE J É O PRÓXIMO DO VETOR, POR ISSO O +1;
			if(vetb[i] > vetb[j]){
				aux=vetb[i];
				vetb[i]=vetb[j];
				vetb[j]=aux;
				cc++;
				c++;
			}
		}
	}
	cout<<"\nQuantidade de Trocas BubbleSort-> "<<c<<endl;
	cout<<"Quantidade de Comparacoes BubbleSort-> "<<cc<<"   < Tecle >"<<endl;
	getch();
}
int main(){
	srand(time(NULL));
	int vet[10]={};
	int v[10]={};
	int vetb[10]={};
	cout<<"<<<<<<<<<<<<<<<< NUMEROS GERADOS >>>>>>>>>>>>>>>>"<<endl;
	for(int i=0;i<10;i++){
		vet[i]=rand()%100;
		v[i]=rand()%10;
		vetb[i]=rand()%1000;
		cout<<" Gerados Select: "<<vet[i];
		cout<<"   Gerados Insert: "<<v[i];
		cout<<"   Gerados Bubble: "<<vetb[i]<<endl;
	}
	SelectSort(vet);
	printf("\n\n");
	for(int j=0;j<10;j++){
		cout<<j+1<<"   SelectSort: "<<vet[j]<<"   |"<<endl;
	}
	cout<<"     "<<endl;
	InsertSort(v);
	cout<<"\n\n";
	for(int i=0;i<10;i++){
		cout<<"InsertSort: "<<v[i]<<"   ||"<<endl;
	}
	cout<<"    "<<endl;
	BubbleSort(vetb);
	for(int j=0;j<10;j++){
		cout<<"BubbleSort: "<<vetb[j]<<"    |||"<<endl;
	}
    return 0;
}

 

  • Curtir 1
Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...