C Programming Code Examples C > Arrays and Matrices Code Examples sort even and odd elements of array separately sort even and odd elements of array separately Write a C program to input elements in an array from user and sort all even and odd elements of the given array separately without using any other array. If minimum element of the array is even then all even elements should be placed in sorted order before odd elements otherwise all odd elements should be placed first. How to sort all even and odd elements of a given array separately in C programming. To sort even and odd elements of the array separately. We need to arrange elements in two parts even and odd (proper sorting of elements are not necessary). Then after even and odd elements sorting should be performed separately. #include <stdio.h> #include <limits.h> //Used for INT_MAX #define maxsize 1000 //Maximum size of the array /* Functions used in this program */ void arrange(int arr[], int len, int pivot); void sort(int arr[], int start, int end); void print(int arr[], int len); int main() { int arr[maxsize], x, n; int pivot, evenCount, oddCount; pivot = 0; evenCount = oddCount = 0; /* Reads size and elements in the array */ printf("Enter size of the array: "); scanf("%d", &n); printf("Enter elements in the array: "); for(x=0; x<n; x++) { scanf("%d", &arr[x]); // If current element is odd then increase pivot if(arr[x] & 1) oddCount++; else evenCount++; } /* Pivot is position that separates even and odd elements */ pivot = (evenCount > oddCount) ? evenCount : oddCount; print(arr, n); // Arranges all even and odd elements sequentially arrange(arr, n, pivot); // Print elements after arranging even and odd elements printf("\nElements after arranging even and odd elements separately\n"); print(arr, n); //Sorts even part of the array sort(arr, pivot, n); //Sorts odd part of the array sort(arr, 0, pivot); //Prints the final sorted array printf("\nFinal array after sorting even and odd elements separately\n"); print(arr, n); return 0; } /* Arranges all even and odd elements of the array separately. Puts all even elements first then all odd elements. */ void arrange(int arr[], int len, int pivot) { int x, j, temp; for(x=0; x<pivot; x++) { /* If current element of array is odd put it into odd element place */ if(arr[x] & 1) { for(j=pivot; j<len; j++) { //Look for an even element then swap with odd element if(!(arr[j] & 1)) { temp = arr[j]; arr[j] = arr[x]; arr[x] = temp; break; } } } } } /* Sorts the elements of array within a range */ void sort(int arr[], int start, int end) { int x, j, temp; int len = start + end; for(x=start; x<len; x++) { for(j=x+1; j<len; j++) { if(arr[j] < arr[x]) { temp = arr[x]; arr[x] = arr[j]; arr[j] = temp; } } } } /* Prints the entire integer array */ void print(int arr[], int len) { int x; printf("Elements in the array: "); for(x=0; x<len; x++) { printf("%d ", arr[x]); } printf("\n"); }