Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///EJERCICIOS 2-4
- #include <stdio.h>
- #include <stdlib.h>
- void swapp (int *a, int *b){
- int aux = *a;
- *a = *b;
- *b = aux;
- }
- void ssort(int array[], int largo){
- int menor, posMenor;
- for(int i = 0; i < largo; i++){
- menor = array[i];
- posMenor = i;
- for(int j = i + 1; j < largo; j++){
- if(array[j] < menor){
- menor = array[j];
- posMenor = j;
- }
- }
- swapp(&array[i], &array[posMenor]);
- }
- }
- void isort(int array[], int largo){
- for(int i = 0; i < largo; i++){
- for(int j = i; j > 0; j--){
- if(array[j] >= array[j-1])
- break;
- else
- swapp(&array[j-1], &array[j]);
- }
- }
- }
- void bsort(int array[], int largo){
- int flag = 0;
- for(int j = largo; j > 0; j--){
- flag = 1;
- for(int i = 0; i < j-1; i++){
- if(array[i] > array[i+1]){
- swapp(&array[i], &array[i+1]);
- flag = 0;
- }
- }
- if(flag) break;
- }
- }
- int main(){
- int array[] = {0,14,-7,9,4,59,-20,1,2,3};
- ssort(array, 10);
- for(int i = 0; i < 10; i++)
- printf("%d ", array[i]);
- printf("\n");
- int array2[] = {0,14,-7,9,4,59,-20,1,2,3};
- isort(array2, 10);
- for(int i = 0; i < 10; i++)
- printf("%d ", array2[i]);
- printf("\n");
- int array3[] = {0,14,-7,9,4,59,-20,1,2,3};
- bsort(array3, 10);
- for(int i = 0; i < 10; i++)
- printf("%d ", array3[i]);
- printf("\n");
- return 0;
- }
- ///EJERCICIO 5
- #include <stdio.h>
- #include <stdlib.h>
- int es_permutacion(int a[], int b[], int largo){
- int aux[largo], flag = 0;
- for(int i = 0; i < largo; i++) aux[i] = 0;
- for(int i = 0; i < largo; i++){
- flag = 0;
- for(int j = 0; j < largo; j++)
- if(b[j] == a[i] && aux[j] == 0){
- aux[j]++;
- flag++;
- break;
- }
- if(!flag)
- return 0;
- }
- return 1;
- }
- int main(){
- int array1[] = {1,2,3,4,5}, array2[] = {1,2,2,3,3,3,4,4,4,4}, array3[] = {4,3,2,1,4,3,2,4,3,4};
- (es_permutacion(array1, array1, 5) == 1)? printf("Está todo bien.\n") : printf("OhNo.mp3\n");
- (es_permutacion(array2, array3, 10) == 1)? printf("Está todo bien.\n") : printf("OhNo.mp3\n");
- (es_permutacion(array1, array2, 5) == 0)? printf("Está todo bien.\n") : printf("OhNo.mp3\n");
- return 0;
- }
- ///EJERCICIO 10
- //Selection NO es estable.
- //Insertion SI es estable.
- //Bubble SI es estable.
- ///EJERCICIOS 11 - 12
- #include <stdio.h>
- #include <stdlib.h>
- typedef enum{
- Random,
- Primero,
- Ultimo,
- Mitad,
- MedianaDe3,
- }TipoPivot;
- void swapp (int *a, int *b){
- int aux = *a;
- *a = *b;
- *b = aux;
- }
- void quicksortPrimero(int array[], int posI, int posF){
- if(posF > posI){
- int menor = posI+1, mayor = posF, pivot = array[posI];
- while(menor <= mayor){
- if(array[menor] < pivot){
- array[menor - 1] = array[menor];
- menor++;
- }
- else{
- swapp(&array[menor], &array[mayor]);
- mayor--;
- }
- }
- array[menor - 1] = pivot;
- quicksortPrimero(array, posI, mayor - 1);
- quicksortPrimero(array, menor, posF);
- }
- }
- void quicksortUltimo(int array[], int posI, int posF){
- if(posF > posI){
- swapp(&array[posI], &array[posF]);
- int menor = posI+1, mayor = posF, pivot = array[posI];
- while(menor <= mayor){
- if(array[menor] < pivot){
- array[menor - 1] = array[menor];
- menor++;
- }
- else{
- swapp(&array[menor], &array[mayor]);
- mayor--;
- }
- }
- array[menor - 1] = pivot;
- quicksortUltimo(array, posI, mayor - 1);
- quicksortUltimo(array, menor, posF);
- }
- }
- void quicksortMitad(int array[], int posI, int posF){
- if(posF > posI){
- swapp(&array[posI], &array[posI + (posF-posI)/2]);
- int menor = posI+1, mayor = posF, pivot = array[posI];
- while(menor <= mayor){
- if(array[menor] < pivot){
- array[menor - 1] = array[menor];
- menor++;
- }
- else{
- swapp(&array[menor], &array[mayor]);
- mayor--;
- }
- }
- array[menor - 1] = pivot;
- quicksortMitad(array, posI, mayor - 1);
- quicksortMitad(array, menor, posF);
- }
- }
- void quicksortRandom(int array[], int posI, int posF){
- if(posF > posI){
- srand(time(NULL));
- swapp(&array[posI], &array[rand()%(posF-posI) + posI]);
- int menor = posI+1, mayor = posF, pivot = array[posI];
- while(menor <= mayor){
- if(array[menor] < pivot){
- array[menor - 1] = array[menor];
- menor++;
- }
- else{
- swapp(&array[menor], &array[mayor]);
- mayor--;
- }
- }
- array[menor - 1] = pivot;
- quicksortRandom(array, posI, mayor - 1);
- quicksortRandom(array, menor, posF);
- }
- }
- int decidirMediana(int array[], int a, int b, int c){
- if(array[a] <= array[b]){
- if(array[b] <= array[c])
- return b;
- if (array[c] >= array[a])
- return c;
- return a;
- }
- if(array[c] >= array[a])
- return a;
- if(array[c] >= array[b])
- return c;
- return b;
- }
- void quicksortMediana(int array[], int posI, int posF){
- if(posF > posI){
- int mediana = decidirMediana(array, posI, posI + (posF - posI) / 2, posF);
- swapp(&array[posI], &array[mediana]);
- int menor = posI+1, mayor = posF, pivot = array[posI];
- while(menor <= mayor){
- if(array[menor] < pivot){
- array[menor - 1] = array[menor];
- menor++;
- }
- else{
- swapp(&array[menor], &array[mayor]);
- mayor--;
- }
- }
- array[menor - 1] = pivot;
- quicksortMediana(array, posI, mayor - 1);
- quicksortMediana(array, menor, posF);
- }
- }
- void quicksort(int array[], int largo, TipoPivot pivot){
- switch(pivot){
- case Primero:
- quicksortPrimero(array, 0, largo-1);
- break;
- case Ultimo:
- quicksortUltimo(array, 0, largo-1);
- break;
- case Mitad:
- quicksortMitad(array, 0, largo-1);
- break;
- case Random:
- quicksortRandom(array, 0, largo-1);
- break;
- case MedianaDe3:
- quicksortMediana(array, 0, largo-1);
- break;
- }
- }
- int main(){
- int array[] = {0,14,-7,9,4,59,-20,1,2,3};
- quicksort(array, 10, MedianaDe3);
- for(int i = 0; i < 10; i++)
- printf("%d ", array[i]);
- printf("\n");
- return 0;
- }
- ///EJERCICIO 14
- #include <stdio.h>
- #include <stdlib.h>
- void swapp (int *a, int *b){
- int aux = *a;
- *a = *b;
- *b = aux;
- }
- void hsort(int array[], int largo){
- ///Armado del heap.
- for(int i = 0; i < largo; i++)
- for(int j = i; j > 0; j = (j-1) / 2){
- if(array[j] > array[(j-1)/2])
- swapp(&array[j], &array[(j-1) / 2]);
- else break;
- }
- ///Eliminaciones del máximo.
- for(int i = largo - 1; i > 0; i--){
- swapp(&array[0], &array[i]);
- for(int j = 0; j < i/2 ;){
- if(j*2 + 2 < i){
- if(array[j] < array[j*2 + 1] || array[j] < array[j*2 + 2]){
- if(array[j*2 + 1] > array[j*2 + 2]){
- swapp(&array[j], &array[j*2 + 1]);
- j = j*2 + 1;
- }
- else{
- swapp(&array[j], &array[j*2 + 2]);
- j = j*2 + 2;
- }
- }
- else break;
- }
- else{
- if(array[j] < array[j*2 + 1]){
- swapp(&array[j], &array[j*2 + 1]);
- j = j*2 + 1;
- }
- else break;
- }
- }
- }
- }
- int main(){
- int array[] = {0,14,-7,9,4,59,-20,1,2,3};
- hsort(array, 10);
- for(int i = 0; i < 10; i++)
- printf("%d ", array[i]);
- printf("\n");
- return 0;
- }
- ///EJERCICIO 16
- #include <stdio.h>
- #include <stdlib.h>
- void swapp (int *a, int *b){
- int aux = *a;
- *a = *b;
- *b = aux;
- }
- void msortAux(int array[], int posI, int posF){
- if(posI < posF){
- int mitad = posI + (posF - posI)/2;
- msortAux(array, posI, mitad);
- msortAux(array, mitad + 1, posF);
- int arrayAux[posF - posI + 1], inicioIzq = posI, inicioDer = mitad + 1;
- for(int i = 0; i <= posF - posI + 1; i++){
- if(inicioIzq == mitad + 1)
- arrayAux[i] = array[inicioDer++];
- else if(inicioDer == posF + 1)
- arrayAux[i] = array[inicioIzq++];
- else{
- if(array[inicioIzq] <= array[inicioDer])
- arrayAux[i] = array[inicioIzq++];
- else
- arrayAux[i] = array[inicioDer++];
- }
- }
- for(int i = 0; i <= posF - posI + 1; i++)
- array[posI + i] = arrayAux[i];
- }
- }
- void msort(int array[], int largo){
- msortAux(array, 0, largo - 1);
- }
- int main(){
- int array[] = {0,14,-7,9,4,59,-20,1,2,3};
- msort(array, 10);
- for(int i = 0; i < 10; i++)
- printf("%d ", array[i]);
- printf("\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment