Created
October 21, 2022 00:33
-
-
Save yuri117/eab4e5c6845a631a81355d936b677c6b to your computer and use it in GitHub Desktop.
Código muito bom que aprofunda o uso de números aleatórios do prof. Diego Furtado de LabICC2 daqui da USP ICMC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#define M_RAND 846515 | |
#define MAX_RAND 1000 | |
//n_random = (a+m)%b | |
int gera_rand(int a, int m, int b) { | |
return (a+m)%b; | |
} | |
// no C (na stdlib) tem um a função rand() pra fazer isso | |
// srand() para configurar uma seed | |
// usar % na saída do rand, i.e., rand()%b | |
void bubble_sort(int *v, int n) { | |
int aux; | |
for (int i=n-1; i > 0; i--) { // fixando os maiores no fim | |
for (int j = 0; j < i; j++) { //empurra o agora maior para a frente | |
if (v[j] > v[j+1]) { | |
aux = v[j]; | |
v[j] = v[j+1]; | |
v[j+1] = aux; | |
} | |
} | |
} | |
} | |
void printa_v(int *v, int n) { | |
for (int i=0; i < n; i++) { | |
printf("%d ", v[i]); | |
} | |
printf("\n"); | |
} | |
int cmpfunc (const void * a, const void * b) { | |
return ( *(int*)a - *(int*)b ); | |
} | |
int main() { | |
int n = 10; | |
int r = time(NULL); | |
int *v = (int *) malloc(n*sizeof(int)); | |
for (int i=0; i < n; i++) { | |
r = gera_rand(r, M_RAND, MAX_RAND); | |
v[i] = r; | |
} | |
printa_v(v, n); | |
//bubble_sort(v, n); | |
qsort(v, n, sizeof(int), cmpfunc); | |
printa_v(v, n); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment