Ir ao conteúdo
  • Cadastre-se

To tentando rodar o SelectSort e o InsertSort no mesmo vetor, mas roda ordenado


Posts recomendados

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

void SelectSort(int vet[5000])
{
	int min,aux,c,cc;
	for(int i=0;i<5000;i++)
	{
		min=i;
		for(int j=i+1;j<5000;j++)
		{
			min=j;
			cc++;
		}
		aux=vet[min];
		vet[min]=vet[i];
		vet[i]=aux;
	}
	cout<<"\nQuantidade de Trocas SelectSort-> "<<c;
	cout<<"\n";
	cout<<"Quantidade de Comparacoes-> "<<cc;
}
void InsertSort(int v[5000])
{
	int i,key,c=0,cc=0;
	for(int j=0;j<5000;j++)
	{
		key=v[i];
		for(i=j-1;(i>=0)&&(v[i]>key);i--)
		{
			cc++;
			v[i+1]=v[i];
			c++;
		}
		v[i+1]=key;
	}
	cout<<"\nQuantidade de Trocas InsertSort-> "<<c;
	cout<<"\n";
	cout<<"Quantidade de Comparacoes InsertSort-> "<<cc;
}
main(){
	srand(time(NULL));
	int vet[5000]={};
	int v[5000]={};
	SelectSort(vet);
	InsertSort(v);
	cout<<"<<<<<<<<<<<<<<<< NUMEROS GERADOS >>>>>>>>>>>>>>>>"<<endl;
	for(int i=0;i<5000;i++){
		vet[i]=rand()%5000;
		v[i]=rand()%5000;
		
		cout<<" Gerados Select: "<<vet[i];
		cout<<"\n";
		cout<<" Gerados Insert: "<<v[i];
	}
	SelectSort(vet);
	InsertSort(v);
	for(int j=0;j<5000;j++){
		cout<<vet[j]<<"|";
		//cout<<"\n";
		cout<<v[j]<<"||";
	}
}

 O Select não ordenada os números e o Insert não ordenada também e fica repetindo um único número;

deem um help aqui por favor.

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

@DoodohRafael     você esqueceu de colocar o if ali no selectsort .

veja como o código ficaria :

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

void SelectSort(int vet[5000])
{
	int min,aux,c,cc;
	for(int i=0;i<25;i++)
	{
		min=i;
		for(int j=i+1;j<25;j++)
		{
		    if(vet[min] > vet[j]){
			    min=j;
			    cc++;
		    }
		}
		aux=vet[min];
		vet[min]=vet[i];
		vet[i]=aux;
	}
	cout<<"\nQuantidade de Trocas SelectSort-> "<<c<<endl;
	cout<<"Quantidade de Comparacoes-> "<<cc;
}
void InsertSort(int v[5000])
{
	int i,key,c=0,cc=0;
	for(int j=0;j<25;j++)
	{
		key=v[i];
		for(i=j-1;(i>=0)&&(v[i]>key);i--)
		{
			cc++;
			v[i+1]=v[i];
			c++;
		}
		v[i+1]=key;
	}
	cout<<"\nQuantidade de Trocas InsertSort-> "<<c;
	cout<<"\n";
	cout<<"Quantidade de Comparacoes InsertSort-> "<<cc;
}
int main(){
	srand(time(NULL));
	int vet[5000]={};
	int v[5000]={};
	SelectSort(vet);
	InsertSort(v);
	cout<<"<<<<<<<<<<<<<<<< NUMEROS GERADOS >>>>>>>>>>>>>>>>"<<endl;
	for(int i=0;i<25;i++){
		vet[i]=rand()%5000;
		v[i]=rand()%5000;

		cout<<" Gerados Select: "<<vet[i];
		cout<<"    Gerados Insert: "<<v[i]<<endl;
	}
	SelectSort(vet);
	//InsertSort(v);
	for(int j=0;j<25;j++){
		cout<<j+1<<" numero "<<vet[j]<<"  |"<<endl;
		//cout<<"\n";
		//cout<<v[j]<<"|| ";
	}
}

 

Link para o comentário
Compartilhar em outros sites

Ah sim, falta de atenção; Mas o InsertSort ainda continua não ordenando, ele fica repetindo o mesmo número;

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

