Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct bucket
- {
- int count;
- int* value;
- };
- int compareIntegers(const void* first, const void* second)
- {
- int x = *((int*)first), y = *((int*)second);
- if (x == y)
- {
- return 0;
- }
- else if (x < y)
- {
- return -1;
- }
- else
- {
- return 1;
- }
- }
- void Bucket_sort(int array[], int n, int contador, Resul* x)
- {
- time_t t_ini, t_fim;
- long long int swaps=0, compara=0;
- struct bucket buckets[3];
- int i, j, k;
- for (i = 0; i< 3; i++){
- buckets[i].count = 0;
- buckets[i].value = (int*) malloc(sizeof(int) * n);
- }
- for (i = 0; i<n; i++){
- compara++;
- if (array[i] < 0)
- {
- buckets[0].value[buckets[0].count++] = array[i];
- }
- compara++;
- else if (array[i] > 10)
- {
- buckets[2].value[buckets[2].count++] = array[i];
- }
- else
- {
- buckets[1].value[buckets[1].count++] = array[i];
- }
- }
- for (k = 0, i = 0; i< 3; i++){
- // now using quicksort to sort the elements of buckets
- qsort(buckets[i].value, buckets[i].count, sizeof(int), &compareIntegers);
- for (j = 0; j<buckets[i].count; j++){
- array[k + j] = buckets[i].value[j];
- }
- k += buckets[i].count;
- free(buckets[i].value);
- }
- t_fim = time(NULL);
- x[contador].tempo = difftime(t_ini, t_fim);
- x[contador].swaps = swaps;
- x[contador].compara = compara;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement