How to write a C Program Sort Array By Segment in C Programming Language ?
This C Program Sort Array By Segment.
Solution:
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <pthread.h>
- struct SortParameters{
- int *arr;
- int begin;
- int end;
- };
- void buildRandomArray(int *arr,int size);
- void * sortArrayBySegment(void * params);
- void printArray(int *arr,int size);
- void Merge(int *A,int *L,int leftCount,int *R,int rightCount);
- void MergeSort(int *A,int n);
- int main(){
- int size=0;
- int cantH=0;
- printf("Ingresar size del arreglo\n");
- scanf("%d", &size);
- printf("Ingresar cant de hilos\n");
- scanf("%d", &cantH);
- if(size % cantH != 0){
- printf("%d no es divisible entre %d\n",size,cantH);
- return -1;
- }
- int *arr=(int *)malloc(sizeof(int)*size);
- buildRandomArray(arr,size);
- int divsize = size / cantH;
- struct SortParameters params;
- pthread_t tid[cantH];
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- params.begin = 0;
- params.end = divsize;
- for (int i = 0; i < cantH; i++)
- {
- params.arr = &arr[i*divsize];
- pthread_create(&tid[i],&attr,sortArrayBySegment,¶ms);
- pthread_join(tid[i],0);
- }
- printArray(arr,size);
- // MergeSort(arr,size);
- // printArray(arr,size);
- printf("\n");
- return 0;
- }
- void buildRandomArray(int *arr,int size){
- for(int i =0;i<size;i++){
- arr[i]= rand()%1000+1;
- }
- }
- void * sortArrayBySegment(void * params){
- printf("%s\n","Corriendo hilo...." );
- struct SortParameters *parameters =(struct SortParameters*)params;
- for(int i=parameters->begin;i<parameters->end;i++){
- for(int j=parameters->begin;j<parameters->end;j++){
- if(parameters->arr[i]<parameters->arr[j]){
- int tmp=parameters->arr[i];
- parameters->arr[i]=parameters->arr[j];
- parameters->arr[j]=tmp;
- }
- }
- }
- pthread_exit(0);
- }
- void printArray(int *arr,int size){
- for(int i =0;i<size;i++){
- printf("%d ",arr[i]);
- }
- }
Learn More :
Segment
Sort
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program Sort Example
- C Program Shell Sort Using C Programming Language
- C Program Selection Sort Using C Programming Language
- C Program to Bubble Sort Using C Programming Language
- C Program Insertion Sort Using C Programming Language
- Heap Sort Using C Programming Language
- C Program To Sort An Array In Ascending And Descending Order
- C Program To Sort An Array Of Names In Alphabetical And Reverse Order
- C Program to sort an array using bubble sort
- C Program to merge and sort two arrays
- Un-sortiertes Array and Sortiertes Array
- matrix sort in C Program
- C program that receives 10 float numbers from the console and sort them in non-ascending order, and prints the result
- C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order
- C Program to accept n numbers from user,store these numbers into an array & sort the number of an array
- C Program to Implement Quick Sort
- QuickVSInsertion Sorting C Program
Array
- How to pass one dimensional array to function in c.
- Write a c program which passes two dimension array to function.
- C Program Array Index Example
- C Program Array Example: Average
- C Program Array Example: Reverse
- C Program Array Example
- C Program to Array
- C Program Array NxM Elements Geometric/Arithmetic
- C Program To Find The Maximum And Minimum Value In An Array
- C Program To Find Union & Intersection Of Two Array
- C Program To Search A Number Inside The Array
- C Program To Sort An Array In Ascending And Descending Order
- C Program To Sort An Array Of Names In Alphabetical And Reverse Order
- C Program To Copy One Character Array Into Another
- Returns: array of decoded values. [0] - count of values
- C Program to Check if a number is in an array[1000]
- C Program to Find max and min in array using pointer concept
- C Program to sort an array using bubble sort
- C Program to find smallest in an array using pointer
- C Program to merge and sort two arrays
- Un-sortiertes Array and Sortiertes Array
- C Program to Array Deserialization
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- Input reads in the array positions and range for random generation.