Bem simples, mas serve para o que eu estou querendo.
Quem quiser pode envenenar para outros tipos de dados.
Só não usem um array grande demais, quanto mais informação mais lento ele será.
Bom, não achei como enviar um arquivo, então vai o codigo: Bubble_sort.h #ifndef Bubble_sort_h #define Bubble_sort_h #include "WProgram.h"
class Bubble_sort
{
public:
Bubble_sort(unsigned int quantidade);
void sort(unsigned long matriz[]);
private:
unsigned int _quantidade;
}; #endif
Bubble_sort::Bubble_sort(unsigned int quantidade)
{
_quantidade = quantidade;
}
void Bubble_sort::sort(unsigned long matriz[])
{
unsigned int i, j;
unsigned long temp;
for (i=0; i < quantidade; i++)
for(j=0;j < quantidade; j++)
if (matriz < matriz[j]){
_ temp=matriz*;*
matriz*=matriz[j]; matriz[j]=temp; } } É só criar um diretorio chamado Bubble_sort no diretorio libraries. []s*_
dei uma olhada no código fonte disponibilizado e percebi que é possível melhorar seu desempenho. Segue a baixo alguns exemplos.
Versão do BubbleSort otimizada para ordenação ascendente:
void asort (int table[], const int n) {
bool swap;
int temp, top, i = 0;
do {
swap = false;
top = n - i;
for (int j = 1; j < top; ++j) {
if (table[j - 1] > table[j]) {
temp = table[j];
table[j] = table[j - 1];
table[j - 1] = temp;
swap = true;
}
}
++i;
} while(swap);
}
Versão do BubbleSort otimizada para ordenação descendente:
void dsort (int table[], const int n) {
bool swap;
int temp, top, i = 0;
do {
swap = false;
top = n - i;
for (int j = 1; j < top; ++j) {
if (table[j - 1] < table[j]) {
temp = table[j];
table[j] = table[j - 1];
table[j - 1] = temp;
swap = true;
}
}
++i;
} while(swap);
}
Também tem a versão clássica do Knuth:
void bubbleSort(int a[], int n)
/* Sorts in increasing order the array A of the size N */
{
int k;
int bound = n - 1;
int t;
int last_swap;
while (bound) {
last_swap = 0;
for (k = 0; k < bound; k++) {
t = a[k]; /* t is a maximum of A[0]..A[k] */
if (t > a[k + 1]) {
a[k] = a[k + 1]; a[k + 1] = t;
last_swap = k; /* mark the last swap position */
} //if
} //for
bound = last_swap; /*elements after bound already sorted */
} // while
} // bubbleSort