void SelectSort(int vet[5000])
{
	int min,aux,c,cc;
	for(int i=0;i<5000;i++)
	{
		min=i;
		for(int j=i+1;j<5000;j++)
		{
			if(vet[min] > vet[j])
			{
				min=j;
				cc++;
			}
			
		}
		aux=vet[min];
		vet[min]=vet[i];
		vet[i]=aux;
	}
	cout<<"\nQuantidade de Trocas SelectSort-> "<<c<<endl;
	cout<<"\n";
	cout<<"Quantidade de Comparacoes SelectSort-> "<<cc<<endl;
}
void InsertSort(int v[5000])
{
	int i,key,c=0,cc=0;
	for(int j=0;j<5000;j++)
	{
		key=v[i];
		for(i=j-1;(i>=0)&&(v[i]>key);i--)
		{
			cc++;
			v[i+1]=v[i];
			c++;
		}
		v[i+1]=key;
	}
	cout<<"\nQuantidade de Trocas InsertSort-> "<<c<<endl;
	cout<<"\n";
	cout<<"Quantidade de Comparacoes InsertSort-> "<<cc<<endl;
}
main(){
	srand(time(NULL));
	int vet[5000]={};
	int v[5000]={};
	SelectSort(vet);
	InsertSort(v);
	cout<<"<<<<<<<<<<<<<<<< NUMEROS GERADOS >>>>>>>>>>>>>>>>"<<endl;
	for(int i=0;i<5000;i++){
		vet[i]=rand()%5000;
		v[i]=rand()%5000;
		
		cout<<" Gerados Select: "<<vet[i];
		cout<<"\n";
		cout<<" Gerados Insert: "<<v[i];
	}
	SelectSort(vet);
	InsertSort(v);
	for(int j=0;j<5000;j++){
		cout<<vet[j]<<"|";
		//cout<<" ";
		cout<<v[j]<<" ";
	}

}

 

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

@DoodohRafael     No insertsort você colocou um loop for, mas o melhor jeito é com o while 

então modifiquei essas linhas do código, experimente assim :

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

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

		while ((j >= 0) && (key < v[j]))
		{
			cc++;
			v[j+1]=v[j];
			j = j - 1;
			c++;
		}
		v[j+1]=key;
	}
	cout<<"\nQuantidade de Trocas InsertSort-> "<<c;
	cout<<"\n";
	cout<<"Quantidade de Comparacoes InsertSort-> "<<cc<<endl;
}
int main(){
	srand(time(NULL));
	int vet[5000]={};
	int v[5000]={};
	SelectSort(vet);
	InsertSort(v);
	cout<<"\n<<<<<<<<<<<<<<<< NUMEROS GERADOS >>>>>>>>>>>>>>>>\n"<<endl;
	for(int i=0;i<25;i++){
		vet[i]=rand()%5000;
		v  [i]=rand()%5000;

		cout<<" Gerados Select: "<<vet[i];
		cout<<"    Gerados Insert: "<<v[i]<<endl;
	}
	SelectSort(vet);
	InsertSort(v);
	printf("\n==== Numeros Organizados Em Ordem Crescente ===\n\n");
	for(int j=0;j<25;j++){
        cout<<j+1<<"  numero "<<vet[j]<<"   |";
		cout<<"    numero "<<v[j]<<"   ||"<<endl;
	}
	getch();
}

 

Link para o comentário
Compartilhar em outros sites

27 minutos atrás, devair1010 disse:

@DoodohRafael     No insertsort você colocou um loop for, mas o melhor jeito é com o while 

então modifiquei essas linhas do código, experimente assim :


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

void SelectSort(int vet[5000])
{
	int min,aux,c,cc;
	for(int i=0;i<5000;i++)
	{
		min=i;
		for(int j=i+1;j<5000;j++)
		{
			if(vet[min] > vet[j])
			{
				min=j;
				cc++;
			}
			
		}
		aux=vet[min];
		vet[min]=vet[i];
		vet[i]=aux;
	}
	cout<<"\nQuantidade de Trocas SelectSort-> "<<c<<endl;
	cout<<"\n";
	cout<<"Quantidade de Comparacoes SelectSort-> "<<cc<<endl;
}
void InsertSort(int v[5000])
{
	int i,key,c=0,cc=0;
	for(int j=0;j<5000;j++)
	{
		key=v[i];
		//for(i=j-1;(i>=0)&&(v[i]>key);i--)
		while((j >= 0) && (key < v[j]))
		{
			cc++;
			v[i+1]=v[i];
			j=j-1;
			c++;
		}
		v[i+1]=key;
	}
	cout<<"\nQuantidade de Trocas InsertSort-> "<<c<<endl;
	cout<<"\n";
	cout<<"Quantidade de Comparacoes InsertSort-> "<<cc<<endl;
}
main(){
	srand(time(NULL));
	int vet[5000]={};
	int v[5000]={};
	SelectSort(vet);
	InsertSort(v);
	cout<<"<<<<<<<<<<<<<<<< NUMEROS GERADOS >>>>>>>>>>>>>>>>"<<endl;
	for(int i=0;i<5000;i++){
		vet[i]=rand()%5000;
		v[i]=rand()%5000;
		
		cout<<" Gerados Select: "<<vet[i];
		cout<<"\n";
		cout<<" Gerados Insert: "<<v[i];
	}
	SelectSort(vet);
	InsertSort(v);
	for(int j=0;j<5000;j++){
		cout<<vet[j]<<"  |  ";
		//cout<<v[j];
	}
	
	for(int i=0;i<5000;i++){
		cout<<v[i]<<"||";
	}
}

Tentei assim agora, coloquei pra mostrar ordenado em outro for e mudei pra while, mas n funcionou, o Insert ainda fica desordenado;

 

  • 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...

Ebook grátis: Aprenda a ler resistores e capacitores!

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!