Código de ordenação bogosort

Código de ordenação bogosort


0
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <sys/time.h>

int isordered(int vec[], int len) {
int i;
for (i=0; i<len-1; i++)
if (vec[i] > vec[i+1])
return 0;
return 1;
}

void bogosort(int vec[], int len) {
int i, r, tmp;
int iter = 0;

while (!isordered(vec, len)) {
for (i=0; i<len-1; i++) {
r = rand()%len;
tmp = vec[i];
vec[i] = vec[r];
vec[r] = tmp;
}
printf("rIterations: %d", ++iter);
fflush(stdout);
}
printf("n");
}

int main(int argc, char **argv) {
int i;
int *vec;
struct timeval *t;

if (argc < 2) {
printf("Usage: bogosort num...n");
return 1;
}

if (!( vec = malloc((argc-1) * sizeof(int)) )) {
printf("-- Error allocating memory!n");
return 1;
}

for (i=0; i<argc-1; i++)
vec[i] = atoi(argv[i+1]);

//srand(time(NULL));
gettimeofday(t, NULL);
srand(t->tv_usec);
bogosort(vec, argc-1);

for (i=0; i<argc-1; i++)
printf("%d ", vec[i]);
printf("n");
return 0;
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }



Comentários

Postagens mais visitadas deste blog

Instalação NetBeans

Calcular frete pelos correios via PHP