Skip to content
Snippets Groups Projects
Commit 25500bcb authored by adinb's avatar adinb
Browse files

insertion sort and bucket

parent 729bc0e6
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define MAX_VALUE 10000
void bucket_sort(int* sorted_array, int element_num); /* Thread function */
void insertion_sort(int* sorted_array, int size);
int main(int argc, char *argv[]) {
int i;
// Num of threads
int thread_count = atoi(argv[1]);
// Num of elements
int element_num = atoi(argv[2]);
int range = MAX_VALUE / thread_count;
// Seed
srand(time(NULL));
// Bucket
int* bucket = (int *)malloc(sizeof(int) * element_num);
memset (bucket, 0, (sizeof(int) * element_num));
// Sorted Array
int* sorted_array = (int *)malloc(sizeof(int) * element_num);
memset (sorted_array, 0, (sizeof(int) * element_num));
// Do bucket sort
#pragma omp parallel num_threads(thread_count)
bucket_sort(sorted_array, element_num);
for (i=0;i<element_num;i++){
printf("%d\n", sorted_array[i]);
}
return 0;
}
void insertion_sort(int* sorted_array, int size)
{
int n, array[size], c, d, t;
int my_rank = omp_get_thread_num();
int thread_count = omp_get_num_threads();
for (c = 0; c < n; c++) {
array[c] = rand() % (size*(thread_count+1));
}
// Sorting
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d] < array[d-1]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
memcpy(&array, sorted_array+(my_rank * size), size);
}
void bucket_sort(int* sorted_array, int element_num) {
int my_rank = omp_get_thread_num();
int thread_count = omp_get_num_threads();
int bucket_size = element_num / thread_count;
insertion_sort(sorted_array, bucket_size);
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment