Vediamo un brese e semplice esempio realizzato in c++ di ordinamento BUBBLE SORT
Problema: Ordinare un array di 8 elementi (5 2 8 4 7 1 3 6) utilizzando l’algoritmo di ordinamento Bubble sort.
Codice.cpp
#include <iostream> using namespace std; void bubble(int A[],int n){ int flag=1; int stop=n-1; while(flag==1){ flag=0; for(int i=0; i < stop; i++){ if(A[i]>A[i+1]){ int t; t=A[i]; A[i]=A[i+1]; A[i+1]=t; flag=1; } } stop=stop-1; } } |