Last active
June 22, 2018 02:15
-
-
Save lucasmarqs/a748e432333972eb09103255a6d3c1ac to your computer and use it in GitHub Desktop.
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> | |
void bubbleSort(int vet[], int len); | |
int main() { | |
srand(time(NULL)); | |
int vet[10]; | |
int i, r; | |
for (i = 0; i < 10; i++) { | |
r = rand(); | |
vet[i] = r; | |
printf("%i, ", r); | |
} | |
bubbleSort(vet, 10); | |
printf("\n\nOrdenado:\n"); | |
for (i = 0; i < 10; i++) { | |
printf("%i, ", vet[i]); | |
} | |
} | |
void bubbleSort(int vet[], int len) { | |
int i, j, aux; | |
for (i = 0; i < len - 1; i++) { | |
for (j = 0; j < len - 1; j++) { | |
if (vet[j + 1] < vet[j]) { | |
aux = vet[j]; | |
vet[j] = vet[j + 1]; | |
vet[j + 1] = aux; | |
} | |
} | |
} | |
} |
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> | |
void quickSort(int vet[], int left, int right) ; | |
int main() { | |
srand(time(NULL)); | |
int vet[10]; | |
int i, r; | |
for (i = 0; i < 10; i++) { | |
r = rand(); | |
vet[i] = r; | |
printf("%i, ", r); | |
} | |
quickSort(vet, 0, 9); | |
printf("\n\nOrdenado:\n"); | |
for (i = 0; i < 10; i++) { | |
printf("%i, ", vet[i]); | |
} | |
} | |
void quickSort(int vet[], int left, int right) { | |
int i, j, pivot, y; | |
i = left; | |
j = right; | |
pivot = vet[(left + right) / 2]; | |
while(i <= j) { | |
while(vet[i] < pivot && i < right) { | |
i++; | |
} | |
while(vet[j] > pivot && j > left) { | |
j--; | |
} | |
if(i <= j) { | |
y = vet[i]; | |
vet[i] = vet[j]; | |
vet[j] = y; | |
i++; | |
j--; | |
} | |
} | |
if(j > left) quickSort(vet, left, j); | |
if(i < right) quickSort(vet, i, right); | |
} |
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> | |
void shellSort(int vet[], int len); | |
int main() { | |
srand(time(NULL)); | |
int vet[10]; | |
int i, r; | |
for (i = 0; i < 10; i++) { | |
r = rand(); | |
vet[i] = r; | |
printf("%i, ", r); | |
} | |
shellSort(vet, 10); | |
printf("\n\nOrdenado:\n"); | |
for (i = 0; i < 10; i++) { | |
printf("%i, ", vet[i]); | |
} | |
} | |
void shellSort(int vet[], int len) | |
{ | |
int i , j , value; | |
int gap = 1; | |
do { | |
gap = 3*gap+1; | |
} while(gap < len); | |
do { | |
gap /= 3; | |
for(i = gap; i < len; i++) { | |
value = vet[i]; | |
j = i - gap; | |
while (j >= 0 && value < vet[j]) { | |
vet[j + gap] = vet[j]; | |
j -= gap; | |
} | |
vet[j + gap] = value; | |
} | |
} while(gap > 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment