#include #include #include /* ============================================================ RETURNS THE MAX ELEMENT FROM A VECTOR ============================================================ */ int getMax(int size, int elements[]){ int i; int max = elements[0]; for(i=1; i max) max = elements[i]; } return(max); } /* ============================================================ RETURNS THE MIN ELEMENT FROM A VECTOR ============================================================ */ int getMin(int size, int elements[]){ int i; int min = elements[0]; for(i=1; i 0){ divNum = intPart / 10; intPart = (int)divNum; frac = divNum - intPart; numDigits++; } } return(numDigits); } /* ============================================================ RETURNS THE ITH DIGIT OF A NUMBER ============================================================ */ int returnDigit(int num, int posDigit){ int i, intPart, digit; float divNum, frac, result; intPart = num; for(i=0;i<=posDigit;i++){ divNum = (float)intPart / 10; intPart = (int)divNum; frac = divNum - intPart; result = round(frac * 10); } digit = (int)result; return(digit); } /* ============================================================ SELECT SORT ============================================================ */ void selectSort(int v[], int size){ int i, j, min, x; for(i=0;i= j*10){ k++; } else{ break; } } //Put elements in the bucket for(l=1;l<=buckets[k][0];l++){ } buckets[k][l] = v[i]; buckets[k][0]++; } //Sort the buckets with a sorting algorithm for(i=0;i 0){ //Put the elements in a vector to be sorted sortedBucked = (int *)malloc(buckets[i][0] * sizeof(int)); for(j=1;j<=buckets[i][0];j++){ sortedBucked[j-1] = buckets[i][j]; } //Sort the elements of the bucket selectSort(sortedBucked,buckets[i][0]); //Put the elements back to the bucket for(j=1;j<=buckets[i][0];j++){ buckets[i][j] = sortedBucked[j-1]; } free(sortedBucked); } } //Iterate over the buckets to put elements back to the original list k = 0; for(i=0;i 0){ for(j=1;j<=buckets[i][0];j++){ v[k] = buckets[i][j]; k++; } } } free(buckets); } /* ============================================================ PIGEONHOLE SORT ============================================================ */ void pigeonholeSort(int v[], int size){ printf("\n========== PIGEONHOLE SORT =============="); int i, j, k, sizePG, **pigeonhole; //Min and max elements int max = getMax(size,v); int min = getMin(size,v); //Create the pigeonhole sizePG = (max-min)+2; pigeonhole = (int **)malloc((sizePG-1) * sizeof(int *)); for(i=0;i<(sizePG-1);i++){ pigeonhole[i] = (int *)malloc(sizePG * sizeof(int)); } for(i=0;i<(sizePG-1);i++){ for(j=0;j 0){ for(j=1;j<=pigeonhole[i][0];j++){ v[k] = pigeonhole[i][j]; k++; } } } free(pigeonhole); } /* ============================================================ RADIX SORT ============================================================ */ void radixSort(int v[], int size){ printf("\n========== RADIX SORT =============="); int i, j, k, l, **buckets, digit; //Add the positive min elements to all numbers //This is done to handle negative values int min = getMin(size,v); if(min < 0){ min = min*-1; } for(i=0;i 0){ for(j=1;j<=buckets[l][0];j++){ v[k] = buckets[l][j]; k++; } } } //Set the buckets empty for(k=0;k<10;k++){ for(l=0;l 0){ for(k=0; k Counting Sort\n"); printf("2 -> Pigeonhole Sort\n"); printf("3 -> Bucket Sort\n"); printf("4 -> Radix Sort\n"); printf("5 -> Sair\n"); } /* ============================================================ MAIN FUNCTION ============================================================ */ int main(){ int option; while(option != 5){ menu(); scanf("%d",&option); switch(option){ case 1: createVector(1); break; case 2: createVector(2); break; case 3: createVector(3); break; case 4: createVector(4); break; default: option = 5; break; } } return EXIT_SUCCESS; }