C program to perform operations on two halves' in a single array

In C programming, we can perform different operations on two halves of a single array. This approach is useful when you need to apply distinct sorting criteria to different portions of your data. Let's explore how to split an array into two halves and sort the first half in ascending order while sorting the second half in descending order.

Syntax

// Splitting array into two halves
int mid = n / 2;  // where n is array size

// Bubble sort for ascending order (first half)
for (i = 0; i < mid; i++) {
    for (j = i + 1; j < mid; j++) {
        if (array[i] > array[j]) {
            // swap elements
        }
    }
}

// Bubble sort for descending order (second half)
for (i = mid; i < n; i++) {
    for (j = i + 1; j < n; j++) {
        if (array[i] < array[j]) {
            // swap elements
        }
    }
}

Algorithm

The algorithm to perform operations on two halves of an array follows these steps −

  • Step 1: Read the array size and elements from user
  • Step 2: Calculate the midpoint: mid = n / 2
  • Step 3: Sort first half (0 to mid-1) in ascending order using bubble sort
  • Step 4: Sort second half (mid to n-1) in descending order using bubble sort
  • Step 5: Display both sorted halves

Example

Following is the complete C program to perform operations on two halves of a single array −

#include <stdio.h>

int main() {
    int i, j, temp, n, mid, number[30];
    
    printf("Enter the value of N: ");
    scanf("%d", &n);
    
    mid = n / 2;
    
    printf("Enter the numbers: ");
    for (i = 0; i < n; i++)
        scanf("%d", &number[i]);
    
    printf("\nOriginal array: ");
    for (i = 0; i < n; i++)
        printf("%d ", number[i]);
    
    /* Sort first half in ascending order */
    for (i = 0; i < mid; i++) {
        for (j = i + 1; j < mid; j++) {
            if (number[i] > number[j]) {
                temp = number[i];
                number[i] = number[j];
                number[j] = temp;
            }
        }
    }
    
    /* Sort second half in descending order */
    for (i = mid; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if (number[i] < number[j]) {
                temp = number[i];
                number[i] = number[j];
                number[j] = temp;
            }
        }
    }
    
    printf("<br>\nFirst half (ascending): ");
    for (i = 0; i < mid; i++)
        printf("%d ", number[i]);
        
    printf("\nSecond half (descending): ");
    for (i = mid; i < n; i++)
        printf("%d ", number[i]);
        
    printf("<br>");
    return 0;
}
Enter the value of N: 8
Enter the numbers: 45 23 67 12 89 34 56 78

Original array: 45 23 67 12 89 34 56 78 

First half (ascending): 12 23 45 67 
Second half (descending): 89 78 56 34

Key Points

  • The array is split at index n/2, where the first half contains elements from index 0 to mid-1
  • Bubble sort algorithm is used for both sorting operations with different comparison conditions
  • For ascending order: swap if array[i] > array[j]
  • For descending order: swap if array[i] < array[j]
  • Time complexity is O(n²) due to nested loops in bubble sort

Conclusion

This program demonstrates how to manipulate different portions of an array independently. The technique of splitting arrays and applying different operations is commonly used in divide-and-conquer algorithms and data processing applications.

Updated on: 2026-03-15T14:15:47+05:30

914 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements