fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <pthread.h>
  5.  
  6. // Global array to be sorted
  7. int arr[] = {7, 12, 19, 3, 18, 4, 2, 6, 15, 8};
  8. int sorted_arr[10]; // To store the final sorted result
  9.  
  10. // Function for Bubble Sort on a specified range of the array
  11. void bubbleSort(int *array, int start, int end) {
  12. // Bubble sort for sorting the array between start and end indices
  13. for (int i = start; i < end - 1; i++) {
  14. for (int j = start; j < end - i - 1 + start; j++) {
  15. // If the current element is greater than the next, swap them
  16. if (array[j] > array[j + 1]) {
  17. int temp = array[j];
  18. array[j] = array[j + 1];
  19. array[j + 1] = temp;
  20. }
  21. }
  22. }
  23. }
  24.  
  25. // Thread function for sorting a specific range of the array
  26. void *sortThread(void *arg) {
  27. // Cast the argument back to an integer array containing the start and end indices
  28. int *indices = (int *)arg;
  29. int start = indices[0];
  30. int end = indices[1];
  31.  
  32. // Call the bubbleSort function on the specified range
  33. bubbleSort(arr, start, end);
  34. return NULL;
  35. }
  36.  
  37. // Merge function to merge two sorted subarrays into the final sorted array
  38. void *mergeThread(void *arg) {
  39. int size = 10; // Size of the original array
  40. int mid = size / 2; // Mid-point for splitting the array
  41. int i = 0, j = mid, k = 0;
  42.  
  43. // Merge the two sorted halves into the sorted_arr
  44. while (i < mid && j < size) {
  45. if (arr[i] < arr[j]) {
  46. sorted_arr[k++] = arr[i++]; // Copy the smaller element from the first half
  47. } else {
  48. sorted_arr[k++] = arr[j++]; // Copy the smaller element from the second half
  49. }
  50. }
  51.  
  52. // If there are any remaining elements in the first half, add them
  53. while (i < mid) {
  54. sorted_arr[k++] = arr[i++];
  55. }
  56.  
  57. // If there are any remaining elements in the second half, add them
  58. while (j < size) {
  59. sorted_arr[k++] = arr[j++];
  60. }
  61.  
  62. return NULL;
  63. }
  64.  
  65. int main() {
  66. int size = 10; // Size of the array
  67. pthread_t thread1, thread2, mergeThreadID;
  68.  
  69. // Define the indices for the two halves of the array
  70. int indices1[2] = {0, size / 2}; // First half of the array
  71. int indices2[2] = {size / 2, size}; // Second half of the array
  72.  
  73. // Create two threads to sort the two halves of the array concurrently
  74. pthread_create(&thread1, NULL, sortThread, (void *)indices1); // Sort the first half
  75. pthread_create(&thread2, NULL, sortThread, (void *)indices2); // Sort the second half
  76.  
  77. // Wait for both sorting threads to finish
  78. pthread_join(thread1, NULL);
  79. pthread_join(thread2, NULL);
  80.  
  81. // Create a thread to merge the two sorted halves into a final sorted array
  82. pthread_create(&mergeThreadID, NULL, mergeThread, NULL);
  83. pthread_join(mergeThreadID, NULL); // Wait for the merge thread to finish
  84.  
  85. // Print the final sorted array
  86. printf("Sorted array: ");
  87. for (int i = 0; i < size; i++) {
  88. printf("%d ", sorted_arr[i]); // Output each element of the sorted array
  89. }
  90. printf("\n");
  91.  
  92. return 0; // Return successful execution
  93. }
  94.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Sorted array: 2 3 4 6 7 8 12 15 18 19