Scripts
Script em C++ para ordenação de vetor pelo método Bubblesort
Este script faz a ordenação de vetor pelo método Bubblesort ordenando-o de forma crescente e decrescente.
Pessoal uso o NetBeans para compilá-lo quem usar algum outro faça as devidas modificações.
Pessoal uso o NetBeans para compilá-lo quem usar algum outro faça as devidas modificações.
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
//Autor Eliab Coelho Venancio 28_11_11
//versão 1.0 Metodos de ordenação bublesort
using namespace std;
void bubblesortc(int vetor[], int size)//Método bubblesort crescente
{
int tmp;
size=7;
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
if (vetor[j] > vetor[j+1]) {
tmp = vetor[j+1];
vetor[j+1] = vetor[j];
vetor[j] = tmp;
}
}
}
}
void bubblesortd(int vetor[], int size)//Metodo bubblesorte Decrescente
{
int tmp;
size=7;
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
if (vetor[j] < vetor[j+1]) {
vetor[j+1] = vetor[j];
vetor[j] = tmp;
}
}
}
int main(int argc, char **argv) {
int vetor[8];
cout<<"---------------------------------------------\n";
cout<<"--Informe 8 números--\n";
cout<<"---------------------------------------------\n";
for(int i=0;i< 8;i++){
cout<<"Informe um número qualquer na posição ["<< i <<"]:";
cin>>vetor[i];
}
cout<<"---------------------------------------\n";
cout<<" Vetor atual (";
for(int i=0;i< 8;i++){
cout<<vetor[i];
cout<<", ";
}
cout<<")";
cout<<"\n---------------------------------------\n";
bubblesortc(vetor, 7);
cout<<"\n--------------------------------------------------------------------\n";
cout<<" Vetor Ordenado pelo bubbleSort_Crescente (";
for(int i=0;i< 8;i++){
cout<<vetor[i];
cout<<", ";
}
cout<<")";
cout<<"\n-------------------------------------------------------------------\n";
bubblesortd(vetor, 7);
cout<<"\n------------------------------------------------------------------\n";
cout<<" Vetor Ordenado pelo bubbleSort_Descrescente (";
for(int i=0;i< 8;i++){
cout<<vetor[i];
cout<<", ";
}
cout<<")";
cout<<"\n-------------------------------------------------------------------\n";
cout<<"\n\n";
return (EXIT_SUCCESS);
}
Comentários
Postar um comentário