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[10]; // Dynamic size array based on user input
  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. printf("Thread sorting indices: %d to %d\n", start, end); // Show which part of the array is being sorted
  33.  
  34. // Call the bubbleSort function on the specified range
  35. bubbleSort(arr, start, end);
  36. return NULL;
  37. }
  38.  
  39. // Merge function to merge two sorted subarrays into the final sorted array
  40. void *mergeThread(void *arg) {
  41. int size = 10; // Size of the original array
  42. int mid = size / 2; // Mid-point for splitting the array
  43. int i = 0, j = mid, k = 0;
  44.  
  45. // Merge the two sorted halves into the sorted_arr
  46. while (i < mid && j < size) {
  47. if (arr[i] < arr[j]) {
  48. sorted_arr[k++] = arr[i++]; // Copy the smaller element from the first half
  49. } else {
  50. sorted_arr[k++] = arr[j++]; // Copy the smaller element from the second half
  51. }
  52. }
  53.  
  54. // If there are any remaining elements in the first half, add them
  55. while (i < mid) {
  56. sorted_arr[k++] = arr[i++];
  57. }
  58.  
  59. // If there are any remaining elements in the second half, add them
  60. while (j < size) {
  61. sorted_arr[k++] = arr[j++];
  62. }
  63.  
  64. return NULL;
  65. }
  66.  
  67. int main() {
  68. int size = 10; // Size of the array (fixed as 10 for this example)
  69. pthread_t thread1, thread2, mergeThreadID;
  70.  
  71. // Prompt the user to enter 10 integers for the array
  72. printf("Enter 10 integers for the array: ");
  73. for (int i = 0; i < size; i++) {
  74. scanf("%d", &arr[i]); // Get user input for the array
  75. }
  76.  
  77. // Define the indices for the two halves of the array
  78. int indices1[2] = {0, size / 2}; // First half of the array
  79. int indices2[2] = {size / 2, size}; // Second half of the array
  80.  
  81. // Create two threads to sort the two halves of the array concurrently
  82. pthread_create(&thread1, NULL, sortThread, (void *)indices1); // Sort the first half
  83. pthread_create(&thread2, NULL, sortThread, (void *)indices2); // Sort the second half
  84.  
  85. // Wait for both sorting threads to finish
  86. pthread_join(thread1, NULL);
  87. pthread_join(thread2, NULL);
  88.  
  89. // Create a thread to merge the two sorted halves into a final sorted array
  90. pthread_create(&mergeThreadID, NULL, mergeThread, NULL);
  91. pthread_join(mergeThreadID, NULL); // Wait for the merge thread to finish
  92.  
  93. // Print the final sorted array
  94. printf("Sorted array: ");
  95. for (int i = 0; i < size; i++) {
  96. printf("%d ", sorted_arr[i]); // Output each element of the sorted array
  97. }
  98. printf("\n");
  99.  
  100. return 0; // Return successful execution
  101. }
  102.  
Success #stdin #stdout 0s 5260KB
stdin
Standard input is empty
stdout
Enter 10 integers for the array: Thread sorting indices: 5 to 10
Thread sorting indices: 0 to 5
Sorted array: 0 0 0 0 0 0 0 0 0 0