How to sort the part of an array in C

1 Answer

0 votes
#include <stdio.h>

// Function to sort a portion of the array from start to end (inclusive)
void partialSort(int arr[], int start, int end) {
    int temp;
    for (int i = start; i <= end; i++) {
        for (int j = i + 1; j <= end; j++) {
            if (arr[j] < arr[i]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

int main() {
    int arr[] = {15, 6, 19, 8, 3, 7, 9, 1, 4};
    int size = sizeof(arr) / sizeof(arr[0]);

    // Sort elements from index 2 to 6
    partialSort(arr, 2, 6);

    // Print the updated array
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}



/*
run:
 
15 6 3 7 8 9 19 1 4 
 
*/

 



answered Aug 12, 2025 by avibootz
...